Giter Site home page Giter Site logo

slogx's Introduction

slogx

log/slog stdlib requires go version >= 1.21.0

slog handler with opentelemetry tracing support and simple init helper method

trace_id in log

image

added Apex log cli handler like handler (impl ref https://github.com/apex/log/blob/master/handlers/cli/cli.go)

apex cli log like handler

usage

package main

import (
    "context"
    "github.com/ttys3/slogx"
    "github.com/ttys3/tracing-go"
    "go.opentelemetry.io/otel"
    "log/slog"
    "io"
)

func main() {
	// init a new slog json handler at info level with output to stderr
	// and wrap it within a tracing handler
	logger := slogx.New(slogx.WithTracing())
	slog.SetDefault(logger)

	ctx := context.Background()

	// set up a recording tracer, non-recording span will not get a trace_id
	tpShutdown, err := tracing.InitProvider(ctx, tracing.WithStdoutTrace())
	if err != nil {
		panic(err)
	}
	defer tpShutdown(context.Background())

	ctx, newSpan := otel.Tracer("my-tracer-name").Start(ctx, "hello.Slog")
	defer newSpan.End()
	log := slog.With("user_id", 123456)
	// no trace_id in log
	log.Info("hello world")

	// has trace_id in log
	log.With("foo", "bar").ErrorContext(ctx, "have a nice day", "err", io.ErrClosedPipe)
	log.ErrorContext(ctx, "example error", "err", io.ErrClosedPipe)
}

related projects

slog.Handler that writes tinted logs https://github.com/lmittmann/tint/

Example ConsoleHandler for slog Logger https://gist.github.com/wijayaerick/de3de10c47a79d5310968ba5ff101a19

slogx's People

Contributors

dependabot[bot] avatar stevenacoffman avatar ttys3 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

slogx's Issues

a better wrapper

since slog api changed, and has WithContext method and Ctx method removed, and add some bad InfoCtx like method.

we need a better wrapper.

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.