Minimalistic logging library for Go.

logger

Minimalistic logging library for Go. Blog Post

Features:

Install

$ go get github.com/azer/logger

Getting Started

Create an instance with a preferred name;

import "github.com/azer/logger"

var log = logger.New("example-app")

Every logger has three methods: Info, Timer and Error.

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);

$ 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 for a more detailed example.

Filters

You can enable all logs by specifying *:

$ LOG=* go run example-app.go

Or, choose specific packages that you want to see logs from:

$ 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?

$ LOG=images@timer,users go run example-app.go

Another example; show error logs from all packages, but hide logs from database package:

$ LOG=*@error,database@mute go run example-app.go

Timers

You can use timer logs for measuring your program. For example;

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;

log.Info("Sending an e-mail...", logger.Attrs{
  "from": "[email protected]",
  "to": "[email protected]",
})

The above log will appear in the structured output as:

{ "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:

Programmatical Usage

Customizing the default behavior is easy. You can implement your own output;

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: Stream logs into a Slack channel.
Owner
Azer Koçulu
Chief Technology Officer at @hipeople (we're hiring!)
Azer Koçulu
Comments
  • Added setters for log enablement, all-log enablement, and log level.

    Added setters for log enablement, all-log enablement, and log level.

    To make this make sense, also had to add code that checks whether logs are enabled at call time rather than checking the value of IsEnabled which is set at instantiation time. The value of IsEnabled is updated whenever an internal method that uses it checks for enablement.

    Added a little bit of documentation to existing and new functions.

  • Regression in latest version (71c896b)

    Regression in latest version (71c896b)

    Running the example code:

    package main
    
    import (
        "github.com/azer/logger"
        "time"
    )
    
    var log = logger.New("app")
    
    func main() {
        log.Info("Starting at %d", 9088)
    
        log.Info("Requesting an image at foo/bar.jpg")
        timer := log.Timer()
        time.Sleep(time.Millisecond * 250)
        timer.End("Fetched foo/bar.jpg")
    
        log.Error("Failed to start, shutting down...")
    }
    

    with version 71c896b yields the following error:

    panic: assignment to entry in nil map
    
    goroutine 1 [running]:
    panic(0xc58e0, 0xc820064050)
        /usr/local/go/src/runtime/panic.go:464 +0x3e6
    /vendor/github.com/azer/logger.New(0x10baf8, 0x3, 0xc820050000)
        /vendor/github.com/azer/logger/logger.go:26 +0xaa
    main.init()
        /example.go:8 +0x65
    exit status 2
    

    Reverting to d8432cee resolves the issue:

    11:39:22.887 app: Starting at 9088
    11:39:22.887 app: Requesting an image at foo/bar.jpg
    11:39:23.142 app(254.627113ms): Fetched foo/bar.jpg
    11:39:23.142 app(!): Failed to start, shutting down...
    
  • Public access to `enabled` map

    Public access to `enabled` map

    Hey, this logger looks great- a good blend of simple API, levelling/app-segregation, and nice design.

    One feature I'd really like, though, is the ability to programmatically enable logs and log levels, rather than relying on the environment variable method. If I'm already defining a CLI interface for my application I'd prefer to add a "-v" / "-vv" flag to enable various log levels or log types.

    It looks like this would be easy to do if the "enabled" map were exposed publicly. Alternatively, if a setter function were added to complement IsEnabled, so you could call a function with a logger name and a state to enable. Something like SetEnabled(logname string, setstate bool) error.

    I can write it up and PR, if you like?

  • utilize sw.Target rather than hardcoded os.Stderr

    utilize sw.Target rather than hardcoded os.Stderr

    StandardWriter.Target is set[1] but not used and instead it's currently hard coded to os.Stderr rather than using the os.File that was passed in through NewStandardOutput(file *os.File)

    [1] https://github.com/azer/logger/blob/8b7d1ee/standard-output.go#L16

  • Fix isTerminal call on Windows

    Fix isTerminal call on Windows

    On Windows syscall.Stderr is of type syscall.Handle[1] rather than int[2] causing a compilation error.

    github.com/azer/logger/standard-output.go:15:39: cannot use syscall.Stderr (type syscall.Handle) as type int in argument to isterminal.IsTerminal

    A simple cast to int along the lines of isterminal.IsTerminal(int(syscall.Stderr)) would suffice although I believe it's more appropriate to check the actual output destination being passed in rather than Stderr and instead use isterminal.IsTerminal(int(file.Fd()))

    [1] https://golang.org/pkg/syscall/?GOOS=windows#pkg-variables [2] https://golang.org/pkg/syscall/?GOOS=linux#pkg-variables

  • Treat -thing as thing@mute

    Treat -thing as thing@mute

    As a user of visionmedia/debug, I had expected that I could negate using "-", rather than use "mute". While mute isn't much extra effort, I keep forgetting, falling back into old habits.

Gomol is a library for structured, multiple-output logging for Go with extensible logging outputs

gomol Gomol (Go Multi-Output Logger) is an MIT-licensed structured logging library for Go. Gomol grew from a desire to have a structured logging libra

Sep 26, 2022
A simple logging module for go, with a rotating file feature and console logging.

A simple logging module for go, with a rotating file feature and console logging. Installation go get github.com/jbrodriguez/mlog Usage Sample usage W

Dec 14, 2022
FactorLog is a logging infrastructure for Go that provides numerous logging functions for whatever your style may be
FactorLog is a logging infrastructure for Go that provides numerous logging functions for whatever your style may be

FactorLog FactorLog is a fast logging infrastructure for Go that provides numerous logging functions for whatever your style may be. It could easily b

Aug 3, 2022
Package logging implements a logging infrastructure for Go
Package logging implements a logging infrastructure for Go

Golang logging library Package logging implements a logging infrastructure for Go. Its output format is customizable and supports different logging ba

Nov 10, 2021
Pragmatic and minimalistic module for collecting and sending traces from Go code 💪🏽
Pragmatic and minimalistic module for collecting and sending traces from Go code 💪🏽

tracing-go Pragmatic and minimalistic module for collecting and exporting trace data from the Go code. prometheus/client_golang but for Traces NOTE: T

Jan 6, 2023
a lightweight, high-performance, out-of-the-box logging library that relies solely on the Go standard library

English | 中文 olog olog is a lightweight, high-performance, out-of-the-box logging library that relies solely on the Go standard library. Support outpu

Apr 12, 2023
Simple and blazing fast lockfree logging library for golang
Simple and blazing fast lockfree logging library for golang

glg is simple golang logging library Requirement Go 1.11 Installation go get github.com/kpango/glg Example package main import ( "net/http" "time"

Nov 28, 2022
Logging library for Golang

GLO Logging library for Golang Inspired by Monolog for PHP, severity levels are identical Install go get github.com/lajosbencz/glo Severity levels Deb

Sep 26, 2022
The Simplest and worst logging library ever written

gologger A Simple Easy to use go logger library. Displays Colored log into console in any unix or windows platform. You can even store your logs in fi

Sep 26, 2022
Seelog is a native Go logging library that provides flexible asynchronous dispatching, filtering, and formatting.

Seelog Seelog is a powerful and easy-to-learn logging framework that provides functionality for flexible dispatching, filtering, and formatting log me

Jan 3, 2023
A pure Go contextual logging library with "batteries included"

Cue Overview Cue implements contextual logging with "batteries included". It has thorough test coverage and supports logging to stdout/stderr, file, s

Sep 16, 2019
Golang logging library
Golang logging library

Golang logging library Package logging implements a logging infrastructure for Go. Its output format is customizable and supports different logging ba

Dec 27, 2022
Hierarchical, leveled, and structured logging library for Go

spacelog Please see http://godoc.org/github.com/spacemonkeygo/spacelog for info License Copyright (C) 2014 Space Monkey, Inc. Licensed under the Apach

Apr 27, 2021
Minimal structured logging library for Go
Minimal structured logging library for Go

slog slog is a minimal structured logging library for Go. Install go get cdr.dev/slog Features Minimal API First class context.Context support First c

Dec 29, 2022
GoVector is a vector clock logging library written in Go.
GoVector is a vector clock logging library written in Go.

GoVector is a vector clock logging library written in Go. The vector clock algorithm is used to order events in distributed systems in the absence of a centralized clock. GoVector implements the vector clock algorithm and provides feature-rich logging and encoding infrastructure.

Nov 28, 2022
Parametrized JSON logging library in Golang which lets you obfuscate sensitive data and marshal any kind of content.
Parametrized JSON logging library in Golang which lets you obfuscate sensitive data and marshal any kind of content.

Noodlog Summary Noodlog is a Golang JSON parametrized and highly configurable logging library. It allows you to: print go structs as JSON messages; pr

Oct 27, 2022
Cloud logging library in golang

?? logg Open Source Cloud logging library in Go. About the project Connect your golang microservices logs with this engine! Send your logs to kafka, r

Nov 8, 2021
Logging library for Leadjet backend services

Logger Logging library for Leadjet backend services. Install go get -u github.com/Leadjet/logger Usage Initiate a Zap logger; err := zap.RegisterLog(

Feb 21, 2022
📝 🪵 A minimal level based logging library for Go

slogx A minimal level based logging library for Go. Installation Example Usage Logger Log Level Format Output Contribute License Installation go get g

May 23, 2022