Configurable Logger for Go

Overview

Timber!

This is a logger implementation that supports multiple log levels, multiple output destinations with configurable formats and levels for each. It also supports granular output configuration to get more detailed logging for specific files/packages. Timber includes support for standard XML or JSON config files to get you started quickly. It's also easy to configure in code if you want to DIY.

Features

  • Log levels: Finest, Fine, Debug, Trace, Info, Warn, Error, Critical
  • External configuration via XML and JSON
  • Multiple log destinations (console, file, socket)
  • Configurable format per destination
  • Extensible and pluggable design (if you configure via code rather than XML)

Motivation

I like the log4go design with multiple configurable loggers, but it did not support adding formatters to all of the loggers (it is only supported for files). I thought about trying to contribute patches to log4go but some of the features I wanted would break complete backwards compatibility so I decided to do a rewrite from scratch.

I try to expose everything possible that I think might be useful for someone to replace or extend.

Usage

The easiest way to use Timber is to use configure the built-in singleton:

import (
	log "timber"
)

func main() {
	// load xml config, json also supported
	log.LoadConfiguration("timber.xml")
	log.Info("Timber!!!")
}

An example timber.xml and timber.json are included in the package. Timber does implement the interface of the go log package so replacing the log with Timber will work ok.

log.Close() should be called before your program exits to make sure all the buffers are drained and all messages are printed.

Design

Logger is the interface that is used for logging itself with methods like Warn, Critical, Error, etc. All of these functions expect a Printf-like arguments and syntax for the message.

LogFormatter is a generic interface for taking a LogRecord and formatting into a string to be logged. PatFormatter is the only included implementation of this interface.

LogWriter interface wraps an underlying Writer but doesn't allow errors to propagate. There are implementations for writing to files, sockets and the console.

Timber is a MultiLogger which just means that it implements the Logger interface but can log messages to multiple destinations. Each destination has a LogWriter, level and LogFormatter.

Global is the default unconfigured instance of Timber which may be configured and used or, less commonly, replaced with your own instance (be sure to call Global.Close() before replacing for proper cleanup).

Are you planning to wrap Timber in your own logger? Ever notice that if you wrap the go log package or log4go the source file that gets printed is always your wrapper? Timber.FileDepth sets how far up the stack to go to find the file you actually want. It's set to DefaultFileDepth so add your wrapper stack depth to that.

Completeness

  • Some of the runtime configuration changes have not been implemented, such as MultiLogger.SetLevel and MultiLogger.SetFormatter which change the Level or LogFormatter on-the-fly. Loggers may be added at any time with AddLogger but there is no way to delete loggers right now.

Compatibility

  • I don't support the log4go special handling of the first parameter and probably never will. Right now, all of the Logger methods just expect a Printf-like syntax. If there is demand, I may get the proc syntax in for delayed evaluation.
  • PatFormatter format codes are not the same as log4go
  • PatFormatter always adds a newline at the end of the string so if there's already one there, then you'll get 2 so using Timber to replace the go log package may look a bit messy depending on how you formatted your logging.
Comments
  • Embedded config sections

    Embedded config sections

    I'd rather not have to have a separate config file for my logs from the rest of my service. If we cleaned up and published the config structures (and a couple methods), it would make it trivial to embed timber config in a different config file...

    opened by dgrijalva 5
  • Different log levels depending on caller

    Different log levels depending on caller

    Like log4j, it would be nice to be able to configure different log levels for different parts of a program. In a large complicated program, it's sometimes necessary to enable debug logging for only one subsystem without spamming the logs with debug messages from every other subsystem. For example, in a webapp you might only want to enable debug logging for the code that validates incoming requests, without enabling any other debug output.

    This is different than the ability to configure a log level for each output, which doesn't fulfill the need I'm thinking of.

    This ability is similar to log4j's ability to configure different "loggers" for different classes/packages: http://logging.apache.org/log4j/2.x/manual/configuration.html , search for "logger". We could do something similar based on the filename returned from runtime.Caller(), such as determining the log level based on a prefix of the filename.

    opened by drevell 4
  • Add JSON support for configuration

    Add JSON support for configuration

    Added JSON support for configuration and included a example configuration file for it.

    Added a LoadJSONConfiguration and LoadXMLConfiguration function so that you can specifically load a certain type of config file, otherwise use LoadConfiguration attempts to figure out the type by the given filenames extension.

    opened by larzconwell 2
  • Rework of BufferedWriter to be safer and more flexible

    Rework of BufferedWriter to be safer and more flexible

    Two issues I'm solving here:

    1. bufio.Writer isn't safe to be accessed from multiple goroutines at once.
    2. buffer flushing is based on buffer size. apps that log take forever to flush

    Solutions:

    1. changed all operations on bufio.Writer to happen in a goroutine
    2. added an explicit Flush method
    opened by dgrijalva 2
  • don't try and build syslog formatter on platforms which lack log/syslog

    don't try and build syslog formatter on platforms which lack log/syslog

    timber fails to build on windows, due to log/syslog having a build restriction (!windows,!plan9): http://golang.org/src/pkg/log/syslog/syslog.go

    This adds the same restriction to the syslog formatter, fixing the windows build

    opened by jbert 1
  • syslog sucks

    syslog sucks

    The go1.1 format implementation that I copied prints all sorts of garbage in rsyslogd (ubuntu) because of the timestamp format. Same for mac os x. This new format works and I figured it out from snooping the output of the logger command on ubuntu but is totally fucking broken on mac os. At this point I'm done fucking with it unless someone asks for mac os syslog to work.

    It kinda matches the old RFC 3164 but the hostname field didn't work right (rsyslog would just put the hostname doubled).

    opened by smw1218 0
