Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/andrewslotin/llog
A simple Go stdlib-compatible logger providing log message levels
https://github.com/andrewslotin/llog
Last synced: 28 days ago
JSON representation
A simple Go stdlib-compatible logger providing log message levels
- Host: GitHub
- URL: https://github.com/andrewslotin/llog
- Owner: andrewslotin
- License: mit
- Created: 2022-04-19T09:11:11.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-04-19T09:59:31.000Z (over 2 years ago)
- Last Synced: 2024-06-20T12:04:48.530Z (5 months ago)
- Language: Go
- Size: 5.86 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Simple Leveled Logger
=====================[![Documentation](https://pkg.go.dev/badge/github.com/andrewslotin/llog)][godoc]
[![Go Report Card](https://goreportcard.com/badge/github.com/andrewslotin/llog)](https://goreportcard.com/report/github.com/andrewslotin/llog)`github.com/andrewslotin/llog` is a library that provides leveled logs to the Go standard library's `log.Logger`.
Installation
------------```bash
go get github.com/andrewslotin/llog
```Usage
-----The leveled logger is implemented as an `io.Writer` to be provided to the stdlib's `log.SetOutput()`. The writer determines log record level by looking at the message prefix. For example, a message that starts with `warn` will be considered as a `llog.WarnLevel` and sent to the underlying writer, if the writer has the minimal level set to `llog.WarnLevel` or lower.
```
// configure standard logger to write warning messages and above to STDERR
log.SetOutput(llog.NewWriter(os.Stderr, llog.WarnLevel))log.Printf("warn: failed to close the output file: %s", err) // warn level, will be printed out
log.Println("something just happened") // info level, will be ignored
```If a message does not contain any known prefixes, its level is considered to be `llog.InfoLevel`.
Please consult [the package documentation][godoc] for the list of available log levels as well as how they are mapped to the message prefixes.
Performance
-----------There is nearly no overhead in using the `llog.Writer` to write log messages.
Below is the `benchstat` output comparing the benchmarks of `log.Println()` with `io.Discard` and `llog.Writer` set as outputs. The `llog.Writer` is configured to write to `io.Discard` as well with the minimal log level sufficient to print out all messages used in the benchmark.
```
name old time/op new time/op delta
Writer_Write-4 306ns ± 0% 362ns ± 0% ~ (p=1.000 n=1+1)name old alloc/op new alloc/op delta
Writer_Write-4 16.0B ± 0% 16.0B ± 0% ~ (all equal)name old allocs/op new allocs/op delta
Writer_Write-4 1.00 ± 0% 1.00 ± 0% ~ (all equal)
```[godoc]: https://pkg.go.dev/github.com/andrewslotin/llog