MCPcopy Index your code
hub / github.com/subtrace/subtrace / Init

Function Init

logging/logging.go:18–60  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

16var Logfile string
17
18func Init() error {
19 _, path, _, _ := runtime.Caller(0)
20 prefix := strings.TrimSuffix(path, "/logging/logging.go")
21
22 level := &slog.LevelVar{}
23 if Verbose {
24 level.Set(slog.LevelDebug)
25 } else {
26 level.Set(slog.LevelInfo)
27 }
28
29 opts := &slog.HandlerOptions{
30 AddSource: true,
31 Level: level,
32 ReplaceAttr: func(groups []string, attr slog.Attr) slog.Attr {
33 switch attr.Key {
34 case "source":
35 src := attr.Value.Any().(*slog.Source)
36 src.File = strings.TrimPrefix(src.File, prefix+"/")
37 src.File = strings.TrimPrefix(src.File, filepath.Dir(prefix)+"/")
38 return slog.Attr{Key: "src", Value: attr.Value}
39 case "msg":
40 msg := attr.Value.Any().(string)
41 if msg == "" {
42 return slog.Attr{}
43 }
44 }
45 return attr
46 },
47 }
48
49 out := os.Stdout
50 if Logfile != "" {
51 f, err := os.OpenFile(Logfile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
52 if err != nil {
53 return fmt.Errorf("create logfile: %w", err)
54 }
55 out = f
56 }
57 slog.SetDefault(slog.New(slog.NewTextHandler(out, opts)))
58
59 return nil
60}

Callers 4

entrypointMethod · 0.92
entrypointMethod · 0.92
entrypointMethod · 0.92
entrypointMethod · 0.92

Calls 1

SetMethod · 0.45

Tested by

no test coverage detected