Skip to content

geekwhocodes/simple-ps-logger

Repository files navigation

Simple PowerShell Logging Module logo

Build Status Tests Code Quality last commit license


Introduction

SimplePSLogger provides an easy yet powerful way to save or display your logs.

Features

  • Built with πŸ’œ and PowerShell Core
    • Easy to use
    • Cross platform
  • Simple Configuration πŸ‘Œ
  • Pluggable
    • Bring your own logging provider
    • Open source your logging provider to share with your fellow PowerShellers, because sharing is caring
  • Built in Providers πŸ”₯
    • Start logging within a minute
  • Lightning fast ⚑️

Modules

Below is a table contains our modules including built in provider modules.

Description Module Name PowerShell Gallery Link Downloads
SimplePSLogger SimplePSLogger SimplePSLogger Downloads
SimplePSLogger.Console SimplePSLogger.Console Sub module
SimplePSLogger.File SimplePSLogger.File Sub module
SimplePSLogger.AzLogAnalytics SimplePSLogger.AzLogAnalytics Sub module

Installation

PowerShell Gallery

# Install pre-release version 
Install-Module -Name SimplePSLogger -Confirm

Import from Directory

Import-Module -Name drive:path\SimplePSLogger -Verbose

Read more about importing module here Import Module


Usage

Example 1

Create new logger instance using New-SimplePSLogger cmdlet:

<#
    .PARAMETER Name 
    This can be used to identify for which purpose you are using this logger instance.
    example - if you are performing task1
    Simple logger will generate log message like this :
    2020/06/12 15:48:31:2518 PM task1 information Log from task1

    task1 is unique name you used while creating the instance. This will helpful to analyze your logs later. 
#>

New-SimplePSLogger -Name "MyLogger"

# information log
Write-SimpleLog "Log message" "information"

# default log level
Write-SimpleLog "message" # In this case, SimplePSLogger will automatically use default(information) loglevel

$Object = @{User = @{Name= "geekwhocodes", LastLogin = "2020/06/12 15:48:31:2518 PM" } }
# Log PowerShell object, SimplePSLogger will automatically serialize this object
Write-SimpleLog $Object "warning"

# Note - DON'T forget to flush and remove logger instance/instances

# Flush bufferred logs 
Clear-Buffer -Name "MyLogger"
# Remove all logger instances
Remove-SimplePSLogger -Name "MyLogger"
Example 2

Create new logger instance using New-SimplePSLogger cmdlet:
Read more about configuration

<#
    .PARAMETER Name 
    This can be used to identify for which purpose you are using this logger instance.
    example - if you are performing task1
    Simple logger will generate log message like this :
    2020/06/12 15:48:31:2518 PM task1 information Log from task1
    task1 is unique name you used while creating the instance. This will helpful to analyze your logs later. 

    .PARAMETER Configuration
    Configuration to use for logger instance.
#>

$SimplePSLoggerConfig = @{
    Name      = "az-analytics-example-buffered"
    Providers = @{
        File           = @{
            LiteralFilePath = "G:\Git\simple-ps-logger\ExamplesV2\example-with-config.log"
            LogLevel        = "information"
            Enabled         = $true
        }
        AzLogAnalytics = @{
            Enabled    = $true
            LogLevel   = "verbose"
            #WorkspaceId  = "****************" # Fetch from your secure store. example - KeyVault
            #WorkspaceKey = "****************" # Fetch from your secure store. example - KeyVault
            LogType    = "GWCPSLogger" # https://docs.microsoft.com/en-us/azure/azure-monitor/platform/data-collector-api#record-type-and-properties
            BufferSize = 50
            Flush      = $true
        }
    }
}

New-SimplePSLogger -Name "MyLogger" -Configuration $SimplePSLoggerConfig

# information log
Write-SimpleLog "Log message" "information"

# default log level
Write-SimpleLog "message" # In this case, SimplePSLogger will automatically use default(information) loglevel

$Object = @{User = @{Name= "geekwhocodes", LastLogin = "2020/06/12 15:48:31:2518 PM" } }
# Log PowerShell object, SimplePSLogger will automatically serialize this object
Write-SimpleLog $Object "warning"

# Note - DON'T forget to flush and remove logger instance/instances

# Flush bufferred logs 
Clear-Buffer -Name "MyLogger"
# Remove all logger instances
Remove-SimplePSLogger -Name "MyLogger"

Built in Providers

Provider Description Docs
Console Outputs logs to console Console Provider
File Writes logs to static file File Provider
AzLogAnalytics Send logs to Azure Log Analytics Workspace AzLogAnalytics Provider
Rolling File Writes logs to file To do

Supported Log Levels

Level Description
verbose for verbose messages
debug for debug messages
information for information messages
warning for warning messages
error for error messages
critical for critical messages

Reporting Issues and Feedback

Issues

If you find any bugs when using this module, Please an issue on github

Feedback

If there is a feature you would like to see in SimplePSLogger file an issue on github

Show your love

Give a ⭐ if you find this project helpful!