Public/New-LogFile.ps1
|
<# .SYNOPSIS Creates and initializes a new global log file instance. .DESCRIPTION The New-LogFile function creates a new FreeLogClass instance and stores it in the script-scoped $logger variable for global access throughout your script or module. It automatically creates the physical log file if it doesn't exist. This function is the required initialization step before using Write-LogFile. .PARAMETER Path The full file system path where the log file should be created or accessed. This parameter is mandatory and must be a valid file path. The function will create any missing directories in the path structure automatically. .INPUTS None. This function does not accept pipeline input. .OUTPUTS None. The function creates a script-scoped logger variable but does not return any output. .EXAMPLE # Basic log file creation New-LogFile -Path "C:\Logs\application.log" Creates a new log file instance at the specified path and initializes the global logger. .EXAMPLE New-LogFile -Path "C:\Logs\app.log" Write-LogFile -TaskMessage "Application started" Write-LogFile -TaskWarn "Configuration file not found, using defaults" .NOTES Function Name : New-LogFile Requires : PowerShell 5.0 or later (for class support) Dependencies : FreeLogClass Scope : Creates/updates $script:logger variable Module : FreeLog #> function New-LogFile { [CmdletBinding(SupportsShouldProcess = $true)] param( [Parameter(Mandatory = $true)] [string]$Path ) if (-not [string]::IsNullOrEmpty($Path)) { # Add try/catch for failure to create file if ($PSCmdlet.ShouldProcess($Path, 'Creating log file')) { $script:logger = [FreeLogClass]::new($Path) if (-not (Test-Path -Path $Path)) { New-Item -Path $Path -ItemType File -Force } } } else { throw 'Path cannot be null or empty.' } } |