8000 GitHub - eberle1080/slog-pretty-json: A slog handler which will output nicely formatted pretty JSON
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

eberle1080/slog-pretty-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

slog-pretty-json

A simple slog handler that outputs nicely formatted JSON. The handler can be used as a drop in replacement for the standard slog.JSONHandler while still supporting colorised output.

Uses github.com/tidwall/pretty for formatting and github.com/alecthomas/chroma for syntax highlighting.

Example output

Installation

Add the module to your project:

go get github.com/eberle1080/slog-pretty-json/slog/prettyjson

Basic usage

Create a handler and set it as the default logger. The example below mirrors testing/main.go in this repository.

package main

import (
    "log/slog"
    "os"

    "github.com/eberle1080/slog-pretty-json/slog/prettyjson"
)

func main() {
    handler, err := prettyjson.NewHandler(os.Stdout, &slog.HandlerOptions{
        AddSource: true,
        Level:     slog.LevelDebug,
    }, prettyjson.WithStyle("github"))
    if err != nil {
        panic(err)
    }

    logger := slog.New(handler)
    slog.SetDefault(logger)

    slog.Info("This is an info message",
        slog.String("key1", "value1"),
        slog.Int("key2", 42),
        slog.Bool("key3", true))
}

About

A slog handler which will output nicely formatted pretty JSON

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0