This repository has been archived by the owner on Feb 17, 2022. It is now read-only.
/
self.go
97 lines (80 loc) · 2.5 KB
/
self.go
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2018 Schibsted
package main
import (
"context"
"fmt"
"os"
"github.com/schibsted/sebase/plog/internal/pkg/plogproto"
"github.com/schibsted/sebase/plog/pkg/plog"
"github.com/schibsted/sebase/plog/pkg/plogd"
"github.com/schibsted/sebase/util/pkg/slog"
)
type selfSession struct {
*Session
}
func (s selfSession) Write(p []byte) (n int, err error) {
if err := s.Writer.Write("log", string(p)); err != nil {
return 0, err
}
return len(p), nil
}
func newSelfSession(dataStore *DataStorage, sessionStore *SessionStorage) (*selfSession, error) {
output, err := dataStore.findOutput("plogd", plogproto.CtxType_log)
if err != nil {
return nil, err
}
sess, err := sessionStore.newSession(output, plogproto.CtxType_log)
if err != nil {
return nil, err
}
return &selfSession{sess}, nil
}
func (s selfSession) InjectSlog() {
slog.Critical = selfSessionLevel{s, plog.Critical.Code()}.LogMsg
slog.Error = selfSessionLevel{s, plog.Error.Code()}.LogMsg
slog.Warning = selfSessionLevel{s, plog.Warning.Code()}.LogMsg
slog.Info = selfSessionLevel{s, plog.Info.Code()}.LogMsg
slog.Debug = selfSessionLevel{s, plog.Debug.Code()}.LogMsg
slog.CtxCritical = selfSessionLevel{s, plog.Critical.Code()}.LogCtxMsg
slog.CtxError = selfSessionLevel{s, plog.Error.Code()}.LogCtxMsg
slog.CtxWarning = selfSessionLevel{s, plog.Warning.Code()}.LogCtxMsg
slog.CtxInfo = selfSessionLevel{s, plog.Info.Code()}.LogCtxMsg
slog.CtxDebug = selfSessionLevel{s, plog.Debug.Code()}.LogCtxMsg
}
func (s selfSession) ResetSlog() {
slog.Critical.SetLogPrintf()
slog.Error.SetLogPrintf()
slog.Warning.SetLogPrintf()
slog.Info.SetLogPrintf()
slog.Debug.Disable()
slog.CtxCritical.SetLogger(&slog.Critical)
slog.CtxError.SetLogger(&slog.Error)
slog.CtxWarning.SetLogger(&slog.Warning)
slog.CtxInfo.SetLogger(&slog.Info)
slog.CtxDebug.SetLogger(&slog.Debug)
}
type selfSessionLevel struct {
selfSession
level string
}
func (s selfSessionLevel) LogMsg(msg string, kvs ...interface{}) {
m := slog.KVsMap(kvs...)
if len(m) == 0 {
s.Writer.Write(s.level, msg)
} else {
m["msg"] = msg
s.Writer.Write(s.level, m)
}
}
func (s selfSessionLevel) LogCtxMsg(ctx context.Context, msg string, kvs ...interface{}) {
// Avoid infinite recursing messages about plogd problems.
if plogd.ContextProg(ctx) == "plogd" {
stderrLogger.LogMsg(msg, kvs...)
} else {
s.LogMsg(msg, kvs...)
}
}
var stderrLogger = slog.DefaultLogger{stderrPrintf}
func stderrPrintf(format string, v ...interface{}) {
fmt.Fprintf(os.Stderr, format, v...)
}