cmdlets/CommonUtils/classLog.ps1

function New-Log {
    <#
    .SYNOPSIS
        Возвращает экземпляр класса Log.
    .DESCRIPTION
        Создает объект класса Log позволяющий вести журнал действий.
    .PARAMETER FullName
        Полное имя файла журнала.
    .PARAMETER Append
        Если указан, то существующий файл журнала не будет очищаться.
    .EXAMPLE
        #Создать объект для ведения файла журнала
        $Log = New-Log 'c:\.log'
#>

    [CmdletBinding()]
    param(
        [Alias("f")][string]$FullName='',
        [Alias("a")][switch]$Append
    )
    New-Object Log($FullName, $Append)
}# end New-Log


class Log {
    #region Properties
    hidden [String]$FullName=''
    [bool]$Silent=$false
    [string]$TemplateMessage='%Time% %Message%'
    [string]$Symbol='.'
    [int16]$AmountSymbol=45
    #endregion Properties

    #region Constructor
    Log() { $this.SetFullName() }
    Log([String]$FullName, [bool]$Append) { $this.SetFullName($FullName, $Append) }
    #endregion Constructor

    #region Methods
    [string] GetFullName(){ return $this.FullName }

    [string] GetPossibleSubstitutions(){ 
        return @(
            '%Time%',
            '%Data%',
            '%Message%',
            '%TypeMessage%'
        ) 
    }

    [void] SetFullName() { $this.SetFullName('', $false) }
    [void] SetFullName([string]$FullName, [bool]$Append) { 
        $this.FullName = $FullName
        if ('' -ne $this.FullName -and (!$Append -or !(Test-Path $this.FullName))){
            New-Item $this.FullName -ItemType file -Force | Out-Null # создадим лог-файл
        }
    }

    [void] Write() { $this.WriteLog('', '', $false) }
    [void] Write([string]$Message) { $this.WriteLog($Message, '', $false) }
    [void] Write([string]$Message, [bool]$Silent) { $this.WriteLog($Message, '', $Silent) }
   
    [void] WriteColor([string]$Message, [string]$Color) { $this.WriteLog($Message, $Color, $false) }
    
    [void] WriteError([string]$Message) { $this.WriteLog($Message, 'Red', $false) }
    [void] WriteError([string]$Message, [bool]$Silent) { $this.WriteLog($Message, 'Red', $Silent) }
    
    [void] WriteWarning([string]$Message) { $this.WriteLog($Message, 'Yellow', $false) }
    [void] WriteWarning([string]$Message, [bool]$Silent) { $this.WriteLog($Message, 'Yellow', $Silent) }

    hidden [void]WriteLog([string]$Message, [string]$Color, [bool]$Silent){
        $TypeMessage = 'inf'
        switch ($Color) {
            'Red'    { $TypeMessage = 'err' }
            'Yellow' { $TypeMessage = 'wrn' }
        }
        $msg = "{0}" -f $Message
        if ('' -ne $Message){
            $msg = $this.TemplateMessage
            $msg = $msg.Replace('%Time%', (Get-Date -Format "HH:mm:ss"))
            $msg = $msg.Replace('%Data%', (Get-Date -Format "dd.MM.yyyy"))
            $msg = $msg.Replace('%TypeMessage%', $TypeMessage)
            $msg = $msg.Replace('%Message%', $Message)
        }
        if(-not ($Silent -or $this.Silent)){
            if('' -eq $Color){
                Write-Host $msg
            }
            else {
                Write-Host $msg -ForegroundColor $Color
            }
        }
        if ('' -ne $this.FullName){
            $msg | Out-File $this.FullName -append -encoding unicode
        }
    }#end WriteLog

    [void] OutValue([string]$Name, $Value) {
        $msg = $Name.PadRight($this.AmountSymbol, $this.Symbol) + $Value
        Write-Host $msg
        if ('' -ne $this.FullName){
            $msg | Out-File $this.FullName -append -encoding unicode
        }
    }
    #endregion Methods
}#end class Log

#Export-ModuleMember -Function New-Log