Code Monkey home page Code Monkey logo

logbuch's Introduction

Logbuch

Go Reference CircleCI Go Report Card Chat on Discord

Simple Go logging library with support for different output channels (io.Writer) for each log level. A formatter can be provided to change the log output formatting.

Installation

To install logbuch, run go get within your project:

go get github.com/emvi/logbuch

Usage

Here is a quick example on how to use the basic functionality of logbuch:

package main

import (
    "os"
    "github.com/emvi/logbuch"
)

func main() {
    // use the default logger (logging to stdout and stderr)
    logbuch.Debug("Hello %s!", "World")
    logbuch.Info("Info")
    logbuch.Warn("Warning")
    logbuch.Error("Error")

    // logging cannot be disabled for errors except you use the DiscardFormatter
    logbuch.SetLevel(logbuch.LevelInfo)
    logbuch.Debug("Don't log this anymore!")

    // create your own logger
    l := logbuch.NewLogger(os.Stdout, os.Stderr)
    l.Debug("Just like the default logger...")
    l.SetFormatter(logbuch.NewDiscardFormatter())
    l.Error("This error will be dropped!")
    
    // or to panic...
    l.Fatal("We are going down! Error code: %d", 123)
}

Formatters

To use formatters you can either implement your own or use one provided by logbuch. There are three kind of formatters provided right now:

StandardFormatter

This is the default. The log output looks like this:

2019-09-19T17:39:02.4326139+02:00 [DEBUG] This is a debug message.
2019-09-19T17:39:02.4326139+02:00 [INFO ] Hello World!
2019-09-19T17:39:02.4326139+02:00 [WARN ] Some formatted message 123.
2019-09-19T17:39:02.4326139+02:00 [ERROR] An error occurred: 123

FieldFormatter

The FieldFormatter prints the log parameters in a structured way. To have a nice logging output, use the logbuch.Fields type together with this:

formatter := logbuch.NewFieldFormatter(logbuch.StandardTimeFormat, "\t\t\t")
logbuch.SetFormatter(formatter)
logbuch.Debug("Debug message", logbuch.Fields{"some": "value", "code": 123})

The log output looks like this:

2019-09-19T17:45:26.6635897+02:00 [DEBUG] Debug message				 some=value code=123

DiscardFormatter

The DiscardFormatter simply drops all log messages (including errors) and can be used to do just that.

Persistent logs

If you want to persist log data, you can use any io.Writer to do so. logbuch comes with a rolling file appender which can be used to store log output into rolling log files. Here is a quick example of it:

package main

import (
    "fmt"
    "github.com/emvi/logbuch"
)

// create a naming schema for log files
type NameSchema struct {
    name string
    counter int
}

func (schema *NameSchema) Name() string {
    schema.counter++
    return fmt.Sprintf("%d_%s.log", schema.counter, schema.name)
}

func main() {
    stdNameSchema := &NameSchema{name: "std"}
    errNameSchema := &NameSchema{name: "err"}

    // create rolling file appenders for stdout and stderr
    // using a maximum of 5 files, 5 MB per file and a buffer of 4 KB
    stdout, _ := logbuch.NewRollingFileAppender(5, 1024*1024*5, 1024*4, "logs", stdNameSchema)
    stderr, _ := logbuch.NewRollingFileAppender(5, 1024*1024*5, 1024*4, "logs", errNameSchema)

    // this is important!
    defer stdout.Close()
    defer stderr.Close()

    // create your logger
    l := logbuch.NewLogger(stdout, stderr)
    l.Info("Log to standard output files...")
    l.Error("Log to standard error files...")
}

This example will create a directory called logs and writes all standard output to files called 1_std.log and all error output to files called 1_err.log for up to 5 files before starting rolling over. Note that you must close the rolling file appenders.

Contribute

See CONTRIBUTING.md

License

MIT

logbuch's People

Contributors

kugelschieber avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

logbuch's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.