-
Notifications
You must be signed in to change notification settings - Fork 0
/
journald.go
165 lines (148 loc) · 4.44 KB
/
journald.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Copyright © 2023 omegarogue
* SPDX-License-Identifier: AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Package journald
package journald
import (
"bytes"
"encoding/json"
"fmt"
"runtime"
"strconv"
"strings"
"github.com/OmegaRogue/weylus-desktop/utils/pthread"
"github.com/coreos/go-systemd/v22/journal"
"github.com/coreos/go-systemd/v22/sdjournal"
"github.com/pkg/errors"
"github.com/rs/zerolog"
)
const (
ThreadFieldName = "thread"
)
type betterJournaldWriter struct {
}
func (b betterJournaldWriter) WriteLevel(level zerolog.Level, p []byte) (int, error) {
var output map[string]any
if err := json.Unmarshal(p, &output); err != nil {
return 0, errors.Wrap(err, "unmarshal intermediate log message")
}
return b.WriteJSONLevel(level, output, p)
}
type ThreadHook struct {
}
func (t ThreadHook) Run(e *zerolog.Event, _ zerolog.Level, _ string) {
e.Uint64("thread", pthread.Self().ID())
}
// GetGID gets the current goroutine ID (copied from https://blog.sgmansfield.com/2015/12/goroutine-ids/)
func GetGID() uint64 {
b := make([]byte, 64)
b = b[:runtime.Stack(b, false)]
b = bytes.TrimPrefix(b, []byte("goroutine "))
b = b[:bytes.IndexByte(b, ' ')]
n, _ := strconv.ParseUint(string(b), 10, 64)
return n
}
func (b betterJournaldWriter) WriteJSONLevel(level zerolog.Level, o map[string]any, p []byte) (int, error) {
var message string
prio := zerologLevelToJournaldPriority(level)
args := make(map[string]string)
for key, value := range o {
jKey := strings.ToUpper(key)
switch key {
case zerolog.LevelFieldName, zerolog.TimestampFieldName:
continue
case zerolog.MessageFieldName:
message, _ = value.(string)
continue
case zerolog.CallerFieldName:
call := strings.Split(value.(string), ":")
args[sdjournal.SD_JOURNAL_FIELD_CODE_FILE] = call[0]
args[sdjournal.SD_JOURNAL_FIELD_CODE_LINE] = call[1]
continue
case ThreadFieldName:
args["TID"] = fmt.Sprint(uint64(value.(float64)))
continue
case zerolog.ErrorStackFieldName:
if stackTrace, ok := value.([]any); ok {
if frame, ok := stackTrace[0].(map[string]any); ok {
args[sdjournal.SD_JOURNAL_FIELD_CODE_FUNC] = frame["func"].(string)
args[sdjournal.SD_JOURNAL_FIELD_CODE_LINE] = frame["line"].(string)
args[sdjournal.SD_JOURNAL_FIELD_CODE_FILE] = frame["source"].(string)
}
}
}
switch v := value.(type) {
case string:
args[jKey] = v
case json.Number:
args[jKey] = fmt.Sprint(value)
default:
b, err := zerolog.InterfaceMarshalFunc(value)
if err != nil {
args[jKey] = fmt.Sprintf("[error: %v]", err)
} else {
args[jKey] = string(b)
}
}
}
args["JSON"] = string(p)
if err := journal.Send(message, prio, args); err != nil {
return 0, errors.Wrap(err, "send journal message")
}
return len(p), nil
}
func zerologLevelToJournaldPriority(level zerolog.Level) journal.Priority {
switch level {
case zerolog.TraceLevel:
return journal.PriDebug
case zerolog.DebugLevel:
return journal.PriDebug
case zerolog.InfoLevel:
return journal.PriInfo
case zerolog.WarnLevel:
return journal.PriWarning
case zerolog.ErrorLevel:
return journal.PriErr
case zerolog.FatalLevel:
return journal.PriCrit
case zerolog.PanicLevel:
return journal.PriEmerg
case zerolog.NoLevel:
return journal.PriNotice
}
return journal.PriNotice
}
func (b betterJournaldWriter) Write(p []byte) (n int, err error) {
var output map[string]any
if err := json.Unmarshal(p, &output); err != nil {
return 0, errors.Wrap(err, "unmarshal intermediate log message")
}
level, ok := output["level"].(string)
var lvl zerolog.Level
if ok {
lvl, err = zerolog.ParseLevel(level)
if err != nil {
return 0, errors.Wrap(err, "parse level")
}
} else {
lvl = zerolog.NoLevel
}
return b.WriteJSONLevel(lvl, output, p)
}
func NewBetterJournaldWriter() zerolog.LevelWriter {
return betterJournaldWriter{}
}