Minimalistic logging library for Go.

Overview

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?

$ [email protected],users go run example-app.go

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

$ LOG=*@error,[email protected] 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://[email protected]: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.
Issues
  • 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.

    opened by cathalgarvey 9
  • 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...
    
    opened by clocklear 2
  • 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?

    opened by cathalgarvey 2
  • 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

    opened by bitspill 1
  • 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

    opened by bitspill 1
  • Treat -thing as thing@mute

    Treat -thing as [email protected]

    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.

    opened by Brayyy 1
Owner
Azer Koçulu
Chief Technology Officer at @hipeople (we're hiring!)
Azer Koçulu
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

Kristin Davidson 18 Dec 20, 2021
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

Juan B. Rodriguez 25 Jun 11, 2022
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

Kevin Darlington 54 Oct 12, 2021
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

Luke Zhang 0 Nov 10, 2021
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

Bartlomiej Plotka 9 May 20, 2022
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"

Yusuke Kato 161 Jul 2, 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

Lajos Bencz 14 Aug 26, 2021
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

Sadlil Rhythom 41 Jun 22, 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

Seelog project repository 1.6k Jun 30, 2022
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

Bob Ziuchkovski 26 Sep 16, 2019
Golang logging library

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

Örjan Fors 1.8k Jun 23, 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

Space Monkey Go 98 Apr 27, 2021
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

Coder 242 Jul 4, 2022
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.

Distributed clocks 163 Jun 18, 2022
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

Gyoza Tech 37 Apr 19, 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

yuri 3 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(

Leadjet 1 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

私はレオンです 7 May 23, 2022
Logging, distilled

What is distillog? distillog aims to offer a minimalistic logging interface that also supports log levels. It takes the stdlib API and only slightly e

Akshay Moghe 26 Mar 10, 2022