Golang logging library

Overview

Golang logging library

godoc build

Package logging implements a logging infrastructure for Go. Its output format is customizable and supports different logging backends like syslog, file and memory. Multiple backends can be utilized with different log levels per backend and logger.

NOTE: backwards compatibility promise have been dropped for master. Please vendor this package or use gopkg.in/op/go-logging.v1 for previous version. See changelog for details.

Example

Let's have a look at an example which demonstrates most of the features found in this library.

Example Output

package main

import (
	"os"

	"github.com/op/go-logging"
)

var log = logging.MustGetLogger("example")

// Example format string. Everything except the message has a custom color
// which is dependent on the log level. Many fields have a custom output
// formatting too, eg. the time returns the hour down to the milli second.
var format = logging.MustStringFormatter(
	`%{color}%{time:15:04:05.000} %{shortfunc} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}`,
)

// Password is just an example type implementing the Redactor interface. Any
// time this is logged, the Redacted() function will be called.
type Password string

func (p Password) Redacted() interface{} {
	return logging.Redact(string(p))
}

func main() {
	// For demo purposes, create two backend for os.Stderr.
	backend1 := logging.NewLogBackend(os.Stderr, "", 0)
	backend2 := logging.NewLogBackend(os.Stderr, "", 0)

	// For messages written to backend2 we want to add some additional
	// information to the output, including the used log level and the name of
	// the function.
	backend2Formatter := logging.NewBackendFormatter(backend2, format)

	// Only errors and more severe messages should be sent to backend1
	backend1Leveled := logging.AddModuleLevel(backend1)
	backend1Leveled.SetLevel(logging.ERROR, "")

	// Set the backends to be used.
	logging.SetBackend(backend1Leveled, backend2Formatter)

	log.Debugf("debug %s", Password("secret"))
	log.Info("info")
	log.Notice("notice")
	log.Warning("warning")
	log.Error("err")
	log.Critical("crit")
}

Installing

Using go get

$ go get github.com/op/go-logging

After this command go-logging is ready to use. Its source will be in:

$GOPATH/src/pkg/github.com/op/go-logging

You can use go get -u to update the package.

Documentation

For docs, see http://godoc.org/github.com/op/go-logging or run:

$ godoc github.com/op/go-logging

Additional resources

  • wslog -- exposes log messages through a WebSocket.
