Skip to content

Commit

Permalink
Added ability to set a 'Tag' in the 'Set-LogParameters' so that logge…
Browse files Browse the repository at this point in the history
…d messages can be more easily identified
  • Loading branch information
russellseymour committed Mar 10, 2014
1 parent 649424b commit 35809e9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
10 changes: 10 additions & 0 deletions functions/exported/Set-LogParameters.ps1
Expand Up @@ -57,6 +57,11 @@ function Set-LogParameters {
# If not set this item will not appear in the logging object
$custom = $false,

[Parameter(ParameterSetName="switches")]
# Tag
# Tag that should be prepended to any log messahe
$tag,

[Parameter(ParameterSetName="object")]
# Parameters
# This is an object that contains all of the settings that need to be defined in the module
Expand Down Expand Up @@ -116,6 +121,11 @@ function Set-LogParameters {
$Logging.custom = $custom
}

# Add the tag to the sessting
if (!($Logging.ContainsKey("tag")) -and ![String]::IsNullOrEmpty($tag)) {
$Logging.tag = $tag
}

}

"object" {
Expand Down
10 changes: 8 additions & 2 deletions functions/exported/Write-Log.ps1
Expand Up @@ -36,7 +36,7 @@ function Write-Log {
# variable to hold extra information to be added to the message
# this is how default messages can be enchances
$extra = $false,

#region Write-Host Parameters

[Parameter(ParameterSetName="Host")]
Expand Down Expand Up @@ -185,6 +185,12 @@ function Write-Log {
}
$provider_paths += "{0}\providers" -f $module_path

# determine if a tag has been specified in the session
if (![String]::IsNullOrEmpty($script:Logging.tag)) {
$logtag = $script:Logging.tag
} elseif (![String]::IsNullOrEmpty($LogTagPreference)) {
$logtag = $LogTagPreference
}

}

Expand All @@ -194,7 +200,7 @@ function Write-Log {
$resource = Load-Help -Path $resource

# Find the message from the eventid and return as a message object
$msg, $formatting = Get-HelpMessage -EventId $EventId -Resource $resource -Message $Object -ForegroundColor $ForegroundColor -BackgroundColor $BackgroundColor -Prepend $Prepend
$msg, $formatting = Get-HelpMessage -EventId $EventId -Resource $resource -Message $Object -ForegroundColor $ForegroundColor -BackgroundColor $BackgroundColor -Prepend $Prepend -Tag $logtag

# Get the message structure to work with
$structure = Format-Message -Level $level -EventId $EventId -Message $msg -Severity $severity
Expand Down
6 changes: 5 additions & 1 deletion functions/resources/Get-HelpMessage.ps1
Expand Up @@ -42,7 +42,10 @@ function Get-HelpMessage {

# Ensure that the colours from the calling function are pulled in
$ForegroundColor,
$BackgroundColor
$BackgroundColor,

# the log tag that has been supplied
$tag

)

Expand All @@ -57,6 +60,7 @@ function Get-HelpMessage {
foregroundcolor = $ForegroundColor
backgroundcolor = $BackgroundColor
prepend = $prepend
tag = $tag
}

if ($resource -ne $false) {
Expand Down
6 changes: 5 additions & 1 deletion providers/LogFile.ps1
Expand Up @@ -39,7 +39,11 @@ function Set-Message {
# if there are results add the information tot heline
if ($results.count -gt 0 -and ($prependString = $results[0]) -is [System.String])
{
$line = "{0} - {1} - {2} - {3}" -f $prependString, $structure.severity, $structure.eventid, $line
if ([String]::IsNullOrEmpty($formatting.tag)) {
$line = "{0} - {1} - {2} - {3}" -f $prependString, $structure.severity, $structure.eventid, $line
} else {
$line = "{0} - {1} - {2} - {3} - {4}" -f $prependString, $structure.severity, $structure.eventid, $formatting.tag, $line
}
}
}

Expand Down

0 comments on commit 35809e9

Please sign in to comment.