Golang logging library

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.
Owner
Örjan Fors
:hearts: :sparkles: 29k, ex-Spotify. he/him. no human is illegal!
Örjan Fors
Comments
  • 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:

  • 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!

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

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

  • 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.

  • 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
    
  • 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

  • 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

  • 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

  • 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)?

  • 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") }

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

  • 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!

  • 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
    }
    
  • 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.

  • 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.

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
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
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
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
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
Minimalistic logging library for Go.
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

Nov 16, 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
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
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
LogVoyage - logging SaaS written in GoLang
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

Sep 26, 2022