https://github.com/chadnpc/clihelper.logger
Provides a thread-safe in-memory and file-based logging
Science Score: 26.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
○Committers with academic emails
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (11.9%) to scientific vocabulary
Keywords
Keywords from Contributors
Repository
Provides a thread-safe in-memory and file-based logging
Basic Info
- Host: GitHub
- Owner: chadnpc
- License: wtfpl
- Language: PowerShell
- Default Branch: main
- Homepage: https://www.powershellgallery.com/packages/cliHelper.logger
- Size: 3.03 MB
Statistics
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
- Releases: 0
Topics
Metadata Files
README.md
A thread-safe, in-memory and file-based logging module for PowerShell.
Installation
PowerShell
Install-Module cliHelper.logger
Usage demo
Get started:
In an interactive pwsh session
PowerShell Import-Module cliHelper.loggerorIn your script? Add:
PowerShell #Requires -Modules cliHelper.logger, othermodulename...
Then
```PowerShell
1. usage in an object
$demo = [PsCustomObject]@{ PsTypeName = "cliHelper.logger.demo" Description = "Shows how a logger instance is used with cmdlets" Version = [Version]'0.1.2' Logger = New-Logger -Level 1 } $demo.PsObject.Methods.Add([psscriptmethod]::new('SimulateCommand', { Param( [Parameter(Mandatory = $true)] [validateset('Success', 'Failing')] [string]$type )
If($type -eq 'Success') {
$this.Logger.LogInfoLine("Getting username ...")
[Threading.Thread]::Sleep(2000);
$this.Logger.LogInfoLine("Done.")
return [IO.Path]::Join(
[Environment]::UserDomainName,
[Environment]::UserName
)
}
$file = "C:\fake-dir{0}\NonExistentFile.txt" -f (Get-Random -Max 100000000).ToString("D9")
try {
$this.Logger.LogInfoLine("Getting $file ...")
[Threading.Thread]::Sleep(1000);
Get-Item $file -ea Stop
$this.Logger.LogInfoLine("Done!")
} catch {
$this.Logger | Write-LogEntry -l Error -m "Failed to access $([IO.Path]::GetFileName($file))" -e $_.Exception
}
}
) )
2. You can also save logs to json files
$demo.Logger | Add-JsonAppender
$demo.Logger.set_default() # (OPTIONAL) handy only when you are in a pwsh terminal.
Now u don't have to pipe $demo.Logger each time u write or read logs in this session:
try { $logPath = [string][Logger]::Default.logdir Write-LogEntry -Level INFO -Message "app started in directory: $logPath" # same as: $demo.Logger.LogInfoLine("app st4rt3d in d1r3ct0ry: $logPath")
Write-LogEntry -Level Debug -Message "Configuration loaded." # Note: this logline will be skipped! # ie: in this case anything below level 1 (INFO) won't be recorded, since [int]$demo.Logger.MinLevel -eq 1
# Name value # ---- ----- # DEBUG 0 # INFO 1 # WARN 2 # ERROR 3 # FATAL 4 # - that means, only DEBUG lines won't show in logs # - Table from command: [LogLevel[]][Enum]::GetNamesLogLevel | % { [PsCustomObject]@{ Name = $_ ; value = $.value_ } }
# 3. success command $user = $demo.SimulateCommand("Success") Write-LogEntry -Level INFO -Message "Processing request for user: $user"
# 4. Failing command $demo.SimulateCommand("Failing") Write-LogEntry -Level 2 -Message "Operation completed with warnings." Write-LogEntry -Message "Logs saved in $logPath" } finally { Read-LogEntries -Type Json # same as: $demo.Logger.ReadEntries(@{ type = "json" }) # 5. IMPORTANT: Dispose the logger to flush buffers and release file handles # $demo.Logger.Dispose() } ```
Read the docs for In-depth Usage examples.
NOTES:
Remeber to dispose the object
Because appenders (especially file-based ones) hold resources, you must call
$logger.Dispose()when you are finished logging to ensure logs are flushed and files are closed properly.Use a
try...finallyblock to ensure its always called.Failure to call
.Dispose()can lead to:- Log messages not being written to files (still stuck in buffers).
- File locks being held, preventing other processes (or even later runs of your script) from accessing the log files.
License
This project is licensed under the WTFPL License.
Owner
- Name: alain
- Login: chadnpc
- Kind: user
- Website: https://linktr.ee/chadnpc
- Repositories: 82
- Profile: https://github.com/chadnpc
GitHub Events
Total
- Delete event: 1
- Issue comment event: 1
- Push event: 29
- Pull request event: 2
- Create event: 1
- Commit comment event: 1
Last Year
- Delete event: 1
- Issue comment event: 1
- Push event: 29
- Pull request event: 2
- Create event: 1
- Commit comment event: 1
Issues and Pull Requests
Last synced: 9 months ago
All Time
- Total issues: 0
- Total pull requests: 1
- Average time to close issues: N/A
- Average time to close pull requests: 3 minutes
- Total issue authors: 0
- Total pull request authors: 1
- Average comments per issue: 0
- Average comments per pull request: 1.0
- Merged pull requests: 1
- Bot issues: 0
- Bot pull requests: 1
Past Year
- Issues: 0
- Pull requests: 1
- Average time to close issues: N/A
- Average time to close pull requests: 3 minutes
- Issue authors: 0
- Pull request authors: 1
- Average comments per issue: 0
- Average comments per pull request: 1.0
- Merged pull requests: 1
- Bot issues: 0
- Bot pull requests: 1
Top Authors
Issue Authors
Pull Request Authors
- imgbot[bot] (2)
Top Labels
Issue Labels
Pull Request Labels
Dependencies
- anc95/ChatGPT-CodeReview v1.0.12 composite
- Mattraks/delete-workflow-runs v2 composite
- actions/checkout v3 composite
- actions/upload-artifact v3 composite