Issues
  • Formatting versions

    Formatting versions

    @op not really sure what the right interface is -- you know best. But it strikes me that since the logging functions take formatting, maybe they should follow stdlib practice of having both Name(string) and Namef(string, ...interface{}) variants.

    Random reason to lean towards the extra f: lint errors like this:

    opened by jbenet 11
  • Add Errorf, Warningf, Noticef, Infof, Debugf functions.

    Add Errorf, Warningf, Noticef, Infof, Debugf functions.

    Added Errorf, Warningf, Noticef, Infof, Debugf to follow the tradition of functions taking formatters named with an f suffix. go vet is enforcing these for a good reason.

    Please add these to the mainline codebase!

    opened by babo 11
  • Use out of the box

    Use out of the box

    Is there a way to use this 100% out of the box with default settings attached:

    i.e. var log = logging.MustGetLogger("example") log.Info("CustomeR: %+v", customer)

    and BANG it just works?

    opened by pjebs 7
  • Add per-level formatter

    Add per-level formatter

    Currently Formatter is usable only in backend logger. In some cases (e.g. for debug messages) is preferable to apply a different Formatter without touch the backend. So, in this case, it would be useful to apply the Formatter in level functions (e.g. log.Debug("message", formatter) )

    opened by henryx 6
  • Color property has no effect on log output

    Color property has no effect on log output

    NewLogBackend.Color property has no effect when custom format with colors are supplied to SetFormatter. So having one backend to write to a file and colors in console in other is a no go. :(

        log_format := "%{color}[%{level:.4s}] %{time:15:04:05.000000} %{id:03x} %{shortfile} [%{longpkg}] %{longfunc} -> %{color:reset}%{message}"
        logging.SetFormatter(logging.MustStringFormatter(log_format))
    

    If I am reading this correctly Record.Formatted and stringFormatter Format have no information about color from backend. If they would, it would be trivial to add really cool functionality like per backend formats and log details.

    opened by guycalledseven 6
  • don't include memory backend on appengine

    don't include memory backend on appengine

    The google appengine doesn't allow import of the unsafe package and therefor the memory backend should be excluded from that target platform.

    This should fix the following error when trying to deploy code to the appengine that imports go-logging:

    go-app-builder: Failed parsing input: parser: bad import "unsafe" in github.com/op/go-logging/memory.go
    
    opened by pjvds 5
  • Implement backend for the golang default logger

    Implement backend for the golang default logger

    I needed the ability to reopen the log file used by the logging subsystem without losing any logs.

    It seems that the default golang logger allows us via the SetOutput exposed function. However, the exposed Logger structure does not have this as a method and there is no way I can see to change the output for an existing Logger. Thus, NewLogBackend is not able to switch the output for the purposes of reopening the log file.

    This PR implements NewDefaultLogBackend, which rather than creating a Logger structure, simply defers to the default golang logger, and allows the SetOutput exposed function to be used to reopen the log file. I've simplified that process by providing a Reopen and a Close method.

    I welcome your thoughts on usefulness of this, improvements, test requirements etc, and will also in due time propose to Golang to have their stdlib extended so that SetOutput is also a method on a logger. Though that won't be before 1.5/1.6 for sure and at least the method in this PR is compatible much further back.

    Jason

    enhancement 
    opened by driskell 5
  • Add

    Add "logging" alias to the example, to make it work.

    Without it, i am getting : ./example.go:5: undefined: logging ./example.go:10: undefined: logging ./example.go:19: undefined: logging ./example.go:24: undefined: logging ./example.go:25: undefined: logging ./example.go:30: undefined: logging ./example.go:33: undefined: logging ./example.go:34: undefined: logging ./example.go:37: undefined: logging

    opened by neurodesign 5
  • revert Error() api change

    revert Error() api change

    In commit 0758a840 Error changed from Error(format string, args ...interface{}) to Error(args ...interface{})

    So now all of Error calls in my projects print the literal formats. I am seeing stuff like:

    err := fmt.Errorf("Error Message")
    log.Error("Thing failed: %s", err)
    

    now prints the format literally like:

    Thing failed: %sError Message
    

    I would say that the Error api should be no different than the other log level statements Critical, Warning, Notice, Info, Debug.

    So I kindly ask you to revert the Error api change made in 0758a840.

    Thanks! -Cory

    opened by coryb 4
  • NewLogBackend signature is different on *nix and Windows

    NewLogBackend signature is different on *nix and Windows

    It causes errors when compiling on Win (but not on Linux\Mac).

    Win: NewLogBackend(out *os.File, prefix string, flag int) *nix: NewLogBackend(out io.Writer, prefix string, flag int)

    https://github.com/op/go-logging/blob/master/log_windows.go#L54 https://github.com/op/go-logging/blob/master/log_nix.go#L40

    In particular here's how we use go-logging: https://github.com/luci/luci-go/blob/master/common/logging/gologger/config.go#L30

    It used to work fine, but after https://github.com/op/go-logging/commit/03393de130e556c06d1e90f3693cdbf279230595 it compiles on Linux\Mac, but not on Windows (causing some confusion).


    IMHO, public API should look identical on all platforms. Requiring os.File everywhere may be a breaking change. What do you think about using io.Writer in NewLogBackend on Win too, but try to cast it to os.File (and enabling colors only if cast succeeds)?

    opened by vadimsht 4
  • Error & Critical level logging issue

    Error & Critical level logging issue

    Messages used in error and critical level logging are displayed separately before log message as logged in formatted manner

    Sample output:

    16:50:01.581 main . DEBU 001 debug ****** 16:50:01.583 main . INFO 002 info 16:50:01.584 main . NOTI 003 notice 16:50:01.584 main . WARN 004 test this is error level logging message 16:50:01.585 main . ERRO 005 this is error level logging message this is critical level message 16:50:01.587 main . CRIT 006 this is critical level message

    code example:

    package main

    import ( "os"

    "github.com/op/go-logging"
    

    )

    var log = logging.MustGetLogger("example")

    // Example format string. Everything except the message has a custom color // which is dependent on the log level. Many fields have a custom output // formatting too, eg. the time returns the hour down to the milli second. var format = logging.MustStringFormatter( "%{color}%{time:15:04:05.000} %{shortfunc} . %{level:.4s} %{id:03x}%{color:reset} %{message}", )

    // Password is just an example type implementing the Redactor interface. Any // time this is logged, the Redacted() function will be called. type Password string

    func (p Password) Redacted() interface{} { return logging.Redact(string(p)) } func main() { //test := "there are log of error i"

    // For demo purposes, create two backend for os.Stderr.
    backend1 := logging.NewLogBackend(os.Stderr, "", 0)
    backend2 := logging.NewLogBackend(os.Stderr, "", 0)
    
    // For messages written to backend2 we want to add some additional
    // information to the output, including the used log level and the name of
    // the function.
    backend2Formatter := logging.NewBackendFormatter(backend2, format)
    
    // Only errors and more severe messages should be sent to backend1
    backend1Leveled := logging.AddModuleLevel(backend1)
    backend1Leveled.SetLevel(logging.ERROR, "")
    
    // Set the backends to be used.
    logging.SetBackend(backend1Leveled, backend2Formatter)
    
    log.Debug("debug %s", Password("secret"))
    log.Info("info")
    log.Notice("notice")
    log.Warning("test")
    log.Error("this is error level logging message")
    

    // log.Error("err") log.Critical("this is critical level message") }

    opened by Jitendrakry 4
  • How do I set up multiple log files?

    How do I set up multiple log files?

    I found a problem when i use go-logging.As follow:

    In my code, I tried to initialize different loggers with Go-logging, logging into different files, and failed.In the end, the log records only the files corresponding to the last initialized logger. why?Does not support?

    opened by Mrqlxdd 0
  • Question: Can you set maximum logging level

    Question: Can you set maximum logging level

    Hello,

    I am trying to accomplish a logging on different channel depending on the level of the log.

    • If logging level is <= Warning, then log to stdout
    • if logging level is >= Error, then log to stderr

    Is there a way to get this work ?

    For now I got

    	backend := logging.NewLogBackend(os.Stdout, "", 0)
    	leveledConsoleBackend = logging.AddModuleLevel(backend)
            // ...
    	errorBackend := logging.NewLogBackend(os.Stderr, "", 0)
    	leveledConsoleErrorBackend = logging.AddModuleLevel(errorBackend)
    	leveledConsoleErrorBackend.SetLevel(logging.ERROR, "")
    
    	logging.SetBackend(leveledConsoleErrorBackend, leveledConsoleBackend)
    

    But with this, I see error logs output both in Stdoud and Stderr

    Thank you for your help!

    opened by pelletier197 1
  • backend, logger: lock before reassigning `defaultBackend`

    backend, logger: lock before reassigning `defaultBackend`

    If not locked, this results in race:

    1. SetBackend(), backend.go:26 - reassigning the global variable (write)
    func SetBackend(backends ...Backend) LeveledBackend {
    	var backend Backend
      // ...
    
      // DARA RACE START
    	defaultBackend = AddModuleLevel(backend)
      // DATA RACE END
    
    	return defaultBackend
    }
    
    1. IsEnabledFor(), logger.go:141 - accessing a default variable (read)
    func (l *Logger) IsEnabledFor(level Level) bool {
    	return defaultBackend.IsEnabledFor(level, l.Module) // DATA RACE HERE
    }
    
    opened by Asalle 0
  • Add a standard logger

    Add a standard logger

    How do you feel about having a predefined 'standard' Logger similar to the Go log package? Having a standard logger is easier so that users don't have to create a Logger manually. The logger will have sensible defaults, configuration helpers, and be accessible through log helper functions.

    opened by ranganath42 0
  • Fix the private backend test

    Fix the private backend test

    Hello, I have noticed the homie "с" insted of a "c" letter. And it seems that the test is incorrect.

    So I fixed "c" letter and changed the condition.

    opened by reinerRubin 0
Owner
Örjan Fors
:hearts: :sparkles: 29k, ex-Spotify. he/him. no human is illegal!
Örjan Fors
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 24 Nov 23, 2021
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
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 151 Apr 27, 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
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.7k May 13, 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
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 Apr 26, 2022
Minimalistic logging library for Go.

logger Minimalistic logging library for Go. Blog Post Features: Advanced output filters (package and/or level) Attributes Timers for measuring perform

Azer Koçulu 151 May 12, 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 May 9, 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
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 241 May 5, 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 162 May 12, 2022
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

私はレオンです 6 Jan 27, 2022
LogVoyage - logging SaaS written in GoLang

No longer maintained, sorry. Completely rewritten v2 is going to be released soon. Please follow http://github.com/logvoyage LogVoyage - fast and simp

null 91 Apr 21, 2022
Utilities for slightly better logging in Go (Golang).

logutils logutils is a Go package that augments the standard library "log" package to make logging a bit more modern, without fragmenting the Go ecosy

HashiCorp 318 Apr 14, 2022