https://github.com/azazeal/slogger
Supporting functionality related to the log/slog package.
https://github.com/azazeal/slogger
golang logging
Last synced: 6 months ago
JSON representation
Supporting functionality related to the log/slog package.
- Host: GitHub
- URL: https://github.com/azazeal/slogger
- Owner: azazeal
- License: mit
- Created: 2023-03-25T16:06:03.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2025-02-13T17:08:24.000Z (over 1 year ago)
- Last Synced: 2025-02-13T18:23:50.043Z (over 1 year ago)
- Topics: golang, logging
- Language: Go
- Homepage:
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/azazeal/slogger/actions/workflows/build.yml)
[](https://coveralls.io/github/azazeal/slogger?branch=master)
[](https://pkg.go.dev/github.com/azazeal/slogger)
# slogger
Package `slogger` implements supporting functionality related to the
[`log/slog`](https://pkg.go.dev/log/slog) package.
For more details, you may review the package documentation [here](https://pkg.go.dev/github.com/azazeal/slogger).
## Usage
```go
package main
import (
"context"
"github.com/azazeal/slogger"
)
func main() {
// grab a reference to a slog.Logger configured by the $LOG_LEVEL & $LOG_FORMAT environment
// variables.
logger := slogger.FromEnv()
// store that reference to a top-level Context that propagates throughout the program
ctx := slogger.NewContext(context.Background(), logger)
// pass the Context, and therefore the slog.Logger, around; log as needed
doSomething(ctx)
doSomethingElse(ctx)
}
func doSomething(ctx context.Context) {
logger := slogger.FromContext(ctx)
logger.Warn("did something")
}
func doSomethingElse(ctx context.Context) {
logger := slogger.FromContext(ctx)
logger.Info("did something else")
}
```