https://github.com/beatlabs/ctxlog
ctxlog is a library which helps to add a contextual data to your log messages at any time, and have it logged with each message.
https://github.com/beatlabs/ctxlog
context go golang log logging
Last synced: 10 months ago
JSON representation
ctxlog is a library which helps to add a contextual data to your log messages at any time, and have it logged with each message.
- Host: GitHub
- URL: https://github.com/beatlabs/ctxlog
- Owner: beatlabs
- License: apache-2.0
- Created: 2022-11-07T15:17:48.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-11-08T09:51:39.000Z (over 3 years ago)
- Last Synced: 2024-06-20T16:36:22.781Z (about 2 years ago)
- Topics: context, go, golang, log, logging
- Language: Go
- Homepage:
- Size: 1.91 MB
- Stars: 6
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Context Logger 
`ctxlog` is a library which helps to add a contextual data to your log messages at any time, and have it logged with each message.
## Short-term plans
_TBD but be aware it might happen_
- Remove dependency on Patron and replace it with some generic logger
- Add examples in `examples/` folder
## Install it
To add this library as a dependency of your project, run
```shell
$ go get github.com/beatlabs/ctxlog
```
## Usage examples
Initiate a logger in a request context
```go
req := &http.Request{}
ctx := ctxlog.AddLoggerForRequest(req)
```
Add some custom data to it
```go
ctxlog.FromContext(ctx).Int("answer", 42)
ctxlog.FromContext(ctx).Str("so", "long")
ctxlog.FromContext(ctx).SubCtx(map[string]interface{}{
"thanks_for": "all the fish",
"planet": "Earth",
"happened_on": "Thursday",
})
```
And log it
```go
ctxlog.FromContext(ctx).Warnf("this %s has — or rather had — a %s, which was this: %s", "planet", "problem",
"most of the people living on it were unhappy for pretty much all of the time")
```