mirror of
https://github.com/netbirdio/netbird.git
synced 2024-12-24 23:59:25 +01:00
24e031ab74
```console journalctl ``` ```diff - Jul 19 14:41:01 rpi /usr/bin/netbird[614]: 2024-07-19T14:41:01+02:00 ERRO %!s(<nil>): error while handling message of Peer [key: REDACTED] error: [wrongly addressed message REDACTED] - Jul 19 21:53:03 rpi /usr/bin/netbird[614]: 2024-07-19T21:53:03+02:00 WARN %!s(<nil>): disconnected from the Signal service but will retry silently. Reason: rpc error: code = Internal desc = server closed the stream without sending trailers - Jul 19 21:53:04 rpi /usr/bin/netbird[614]: 2024-07-19T21:53:04+02:00 INFO %!s(<nil>): connected to the Signal Service stream - Jul 19 22:24:10 rpi /usr/bin/netbird[614]: 2024-07-19T22:24:10+02:00 WARN [error: read udp 192.168.1.11:48398->9.9.9.9:53: i/o timeout, upstream: 9.9.9.9:53] %!s(<nil>): got an error while connecting to upstream + Jul 19 14:41:01 rpi /usr/bin/netbird[614]: error while handling message of Peer [key: REDACTED] error: [wrongly addressed message REDACTED] + Jul 19 21:53:03 rpi /usr/bin/netbird[614]: disconnected from the Signal service but will retry silently. Reason: rpc error: code = Internal desc = server closed the stream without sending trailers + Jul 19 21:53:04 rpi /usr/bin/netbird[614]: connected to the Signal Service stream + Jul 19 22:24:10 rpi /usr/bin/netbird[614]: [error: read udp 192.168.1.11:48398->9.9.9.9:53: i/o timeout, upstream: 9.9.9.9:53] got an error while connecting to upstream ``` please notice that although log level is no longer present in the syslog message it is still respected by syslog logger, so the log levels are not lost: ```console journalctl -p 3 ``` ```diff - Jul 19 14:41:01 rpi /usr/bin/netbird[614]: 2024-07-19T14:41:01+02:00 ERRO %!s(<nil>): error while handling message of Peer [key: REDACTED] error: [wrongly addressed message REDACTED] + Jul 19 14:41:01 rpi /usr/bin/netbird[614]: error while handling message of Peer [key: REDACTED] error: [wrongly addressed message REDACTED] ```
84 lines
1.9 KiB
Go
84 lines
1.9 KiB
Go
package formatter
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// TextFormatter formats logs into text with included source code's path
|
|
type TextFormatter struct {
|
|
timestampFormat string
|
|
levelDesc []string
|
|
}
|
|
|
|
// SyslogFormatter formats logs into text
|
|
type SyslogFormatter struct {
|
|
levelDesc []string
|
|
}
|
|
|
|
var validLevelDesc = []string{"PANC", "FATL", "ERRO", "WARN", "INFO", "DEBG", "TRAC"}
|
|
|
|
|
|
// NewTextFormatter create new MyTextFormatter instance
|
|
func NewTextFormatter() *TextFormatter {
|
|
return &TextFormatter{
|
|
levelDesc: validLevelDesc,
|
|
timestampFormat: time.RFC3339, // or RFC3339
|
|
}
|
|
}
|
|
|
|
// NewSyslogFormatter create new MySyslogFormatter instance
|
|
func NewSyslogFormatter() *SyslogFormatter {
|
|
return &SyslogFormatter{
|
|
levelDesc: validLevelDesc,
|
|
}
|
|
}
|
|
|
|
// Format renders a single log entry
|
|
func (f *TextFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
|
var fields string
|
|
keys := make([]string, 0, len(entry.Data))
|
|
for k, v := range entry.Data {
|
|
if k == "source" {
|
|
continue
|
|
}
|
|
keys = append(keys, fmt.Sprintf("%s: %v", k, v))
|
|
}
|
|
|
|
if len(keys) > 0 {
|
|
fields = fmt.Sprintf("[%s] ", strings.Join(keys, ", "))
|
|
}
|
|
|
|
level := f.parseLevel(entry.Level)
|
|
|
|
return []byte(fmt.Sprintf("%s %s %s%s: %s\n", entry.Time.Format(f.timestampFormat), level, fields, entry.Data["source"], entry.Message)), nil
|
|
}
|
|
|
|
func (f *TextFormatter) parseLevel(level logrus.Level) string {
|
|
if len(f.levelDesc) < int(level) {
|
|
return ""
|
|
}
|
|
|
|
return f.levelDesc[level]
|
|
}
|
|
|
|
// Format renders a single log entry
|
|
func (f *SyslogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
|
var fields string
|
|
keys := make([]string, 0, len(entry.Data))
|
|
for k, v := range entry.Data {
|
|
if k == "source" {
|
|
continue
|
|
}
|
|
keys = append(keys, fmt.Sprintf("%s: %v", k, v))
|
|
}
|
|
|
|
if len(keys) > 0 {
|
|
fields = fmt.Sprintf("[%s] ", strings.Join(keys, ", "))
|
|
}
|
|
return []byte(fmt.Sprintf("%s%s\n", fields, entry.Message)), nil
|
|
}
|