Création du projet DaliACLI
- CLI PowerShell pour la gestion des sorties DALI adressable (ECxLightDaliA) - Action Read : rapport CSV des configurations (une ligne par output) - Action Write : modification des propriétés via POST JSON - Fallback curl.exe -k pour les automates avec problèmes SSL
This commit is contained in:
95
modules/Logger.psm1
Normal file
95
modules/Logger.psm1
Normal file
@@ -0,0 +1,95 @@
|
||||
# Module Logger - Gestion des logs console + fichier
|
||||
|
||||
$script:LogFile = $null
|
||||
$script:Stopwatch = $null
|
||||
$script:Stats = @{
|
||||
AutomatesTotal = 0
|
||||
AutomatesError = 0
|
||||
OutputsProcessed = 0
|
||||
}
|
||||
|
||||
function Initialize-Logger {
|
||||
[CmdletBinding()]
|
||||
param()
|
||||
|
||||
$timestamp = Get-Date -Format "yyyy-MM-dd_HH\hmm"
|
||||
$script:LogFile = Join-Path (Get-Location) "dali_$timestamp.log"
|
||||
New-Item -ItemType File -Path $script:LogFile -Force | Out-Null
|
||||
|
||||
$script:Stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
|
||||
|
||||
$script:Stats = @{
|
||||
AutomatesTotal = 0
|
||||
AutomatesError = 0
|
||||
OutputsProcessed = 0
|
||||
}
|
||||
|
||||
Write-Log -Message "Logger initialise - fichier: $($script:LogFile)" -Level INFO
|
||||
}
|
||||
|
||||
function Write-Log {
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[Parameter(Mandatory)]
|
||||
[string]$Message,
|
||||
|
||||
[ValidateSet("INFO", "WARN", "ERROR", "SUCCESS")]
|
||||
[string]$Level = "INFO"
|
||||
)
|
||||
|
||||
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
||||
$line = "[$timestamp] [$Level] $Message"
|
||||
|
||||
# Couleur console selon le niveau
|
||||
switch ($Level) {
|
||||
"INFO" { Write-Host $line -ForegroundColor Cyan }
|
||||
"WARN" { Write-Host $line -ForegroundColor Yellow }
|
||||
"ERROR" { Write-Host $line -ForegroundColor Red }
|
||||
"SUCCESS" { Write-Host $line -ForegroundColor Green }
|
||||
}
|
||||
|
||||
# Ecriture fichier
|
||||
if ($script:LogFile) {
|
||||
Add-Content -Path $script:LogFile -Value $line -Encoding UTF8
|
||||
}
|
||||
}
|
||||
|
||||
function Update-Stats {
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[Parameter(Mandatory)]
|
||||
[ValidateSet("AutomatesTotal", "AutomatesError", "OutputsProcessed")]
|
||||
[string]$Counter,
|
||||
|
||||
[int]$Increment = 1
|
||||
)
|
||||
|
||||
$script:Stats[$Counter] += $Increment
|
||||
}
|
||||
|
||||
function Write-Summary {
|
||||
[CmdletBinding()]
|
||||
param()
|
||||
|
||||
$script:Stopwatch.Stop()
|
||||
$duration = $script:Stopwatch.Elapsed
|
||||
|
||||
Write-Log -Message "========== RESUME ==========" -Level INFO
|
||||
Write-Log -Message "Automates traites : $($script:Stats.AutomatesTotal)" -Level INFO
|
||||
Write-Log -Message "Automates en erreur : $($script:Stats.AutomatesError)" -Level $(if ($script:Stats.AutomatesError -gt 0) { "WARN" } else { "INFO" })
|
||||
Write-Log -Message "Outputs traites : $($script:Stats.OutputsProcessed)" -Level INFO
|
||||
Write-Log -Message "Duree totale : $($duration.ToString('hh\:mm\:ss\.ff'))" -Level INFO
|
||||
Write-Log -Message "============================" -Level INFO
|
||||
}
|
||||
|
||||
function Get-LogDirectory {
|
||||
[CmdletBinding()]
|
||||
param()
|
||||
|
||||
if ($script:LogFile) {
|
||||
return Split-Path $script:LogFile -Parent
|
||||
}
|
||||
return $null
|
||||
}
|
||||
|
||||
Export-ModuleMember -Function Initialize-Logger, Write-Log, Update-Stats, Write-Summary, Get-LogDirectory
|
||||
Reference in New Issue
Block a user