Owner
DeNA SF
DeNA SF
Logger - Simple logger without written with std pkg

Go-Logger Simple usage is: package main

MaskedTrench 2 Jan 2, 2022
Logger - A thin wrapper of uber-go/zap logger for personal project

a thin wraper of uber-go/zap logger for personal project 0. thanks uber-go/zap B

tsingson 0 Sep 17, 2022
Simple and configurable Logging in Go, with level, formatters and writers

go-log Logging package similar to log4j for the Golang. Support dynamic log level Support customized formatter TextFormatter JSONFormatter Support mul

Guoqiang Chen 13 Sep 26, 2022
Simple, configurable and scalable Structured Logging for Go.

log Log is a simple, highly configurable, Structured Logging library Why another logging library? There's allot of great stuff out there, but also tho

Go Playgound 283 Sep 26, 2022
A logger, for Go

Go-Log A logger, for Go! It's sort of log and code.google.com/p/log4go compatible, so in most cases can be used without any code changes. Breaking cha

Ian Kent 40 Oct 7, 2022
Simple logger for Go programs. Allows custom formats for messages.

go-logger A simple go logger for easy logging in your programs. Allows setting custom format for messages. Preview Install go get github.com/apsdehal/

Amanpreet Singh 283 Dec 17, 2022
Loggly Hooks for GO Logrus logger

Loggly Hooks for Logrus Usage package main import ( "github.com/sirupsen/logrus" "github.com/sebest/logrusly" ) var logglyToken string = "YOUR_LOG

Sebest 28 Sep 26, 2022
A 12-factor app logger built for performance and happy development

logxi log XI is a structured 12-factor app logger built for speed and happy development. Simpler. Sane no-configuration defaults out of the box. Faste

Mario Gutierrez 349 Nov 27, 2022
Dead simple, super fast, zero allocation and modular logger for Golang

Onelog Onelog is a dead simple but very efficient JSON logger. It is one of the fastest JSON logger out there. Also, it is one of the logger with the

Francois Parquet 402 Sep 26, 2022
A logger for Go SQL database driver without modify existing *sql.DB stdlib usage.

SQLDB-Logger A logger for Go SQL database driver without modify existing *sql.DB stdlib usage. Colored console writer output above only for sample/dev

Sarjono Mukti Aji 278 Jan 3, 2023
xlog is a logger for net/context aware HTTP applications

⚠️ Check zerolog, the successor of xlog. HTTP Handler Logger xlog is a logger for net/context aware HTTP applications. Unlike most loggers, xlog will

Olivier Poitrey 137 Sep 26, 2022
Zero Allocation JSON Logger

Zero Allocation JSON Logger The zerolog package provides a fast and simple logger dedicated to JSON output. Zerolog's API is designed to provide both

Olivier Poitrey 7.4k Jan 1, 2023
A powerful zero-dependency json logger.

ZKits Logger Library About This package is a library of ZKits project. This is a zero-dependency standard JSON log library that supports structured JS

Qingshan Luo 23 Dec 14, 2022
A feature-rich and easy to use logger for golang

A feature-rich and easy to use logger for golang ?? Install ?? Common Logs lumber.Success() lumber.Info() lumber.Debug() lumber.Warning()

Matthew Gleich 51 Dec 31, 2022
A minimal and extensible structured logger

⚠️ PRE-RELEASE ⚠️ DO NOT IMPORT THIS MODULE YOUR PROJECT WILL BREAK package log package log provides a minimal interface for structured logging in ser

Go kit 135 Jan 7, 2023
HTTP request logger for Golang

Horus ?? Introduction Horus is a request logger and viewer for Go. It allows developers log and view http requests made to their web application. Inst

Michael Okoh 81 Dec 27, 2022
Simple Yet Powerful Logger

sypl sypl provides a Simple Yet Powerful Logger built on top of the Golang sypl. A sypl logger can have many Outputs, and each Output is responsible f

Sauce Labs 8 Sep 23, 2022
simple concurrent logger

XMUS-LOGGER pure golang logger compatible with golang io standards. USAGE : logOptions := logger.LoggerOptions{ LogLevel: 6, // read more about lo

amupxm [amir hossein mokarrami far] 6 Aug 1, 2022
Binalyze logger is an easily customizable wrapper for logrus with log rotation

logger logger is an easily customizable wrapper for logrus with log rotation Usage There is only one function to initialize logger. logger.Init() When

Binalyze 27 Oct 2, 2022