FreeLog.psm1
|
<# .SYNOPSIS A simple and robust logging module that works cross-platform in Powershell. .DESCRIPTION FreeLog provides a robust logging system with multiple severity levels, automatic log file management, and pipeline-friendly functions. The module automatically loads all functions from the Private and Public folders, making them available for use. Public functions are exported for module users, while Private functions remain internal to the module. Key Features: - Four severity levels: LOG, WARN, ERROR, FAIL - Automatic log file creation with initialization (CREATED) entry - Consistent timestamp formatting (yyyy-MM-dd HH:mm:ss) - Real-time verbose output support - Pipeline integration for flexible logging - Built-in error handling and validation .PARAMETER None This module does not accept parameters at load time. All configuration is done through the exported functions after the module is imported. .INPUTS None. The module itself does not accept pipeline input during import. .OUTPUTS None. The module does not produce output during import. .EXAMPLE # Import the module Import-Module FreeLog # Basic logging workflow New-LogFile -Path "C:\Logs\application.log" Write-LogFile -TaskMessage "Application started" Write-LogFile -TaskWarn "High memory usage detected" Write-LogFile -TaskError "Failed to connect to database" .NOTES Module Name : FreeLog Author : Michael Free Created Date : 9/17/2024 Dependencies: - PowerShell 5.0 or later (for class support) - Write permissions to target log directories File Handling: - Creates log files automatically if they don't exist - Automatically creates missing directories in the path - Log format: [LEVEL] - [TIMESTAMP] - [MESSAGE] Severity Levels (in order of increasing severity): - LOG : Standard information messages - WARN : Warning conditions that don't prevent operation - ERROR : Error conditions that may affect functionality - FAIL : Critical failures requiring immediate attention - CREATED : Automatically generated when log file is created #> foreach ($folder in @('Private', 'Public')) { $root = Join-Path -Path $PSScriptRoot -ChildPath $folder if (Test-Path -Path $root) { Write-Verbose "processing folder $root" $files = Get-ChildItem -Path $root -Filter '*.ps1' $files | Where-Object { $_.Name -notlike '*.Tests.ps1' } | ForEach-Object { Write-Verbose "Dot-sourcing $($_.Name)" . $_.FullName } } } $exportedFunctions = (Get-ChildItem -Path (Join-Path $PSScriptRoot 'Public') -Filter '*.ps1').BaseName Export-ModuleMember -Function $exportedFunctions foreach ($folder in @('Private', 'Public')) { $root = Join-Path -Path $PSScriptRoot -ChildPath $folder if (Test-Path -Path $root) { Write-Verbose "processing folder $root" $files = Get-ChildItem -Path $root -Filter '*.ps1' $files | Where-Object { $_.Name -notlike '*.Tests.ps1' } | ForEach-Object { Write-Verbose "Dot-sourcing $($_.Name)" . $_.FullName } } } $exportedFunctions = (Get-ChildItem -Path (Join-Path $PSScriptRoot 'Public') -Filter '*.ps1').BaseName Export-ModuleMember -Function $exportedFunctions |