Files
DeployCenter/Modules/Logging.psm1
T
2026-10-15 21:15:33 +02:00

60 lines
1.7 KiB
PowerShell

# DeployCenter - Gestion des journaux
# Compatible Windows PowerShell 5.1
function Initialize-DeployCenterLogging {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$LogRoot,
[AllowNull()]
[string]$IPv4 = $null
)
$script:LogRoot = $LogRoot
if (-not (Test-Path -LiteralPath $script:LogRoot -PathType Container)) {
New-Item -Path $script:LogRoot -ItemType Directory -Force -ErrorAction Stop | Out-Null
}
# L'adresse IPv4 est fournie par Modules\\Network.psm1.
$script:LogIPv4 = $IPv4
$logIpTag = if ([string]::IsNullOrWhiteSpace($script:LogIPv4)) {
'sans-ip'
} else {
$script:LogIPv4 -replace '[^0-9A-Za-z.-]', '_'
}
$script:LogFile = Join-Path $script:LogRoot (
'DeployCenter_{0}_{1}.log' -f $logIpTag, (Get-Date -Format 'yyyyMMdd_HHmmss')
)
}
function Write-Log {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$Message,
[ValidateSet('INFO', 'SUCCESS', 'WARNING', 'ERROR')]
[string]$Level = 'INFO'
)
$line = '{0} [{1}] {2}' -f (Get-Date -Format 'yyyy-MM-dd HH:mm:ss'), $Level, $Message
try {
[System.IO.File]::AppendAllText(
$script:LogFile,
$line + [Environment]::NewLine,
[System.Text.Encoding]::UTF8
)
} catch {
# Le journal graphique reste disponible si le fichier n'est pas accessible.
}
if ($null -ne $script:txtLog -and -not $script:txtLog.IsDisposed) {
$script:txtLog.AppendText($line + [Environment]::NewLine)
$script:txtLog.SelectionStart = $script:txtLog.TextLength
$script:txtLog.ScrollToCaret()
}
}