GithubHelp home page GithubHelp logo

logo's Introduction

logo Go Reference Go Report Card codecov Coverage Status

Experimental, opinionated and minimalistic logging library for Go.

Library provides only two levels out of box — Verbose and Important. Why so? It's mostly inspired by 🇷🇺 this post by @tonsky and my personal experience.

TLDR:

  • Only two logging levels: verbose and important.
  • Important level is for errors and business-critical stuff. Verbose level is for development purposes.
  • Stuff like “successfully connected to host ABC”, “binded port 8000”, etc. are not needed even at verbose level.
  • Libraries only use important level because debug related things are interesting only for library developers.

However library allows to define custom levels. But before doing it, please think carefully.

Installation

go get github.com/tomakado/logo

Usage

You can use pre-instantiated logger and wrapper functions around it or create and customize your own.

Quick start

For quick start use package-level functions like this:

package main

import (
    "context"

    "github.com/tomakado/logo/log"
)

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

    log.Verbose(ctx, "hello!")
    log.Important(ctx, "hello, it's important")
    log.VerboseX(ctx, "hello with extra!", log.Extra{"foo": "bar"})
    log.Verbosef(ctx, "hello, %s", "Jon Snow")

    log.Write(ctx, log.LevelImportant, "hello, it's me", Extra{"a": 42})
    log.Writef(ctx, log.LevelVerbose, "My name is %s, I'm %d y.o.", "Ildar", 23)
}

For fine-tuned logging create custom logger with NewLogger function:

package main

import (
    "context"
    "os"

    "github.com/tomakado/logo/log"
)

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

    logger := log.NewLogger(log.LevelImportant, os.Stderr, log.SimpleTextFormatter)

    logger.Verbose(ctx, "hello!") // will not be sent to output
    logger.Important(ctx, "this is really important")
}

Logging levels

logo's logging level is a pair of numeric value and string representation of level and can be defined with NewLevel function:

var (
    LevelVerbose   Level = NewLevel(10, "VERBOSE")
    LevelImportant Level = NewLevel(20, "IMPORTANT")
)

Message format

NewLogger accepts Formatter as third argument to create logger. There are two formatter types out of box: JSONFormatter and TemplateFormatter and two pre-instantiated template formatters: SimpleTextFormatter and TableTextFormatter.

TemplateFormatter uses template engine from Go's standard library to format messages:

tmpl, err := template.New("tmpl_fmt_example").
    Parse("ts={{.Time}} level={{.Level}} msg=\"{{.Message}}\" extra={{.Extra}}")
if err != nil {
    panic(err)
}

formatter := log.NewTemplateFormatter(tmpl)
logger := log.NewLogger(log.LevelVerbose, os.Stdout, formatter)

logger.Verbose(context.Background(), "hello")

Hooks

Hooks are functions called before or after log message has been sent to output. Pre-hooks are useful when you need to extend the context of event. Post-hooks can be used to send events to external services (e.g. Sentry), collect metrics, etc.

package main

import (
    "context"

    "github.com/tomakado/logo/hooks"
    "github.com/tomakado/logo/log"
)

func main() {
    ...

    log.PreHook(func(ctx context.Context, e *log.Event) {
        e.Extra["request_id"] = uuid.New()
    })

    log.PostHook(
        hooks.FilteredHook(
            func(ctx context.Context, e *log.Event) {
                // Send event to external log storage here
            },
            hooks.LevelBoundsFilter(log.LevelVerbose, log.LevelImportant),
        ),
    )

    log.PostHook(hooks.ExitOnImportant) // os.Exit(1) if event level is >= log.LevelImportant

    ...
}

Contributing

If you want to contribute to logo — you're welcome! Feel free to send your issues and PRs.

logo's People

Contributors

tomakado avatar

Stargazers

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