Feature: Custom logging date format in JSON

Added the opportunity to customize the logging date format in the JSON file. And optimized how logging is done
This commit is contained in:
Olai Vike Bøe 2024-05-23 15:51:22 +02:00
parent 356a8b8d76
commit c5a644b2d4
3 changed files with 88 additions and 26 deletions

View file

@ -2,12 +2,27 @@
$configPath = "$PSScriptRoot\config.json"
$config = Get-Content -Path $configPath | ConvertFrom-Json
# Get the date format from the configuration, or use the default format if not provided
$dateFormat = $config.logDateFormat
if (-not $dateFormat) {
$dateFormat = "dd/MM/yyyy HH:mm:ss"
}
# Function to log messages with the specified date format
function Log-Message {
param (
[string]$message
)
$timestamp = Get-Date -Format $dateFormat
Write-Output "[$timestamp] - $message" | Out-File -Append -FilePath "$PSScriptRoot\Log.txt" -Encoding utf8
}
# Log the start of the script
Write-Output "[$(Get-Date -Format "dd/MM/yyyy HH:mm:ss")] - Script initiation: Chrome Downloader" | Out-File -Append -FilePath "$PSScriptRoot\Log.txt" -Encoding utf8
Log-Message "Script initiation: Chrome Downloader"
# Check if both options are disabled and log a message
if (-not $config.options.enableRegularVersion -and -not $config.options.enableForcedVersion) {
Write-Output "[$(Get-Date -Format "dd/MM/yyyy HH:mm:ss")] - Configuration error: Both Regular and Forced versions are disabled. Please enable at least one option to proceed." | Out-File -Append -FilePath "$PSScriptRoot\Log.txt" -Encoding utf8
Log-Message "Configuration error: Both Regular and Forced versions are disabled. Please enable at least one option to proceed."
exit
}
@ -32,18 +47,18 @@ if ($config.options.enableRegularVersion) {
if (-not (Test-Path $filesFolder)) {
try {
New-Item -Path $filesFolder -ItemType Directory -ErrorAction Stop
Write-Output "[$(Get-Date -Format "dd/MM/yyyy HH:mm:ss")] - Directory creation: 'Chrome - VERSION' and 'Files' folder successfully created in $PSScriptRoot" | Out-File -Append -FilePath "$PSScriptRoot\Log.txt" -Encoding utf8
Log-Message "Directory creation: 'Chrome - VERSION' and 'Files' folder successfully created in $PSScriptRoot"
} catch {
Write-Output "[$(Get-Date -Format "dd/MM/yyyy HH:mm:ss")] - Error: Directory creation failed - $_" | Out-File -Append -FilePath "$PSScriptRoot\Log.txt" -Encoding utf8
Log-Message "Error: Directory creation failed - $_"
}
}
# Copy items from source folder to destination folder
try {
Copy-Item -Path $sourceFolderRegular\* -Destination $destinationFolder -Recurse -Force -ErrorAction Stop
Write-Output "[$(Get-Date -Format "dd/MM/yyyy HH:mm:ss")] - Success: Regular Template successfully copied to $destinationFolder" | Out-File -Append -FilePath "$PSScriptRoot\Log.txt" -Encoding utf8
Log-Message "Success: Regular Template successfully copied to $destinationFolder"
} catch {
Write-Output "[$(Get-Date -Format "dd/MM/yyyy HH:mm:ss")] - Error: Failed to copy Regular Template - $_" | Out-File -Append -FilePath "$PSScriptRoot\Log.txt" -Encoding utf8
Log-Message "Error: Failed to copy Regular Template - $_"
}
# Download 64-bit Chrome installer
@ -51,9 +66,9 @@ if ($config.options.enableRegularVersion) {
$filePath1 = Join-Path -Path $filesFolder -ChildPath $fileName1
try {
Invoke-RestMethod -Uri $url1 -OutFile $filePath1 -ErrorAction Stop
Write-Output "[$(Get-Date -Format "dd/MM/yyyy HH:mm:ss")] - Download complete: 64-bit version of Chrome successfully downloaded to $filePath1" | Out-File -Append -FilePath "$PSScriptRoot\Log.txt" -Encoding utf8
Log-Message "Download complete: 64-bit version of Chrome successfully downloaded to $filePath1"
} catch {
Write-Output "[$(Get-Date -Format "dd/MM/yyyy HH:mm:ss")] - Error: 64-bit Chrome download failed - $_" | Out-File -Append -FilePath "$PSScriptRoot\Log.txt" -Encoding utf8
Log-Message "Error: 64-bit Chrome download failed - $_"
}
# Download 32-bit Chrome installer
@ -61,9 +76,9 @@ if ($config.options.enableRegularVersion) {
$filePath2 = Join-Path -Path $filesFolder -ChildPath $fileName2
try {
Invoke-RestMethod -Uri $url2 -OutFile $filePath2 -ErrorAction Stop
Write-Output "[$(Get-Date -Format "dd/MM/yyyy HH:mm:ss")] - Download complete: 32-bit version of Chrome successfully downloaded to $filePath2" | Out-File -Append -FilePath "$PSScriptRoot\Log.txt" -Encoding utf8
Log-Message "Download complete: 32-bit version of Chrome successfully downloaded to $filePath2"
} catch {
Write-Output "[$(Get-Date -Format "dd/MM/yyyy HH:mm:ss")] - Error: 32-bit Chrome download failed - $_" | Out-File -Append -FilePath "$PSScriptRoot\Log.txt" -Encoding utf8
Log-Message "Error: 32-bit Chrome download failed - $_"
}
}
@ -72,18 +87,18 @@ if ($config.options.enableForcedVersion) {
if (-not (Test-Path $forceUpdateFolder)) {
try {
New-Item -Path $forceUpdateFolder -ItemType Directory -ErrorAction Stop
Write-Output "[$(Get-Date -Format "dd/MM/yyyy HH:mm:ss")] - Directory creation: 'Chrome - VERSION_force_update' successfully created in $PSScriptRoot" | Out-File -Append -FilePath "$PSScriptRoot\Log.txt" -Encoding utf8
Log-Message "Directory creation: 'Chrome - VERSION_force_update' successfully created in $PSScriptRoot"
} catch {
Write-Output "[$(Get-Date -Format "dd/MM/yyyy HH:mm:ss")] - Error: Force update directory creation failed - $_" | Out-File -Append -FilePath "$PSScriptRoot\Log.txt" -Encoding utf8
Log-Message "Error: Force update directory creation failed - $_"
}
}
# Copy items from forced source folder to force update folder
try {
Copy-Item -Path "$sourceFolderForced\*" -Destination $forceUpdateFolder -Recurse -Force -ErrorAction Stop
Write-Output "[$(Get-Date -Format "dd/MM/yyyy HH:mm:ss")] - Success: Forced Template successfully copied to $forceUpdateFolder" | Out-File -Append -FilePath "$PSScriptRoot\Log.txt" -Encoding utf8
Log-Message "Success: Forced Template successfully copied to $forceUpdateFolder"
} catch {
Write-Output "[$(Get-Date -Format "dd/MM/yyyy HH:mm:ss")] - Error: Failed to copy Forced Template - $_" | Out-File -Append -FilePath "$PSScriptRoot\Log.txt" -Encoding utf8
Log-Message "Error: Failed to copy Forced Template - $_"
}
# If the regular version is not enabled, download 64-bit Chrome installer directly to the force update folder
@ -92,9 +107,9 @@ if ($config.options.enableForcedVersion) {
$filePath1 = Join-Path -Path $forceUpdateFolder -ChildPath $fileName1
try {
Invoke-RestMethod -Uri $url1 -OutFile $filePath1 -ErrorAction Stop
Write-Output "[$(Get-Date -Format "dd/MM/yyyy HH:mm:ss")] - Download complete: 64-bit version of Chrome successfully downloaded to force update folder at $filePath1" | Out-File -Append -FilePath "$PSScriptRoot\Log.txt" -Encoding utf8
Log-Message "Download complete: 64-bit version of Chrome successfully downloaded to force update folder at $filePath1"
} catch {
Write-Output "[$(Get-Date -Format "dd/MM/yyyy HH:mm:ss")] - Error: 64-bit Chrome download to force update folder failed - $_" | Out-File -Append -FilePath "$PSScriptRoot\Log.txt" -Encoding utf8
Log-Message "Error: 64-bit Chrome download to force update folder failed - $_"
}
} else {
# If the regular version is enabled, copy the downloaded 64-bit installer to the force update folder
@ -103,12 +118,12 @@ if ($config.options.enableForcedVersion) {
if (Test-Path $filePath1) {
try {
Copy-Item -Path $filePath1 -Destination $forceUpdateFolder -Force -ErrorAction Stop
Write-Output "[$(Get-Date -Format "dd/MM/yyyy HH:mm:ss")] - Success: 64-bit version of Chrome copied to force update folder at $forceUpdateFolder" | Out-File -Append -FilePath "$PSScriptRoot\Log.txt" -Encoding utf8
Log-Message "Success: 64-bit version of Chrome copied to force update folder at $forceUpdateFolder"
} catch {
Write-Output "[$(Get-Date -Format "dd/MM/yyyy HH:mm:ss")] - Error: Failed to copy 64-bit installer to force update folder - $_" | Out-File -Append -FilePath "$PSScriptRoot\Log.txt" -Encoding utf8
Log-Message "Error: Failed to copy 64-bit installer to force update folder - $_"
}
} else {
Write-Output "[$(Get-Date -Format "dd/MM/yyyy HH:mm:ss")] - Warning: 64-bit version of Chrome was not downloaded and could not be copied to force update folder." | Out-File -Append -FilePath "$PSScriptRoot\Log.txt" -Encoding utf8
Log-Message "Warning: 64-bit version of Chrome was not downloaded and could not be copied to force update folder."
}
}
}

View file

@ -10,15 +10,61 @@ The script reads its configuration from a JSON file named `config.json` located
- `enableRegularVersion`: Boolean flag to enable the downloading of the regular version of Chrome.
- `enableForcedVersion`: Boolean flag to enable the downloading of the forced update version of Chrome.
- `logDateFormat`: A string defining the format of timestamps in log messages.
### Configuration File (`config.json`)
#### Date Configuration
##### `yyyy`: This specifier represents the year portion of the date. It uses four digits to represent the year. For example, 2024.
##### `MM`: This specifier represents the month portion of the date. It uses two digits to represent the month, with leading zeros if necessary. For example, 05 represents May.
##### `dd`: This specifier represents the day portion of the date. It uses two digits to represent the day of the month, with leading zeros if necessary. For example, 23.
##### `HH`: This specifier represents the hour portion of the time in 24-hour format. It uses two digits to represent the hour, ranging from 00 to 23. For example, 14 represents 2 PM in 24-hour format.
<details>
<summary><b>More info on HH format</b></summary>
##### `HH` (24-hour format): When HH is used, it represents the hour portion of the time in a 24-hour format, where the hour is represented with two digits from 00 to 23. The HH specifier does not use AM/PM designators since it covers the full 24-hour range. Example: HH:mm:ss might output 14:30:00, representing 2:30 PM in 24-hour format.
##### `hh` (12-hour format): When hh is used, it represents the hour portion of the time in a 12-hour format, where the hour is represented with one or two digits from 1 to 12. The hh specifier is typically used alongside the tt specifier (AM/PM designator) to indicate whether the time is in the AM or PM. Example: hh:mm:ss tt might output 02:30:00 PM, representing 2:30 PM.
</details>
##### `mm`: This specifier represents the minute portion of the time. It uses two digits to represent the minutes, with leading zeros if necessary. For example, 30.
##### `ss`: This specifier represents the second portion of the time. It uses two digits to represent the seconds, with leading zeros if necessary. For example, 45.
##### `tt`: This specifier represents the AM/PM designator in a 12-hour time format. It is typically used alongside the hh specifier to indicate whether the time is in the AM or PM. For example, AM or PM.
### Examples
```json
"logDateFormat": "yyyy'/'MM'/'dd hh:mm:ss tt"
```
Output: <code>2024/06/29 03:19:30 p.m.</code>
```json
"logDateFormat": "MM/dd/yyyy HH:mm:ss"
```
Output: <code>06.29.2024 15:19:30</code>
```json
"logDateFormat": "dd-MM-yyyy HH:mm:ss"
```
Output: <code>29-06-2024 15:19:30</code>
## Configuration File (`config.json`)
```json
{
"options": {
"enableRegularVersion": true,
"enableForcedVersion": false
}
},
"logDateFormat": "yyyy'/'MM'/'dd hh:mm:ss tt"
}
```
## Script Usage

View file

@ -1,6 +1,7 @@
{
"options": {
"enableRegularVersion": true,
"enableForcedVersion": false
}
}
"options": {
"enableRegularVersion": true,
"enableForcedVersion": false
},
"logDateFormat": "dd/MM/yyyy HH:mm:ss"
}