Skip to main content

Console

Console

From version 2.3.3 it supports acquiring lock for issues with git prompt that sometimes gets splitted during output. The mutex name to acquire is ConsoleMtx

Configuration options

OptionTypeMandatoryDefaultDescription
LevelStringNoDefault LevelDefines the lowest logging level to logged
FormatStringNoDefault FormatDefines a custom format for the target
PrintExceptionBooleanNo$truePrints the stacktrace when an exception object is passed to Write-Log
ColorMappingHashtableNoSee belowOverrides the default color mappings
OnlyColorizeLevelBooleanNo$falseIf set to true, only the level name is colorized instead of the whole log row.
ShortLevelBooleanNo$falseIf true the written level name is trimmed to three chars, ie ERROR -> ERR. This makes the logs more aligned and easier to read.
Default color mappings
LevelColor
SQLMagenta
DEBUGCyan
INFODarkGray
WARNINGYellow
ERRORRed
NOTICEGray
VERBOSEYellow
SUCCESSGreen
CRITICALRed
ALERTRed
EMERGENCYMagenta

Each color will be verified against [System.ConsoleColor]. If it is invalid, an error will appear on the screen along with the original message.

Colorize text manually

PSLogs allows you to freely colorize individual parts of the message or variables in the format string. The following two tokens can be used to configure parts of the strings with color.

  • {StartColor:<color>}
  • {EndColor}

Note that it will only work with OnlyColorizeLevel=$true because if this is $false the whole line is colored which overrides this setting.

You can use it within the message itself:

Write-Log -Level VERBOSE -Message 'This is a verbose {StartColor:Magenta}message{EndColor}'

FormatColorMessage

You can also use it within a format string:

Add-LoggingTarget -Name Console -Configuration @{
OnlyColorizeLevel = $true
Format = '{StartColor:Green}%{timestamp:+yyyy-MM-dd HH:mm:ss}{EndColor} | %{level:-7} | %{message}'
}
Write-Log -Level VERBOSE -Message 'This is a verbose {StartColor:Magenta}message{EndColor}'

FormatColorMessage

Example

Add-LoggingTarget -Name Console -Configuration @{
Level = 'DEBUG'
Format = '[%{timestamp}] [%{level}] %{message}'
PrintException = $false
ColorMapping = @{
'DEBUG' = 'Blue'
'INFO' = 'Green'
'WARNING' = 'Yellow'
'ERROR' = 'Red'
}
OnlyColorizeLevel = $true
ShortLevel = $true
}

Format examples

Logging Formats