Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/azer/logger
Minimalistic logging library for Go.
https://github.com/azer/logger
Last synced: 3 days ago
JSON representation
Minimalistic logging library for Go.
- Host: GitHub
- URL: https://github.com/azer/logger
- Owner: azer
- License: wtfpl
- Created: 2014-09-30T06:45:09.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2021-11-22T15:36:32.000Z (about 3 years ago)
- Last Synced: 2024-05-21T04:14:53.987Z (7 months ago)
- Language: Go
- Homepage: http://godoc.org/github.com/azer/logger
- Size: 49.8 KB
- Stars: 158
- Watchers: 7
- Forks: 16
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - logger - Minimalistic logging library for Go. (Logging / Search and Analytic Databases)
- awesome-go - logger - Minimalistic logging library for Go. - ★ 129 (Logging)
- awesome-go-extra - logger - 09-30T06:45:09Z|2021-11-22T15:36:32Z| (Logging / Advanced Console UIs)
- awesome-go-zh - logger
README
## logger
Minimalistic logging library for Go.
**Features:*** [Advanced output filters (package and/or level)](#filters)
* [Attributes](#attributes)
* [Timers for measuring performance](#timers)
* [Structured JSON output](#structured-output)
* [Programmatical Usage](#programmatical-usage)
* [Hooks](#hooks)
* Being used in production since 2014.![](https://cldup.com/aVZBXEhcs2.png)
## Install
```bash
$ go get github.com/azer/logger
```## Getting Started
Create an instance with a preferred name;
```go
import "github.com/azer/logger"var log = logger.New("example-app")
```Every logger has three methods: `Info`, `Timer` and `Error`.
```go
log.Info("Running at %d", 8080)err := DoSomething()
if err != nil {
log.Error("Failed: %s", err.Error())
}
```Done. Now run your app, passing `LOG=*` environment variable. If you don't pass `LOG=*`, ([logging will be silent by default](http://www.linfo.org/rule_of_silence.html));
```
$ LOG=* go run example-app.go
01:23:21.251 example-app Running at 8080
```You can filter logs by level, too. The hierarchy is; `mute`, `info`, `timer` and `error`.
After the package selector, you can optionally specify minimum log level:```
$ LOG=*@timer go run example-app.go
01:23:21.251 example-app Running at 8080
```The above example will only show `timer` and `error` levels. If you choose `error`, it'll show only error logs.
Check out [examples](https://github.com/azer/logger/tree/master/examples) for a more detailed example.
## Filters
You can enable all logs by specifying `*`:
```bash
$ LOG=* go run example-app.go
```Or, choose specific packages that you want to see logs from:
```bash
$ LOG=images,users go run example-app.go
```In the above example, you'll only see logs from `images` and `users` packages. What if we want to see only `timer` and `error` level logs?
```bash
$ LOG=images@timer,users go run example-app.go
```Another example; show error logs from all packages, but hide logs from `database` package:
```bash
$ LOG=*@error,database@mute go run example-app.go
```## Timers
You can use timer logs for measuring your program. For example;
```go
timer := log.Timer()image, err := PullImage("http://foo.com/bar.jpg")
timer.End("Fetched foo.com/bar.jpg")
```Timer log lines will be outputting the elapsed time in time.Duration in a normal terminal, or in int64 format when your program is running on a non-terminal environment.
See below documentation for more info.## Structured Output
When your app isn't running on a terminal, it'll change the output in JSON:
```
{ "time":"2014-10-04 11:44:22.418595705 -0700 PDT", "package":"database", "level":"INFO", "msg":"Connecting to mysql://azer@localhost:9900/foobar" }
{ "time":"2014-10-04 11:44:22.418600851 -0700 PDT", "package":"images", "level":"INFO", "msg":"Requesting an image at foo/bar.jpg" }
{ "time":"2014-10-04 11:44:22.668645527 -0700 PDT", "package":"images", "level":"TIMER", "elapsed":"250032416", "msg":"Fetched foo/bar.jpg" }
{ "time":"2014-10-04 11:44:22.668665527 -0700 PDT", "package":"database", "level":"ERROR", "msg":"Fatal connection error." }
```So you can parse & process the output easily. Here is a command that lets you see the JSON output in your terminal;
```
LOG=* go run examples/simple.go 2>&1 | less
```## Attributes
To add custom attributes to the structured output;
```go
log.Info("Sending an e-mail...", logger.Attrs{
"from": "[email protected]",
"to": "[email protected]",
})
```The above log will appear in the structured output as:
```go
{ "time":"2014-10-04 11:44:22.919726985 -0700 PDT", "package":"mail", "level":"INFO", "msg":"Sending an e-mail", "from": "[email protected]", "to": "[email protected]" }
```In your command-line as:
![](https://cldup.com/FEzVDkEexs.png)
## Programmatical Usage
Customizing the default behavior is easy. You can implement your own output;
```go
import (
"github.com/azer/logger"
)type CustomWriter struct {}
func (cw CustomWriter) Write (log *logger.Log) {
fmt.Println("custom log -> ", log.Package, log.Level, log.Message, log.Attrs)
}func main () {
logger.Hook(&CustomWriter{})
}
```See `examples/programmatical.go` for a working version of this example.
## Hooks
* [Slack](https://github.com/azer/logger-slack-hook): Stream logs into a Slack channel.