Giter VIP home page Giter VIP logo

levelog's Introduction

levelog

Log levels for golang's built in log library

Usage

import (
    "github.com/matansh/levelog"
    "github.com/matansh/levelog/loglevel"
)

func main() {
    ctx := context.Background()

    logger := levelog.NewLogger(
        loglevel.Info,
        log.New(os.Stdout, "", log.LstdFlags),
        log.New(os.Stderr, "", log.LstdFlags),
    )
    // from this line onward panics will be logged as errors
    defer logger.PanicRecovery()
    // embedding the logger instance into the applications context
    ctx = levellogger.ContextWithLogger(ctx, logger)

    logger.Info("tada")
}

Usage in your applications tests

import "github.com/matansh/levelog"

func Test(t *testing.T) {
    logger := levelog.NewTestLogger(t)
    ctx := levelog.ContextWithLogger(context.Background(), logger)

    logger.Debug("I am a log statement that will show up as part of the tests output")
    // log statements made as part of the tested code will also show up as part of the tests output
    TestedCode(ctx)
}

Testing against logs

Outputted log statements can be asserted in tests by passing a bytes buffer as the loggers output

import "github.com/matansh/levelog"

func Test(t *testing.T) {
    stdout := bytes.Buffer{}
    stderr := bytes.Buffer{}

    logger := levelog.NewLogger(
        loglevel.Debug,
        log.New(&stdout, "", 0),
        log.New(&stderr, "", 0),
    )
    ctx := levelog.ContextWithLogger(context.Background(), logger)

    TestedCode(ctx)
    // assertions regarding what is or is not in the buffers

As an example, see the tests implemented for the lib itself

Adapting pre exiting usages of the built in log lib

In order to avoid a painful refactoring the github.com/matansh/levelog/log package provides interface parity with the defacto interface exposed by the log lib.

Existing code as such

import "log"

log.Print("foo")
log.Fatalf("%s", "bar")
log.Panicln("spam")

Can simply replace the import statement to import "github.com/matansh/levelog/log" without any breakage or further refactoring.

Background

While there are plenty of logging libraries in the golang sphere I failed to find one that simply wrapped the languages pre existing logging capabilities in a level based interface.

This library does exactly that, it is a nicer packaging for something we already know and love.

footnote

This library is intentionally dependency-less in order to minimize the dependency trees of its importers, you are welcome ;)

levelog's People

Contributors

matansh avatar

Watchers

 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.