Giter VIP home page Giter VIP logo

stdemuxerhook's Introduction

A stdout/stderr demuxer hook for Logrus

Logrus loggers always output all log levels to a single common output, e.g. stderr. This logrus hook makes it so that logs with a severity below Warning are written to stdout while all the important stuff goes to stderr.

You can also use the hook to demux logs to custom IO writers based on severity. Just override the default outputs using the hook's SetOutput(infoLevel, errorLevel io.Writer) method.

Example

Given you have an application that uses a the logrus standard logger similar to this:

import (
    log "github.com/sirupsen/logrus"
)

func main() {

    log.SetLevel(log.InfoLevel)

    log.Info("A group of penguins emerges from the ocean")

    (...)

The only change required is adding in the hook. Make sure to configure the parent logger before initializing the hook as the latter will inherit it's configuration.

import (
    log "github.com/sirupsen/logrus"
    "github.com/Superbalist/stdemuxerhook"
)

func main() {

    log.SetLevel(log.InfoLevel)
    log.AddHook(stdemuxerhook.New(log.StandardLogger()))

    log.Info("A group of penguins emerges from the ocean") // -> logged to stdout
    log.Panic("A group of polar bears emerges from the ocean") // -> logged to stderr

    (...)

}

Benchmarks

BenchmarkLoggerWithoutHook        300000          4348 ns/op
BenchmarkLoggerWithHook           200000          5436 ns/op

stdemuxerhook's People

Contributors

bryanhorstmann avatar janeczku avatar

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.