File: main.go

package info (click to toggle)
golang-github-chainguard-dev-clog 1.7.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 424 kB
  • sloc: makefile: 4
file content (30 lines) | stat: -rw-r--r-- 773 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package main

import (
	"context"
	"flag"
	"log/slog"
	"os"

	"github.com/chainguard-dev/clog"
	"github.com/chainguard-dev/clog/slag"
)

func main() {
	var level slag.Level
	flag.Var(&level, "log-level", "log level")
	flag.Parse()

	slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: &level})))

	log := clog.NewLogger(slog.Default()).With("a", "b")
	ctx := clog.WithLogger(context.Background(), log)

	// Grab logger from context and use
	clog.FromContext(ctx).With("foo", "bar").Debugf("hello debug world")
	clog.FromContext(ctx).With("info", true).Infof("hello info world")
	clog.FromContext(ctx).With("warn", 42).Warnf("hello warn world")

	// Package level context loggers are also aware
	clog.ErrorContext(ctx, "hello error world")
}