Git Product home page Git Product logo

log.go's Introduction

Log

An attempt to solve the 'company standard log library' problem with a very simple logging interface for go built around context values.

Why

I have worked in a few large Go teams. Each one of them had a pkg or std github repo containing the team's standard for basic go tasks like logging, tracing, http (circuit breaking), sqs wrappers etc etc. As a rule, I always encourage these to be individual independent components, which could be open-source and reusable for many teams. The problem has always been how these standard components log without tightly coupling them to the logging library itself.

The popular logging frameworks for go cannot be described purely as an interface. e.g., Logrus uses *logrus.Fields

Use Cases

Add context information

Including log information from the context. e.g. trace headers.

Assuming a middleware stack, include this as or in a middleware after the context value is created:

ctx = log.WithField(ctx, "trace", trace.FromContext(ctx))

Later calls to log.Info(ctx, "hello") will include the trace header

Library Use

Libraries shouldn't log. But they need to. But they shouldn't.

Solution: Include a minimal logging interface in your library, which defaults to do nothing, but can be wired in to a real logger.

type SQSWorker struct {
	Logger interface{
		Info(context.Context, msg)
		WithFields(context.Context, map[string]interface{}) context.Context
	}
}

The logger interface is designed to be copied (not referenced) in libraries.

log.go's People

Contributors

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