GithubHelp home page GithubHelp logo

podhmo / ctxlog Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 29 KB

contextual logging and structural logging

License: MIT License

Go 99.13% Makefile 0.87%
go golang logging log contextual-logging structrual-logging

ctxlog's Introduction

CircleCI

ctxlog

contextual logging and structural logging

main.go

package main

import (
	"context"
	"log"

	"github.com/podhmo/ctxlog"
	"github.com/podhmo/ctxlog/zapctxlog"
)

func main() {
	if err := run(); err != nil {
		log.Fatal(err)
	}
}

func run() error {
	l := zapctxlog.MustNew()
	ctx, _ := ctxlog.Set(context.Background(), l).With("x-id", 10)
	return f(ctx)
}

func f(ctx context.Context) error {
	ctx, log := ctxlog.Get(ctx).With("y-id", 20)
	log.Info("start f")
	defer log.Info("end f")
	return g(ctx)
}

func g(ctx context.Context) error {
	ctx, log := ctxlog.Get(ctx).With("y-id", 20)
	log.Info("start g")
	defer log.Info("end g")
	return nil
}

output

$ go run _examples/00readme/main.go
{"level":"info","ts":1565388319.7551425,"caller":"00readme/main.go:29","msg":"start f","x-id":10,"y-id":20}
{"level":"info","ts":1565388319.7551734,"caller":"00readme/main.go:36","msg":"start g","x-id":10,"y-id":20,"y-id":20}
{"level":"info","ts":1565388319.7551854,"caller":"00readme/main.go:38","msg":"end g","x-id":10,"y-id":20,"y-id":20}
{"level":"info","ts":1565388319.755195,"caller":"00readme/main.go:31","msg":"end f","x-id":10,"y-id":20}

WithError()

package main

import (
	"context"
	"fmt"

	"github.com/pkg/errors"
	"github.com/podhmo/ctxlog"
	"github.com/podhmo/ctxlog/zapctxlog"
)


func main() {
	l := zapctxlog.MustNew()
	ctx, _ := ctxlog.Set(context.Background(), l).With("x-id", 10)
	if err := f(ctx); err != nil {
		ctxlog.Get(ctx).WithError(err).Warning("!!")
	}
}

func f(ctx context.Context) error {
	return errors.Wrap(fmt.Errorf("hmm"), "on f")
}
{
  "level":"warn",
  "ts":1565421996.0991826,
  "caller":"01readme/main.go:16",
  "msg":"!!",
  "x-id":10,
  "error":"on f: hmm",
  "errorVerbose":"hmm\non f\nmain.f\n\t$GOPATH/src/github.com/podhmo/ctxlog/zapctxlog/_examples/01readme/main.go:21\nmain.main\n\t$GOPATH/src/github.com/podhmo/ctxlog/zapctxlog/_examples/01readme/main.go:15\nruntime.main\n\t/usr/lib/go/src/runtime/proc.go:200\nruntime.goexit\n\t/usr/lib/go/src/runtime/asm_amd64.s:1337"
}

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.