blob: 277cd36ec7552928d399df7b0a259dc3d7fea682 [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
2// SPDX-License-Identifier: Apache-2.0
3
Serge Bazanski6c8ee0b2023-04-05 12:29:57 +02004package logtree
5
Serge Bazanski3c5d0632024-09-12 10:49:12 +00006import (
7 "google.golang.org/grpc/grpclog"
8
9 "source.monogon.dev/go/logging"
10)
Serge Bazanski6c8ee0b2023-04-05 12:29:57 +020011
12// GRPCify turns a LeveledLogger into a go-grpc compatible logger.
Serge Bazanski3c5d0632024-09-12 10:49:12 +000013func GRPCify(logger logging.Leveled) grpclog.LoggerV2 {
Serge Bazanski6c8ee0b2023-04-05 12:29:57 +020014 lp, ok := logger.(*leveledPublisher)
15 if !ok {
16 // Fail fast, as this is a programming error.
17 panic("Expected *leveledPublisher in LeveledLogger from supervisor")
18 }
19
20 lp2 := *lp
21 lp2.depth += 1
22
23 return &leveledGRPCV2{
24 lp: &lp2,
25 }
26}
27
28type leveledGRPCV2 struct {
29 lp *leveledPublisher
30}
31
32func (g *leveledGRPCV2) Info(args ...interface{}) {
33 g.lp.Info(args...)
34}
35
36func (g *leveledGRPCV2) Infoln(args ...interface{}) {
37 g.lp.Info(args...)
38}
39
40func (g *leveledGRPCV2) Infof(format string, args ...interface{}) {
41 g.lp.Infof(format, args...)
42}
43
44func (g *leveledGRPCV2) Warning(args ...interface{}) {
45 g.lp.Warning(args...)
46}
47
48func (g *leveledGRPCV2) Warningln(args ...interface{}) {
49 g.lp.Warning(args...)
50}
51
52func (g *leveledGRPCV2) Warningf(format string, args ...interface{}) {
53 g.lp.Warningf(format, args...)
54}
55
56func (g *leveledGRPCV2) Error(args ...interface{}) {
57 g.lp.Error(args...)
58}
59
60func (g *leveledGRPCV2) Errorln(args ...interface{}) {
61 g.lp.Error(args...)
62}
63
64func (g *leveledGRPCV2) Errorf(format string, args ...interface{}) {
65 g.lp.Errorf(format, args...)
66}
67
68func (g *leveledGRPCV2) Fatal(args ...interface{}) {
69 g.lp.Fatal(args...)
70}
71
72func (g *leveledGRPCV2) Fatalln(args ...interface{}) {
73 g.lp.Fatal(args...)
74}
75
76func (g *leveledGRPCV2) Fatalf(format string, args ...interface{}) {
77 g.lp.Fatalf(format, args...)
78}
79
80func (g *leveledGRPCV2) V(l int) bool {
Serge Bazanski3c5d0632024-09-12 10:49:12 +000081 return g.lp.V(logging.VerbosityLevel(l)).Enabled()
Serge Bazanski6c8ee0b2023-04-05 12:29:57 +020082}