https://github.com/chadnpc/clihelper.logger

Provides a thread-safe in-memory and file-based logging

https://github.com/chadnpc/clihelper.logger

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

cli clihelper diagnostics logging powershell

Keywords from Contributors

hack bruteforce
Last synced: 5 months ago · JSON representation

Repository

Provides a thread-safe in-memory and file-based logging

Basic Info
Statistics
  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Topics
cli clihelper diagnostics logging powershell
Created 11 months ago · Last pushed 10 months ago
Metadata Files
Readme Changelog License

README.md

cliHelper.logger

A thread-safe, in-memory and file-based logging module for PowerShell.

Downloads

Installation

PowerShell Install-Module cliHelper.logger

Usage demo

Get started:

  1. In an interactive pwsh session

    PowerShell Import-Module cliHelper.logger or

  2. In 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:

  1. 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...finally block 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

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

Committers

Last synced: 9 months ago

All Time
  • Total Commits: 150
  • Total Committers: 2
  • Avg Commits per committer: 75.0
  • Development Distribution Score (DDS): 0.007
Past Year
  • Commits: 150
  • Committers: 2
  • Avg Commits per committer: 75.0
  • Development Distribution Score (DDS): 0.007
Top Committers
Name Email Commits
alain 7****c 149
ImgBotApp I****p@g****m 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

.github/workflows/build_module.yaml actions
.github/workflows/codereview.yaml actions
  • anc95/ChatGPT-CodeReview v1.0.12 composite
.github/workflows/delete_old_workflow_runs.yaml actions
  • Mattraks/delete-workflow-runs v2 composite
.github/workflows/publish.yaml actions
  • actions/checkout v3 composite
  • actions/upload-artifact v3 composite