https://github.com/kochava/clog
Simple contextual logger for Go
https://github.com/kochava/clog
golang logger logging tf zap
Last synced: 20 days ago
JSON representation
Simple contextual logger for Go
- Host: GitHub
- URL: https://github.com/kochava/clog
- Owner: Kochava
- License: isc
- Created: 2019-04-25T02:45:19.000Z (about 7 years ago)
- Default Branch: main
- Last Pushed: 2021-08-04T15:31:16.000Z (almost 5 years ago)
- Last Synced: 2025-02-25T04:15:43.524Z (over 1 year ago)
- Topics: golang, logger, logging, tf, zap
- Language: Go
- Homepage:
- Size: 62.5 KB
- Stars: 0
- Watchers: 14
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: COPYING
Awesome Lists containing this project
README
clog
===
[](https://godoc.org/github.com/Kochava/clog)
go get github.com/Kochava/clog
clog is a simple package for initializing a [Zap][] logger and attaching it to
a context, along with functions for logging from the context-attached logger or
associating new fields to the logger.
Generally speaking this is a bad use of the context package, but utility won out
over passing both a context and a logger around all the time. In particular,
this is useful for passing a request-scoped logger through different
http.Handler implementations that otherwise do not support Zap.
[Zap]: https://go.uber.org/zap
Usage
---
A few examples of basic usage follow.
### Initialize a logger
```go
// Create a logger at info level with a production configuration.
level := zap.NewAtomicLevelAt(zap.InfoLevel)
l, err := clog.New(level, false)
if err != nil {
panic(err)
}
l.Info("Ready")
```
### Attach a logger to a context
```go
// var l *zap.Logger
// Attach the logger, l, to a context:
ctx := clog.WithLogger(context.Background(), l)
// Attach fields to the logger:
ctx = clog.With(ctx, zap.Int("field", 1234))
// Log at info level:
clog.Info(ctx, "Log message")
```
License
---
clog is made available under the ISC license. A copy of it can be found in the
repository in the `COPYING` file.
## Default Branch
As of October 1, 2020, github.com uses the branch name ‘main’ when creating the initial default branch for all new repositories. In order to minimize any customizations in our github usage and to support consistent naming conventions, we have made the decision to rename the ‘master’ branch to be called ‘main’ in all Kochava’s github repos.
For local copies of the repo, the following steps will update to the new default branch:
```
git branch -m master main
git fetch origin
git branch -u origin/main main
git remote set-head origin -a
```