mirror of
https://github.com/hay-kot/homebox.git
synced 2025-07-30 14:20:27 +00:00
switch to zero log
This commit is contained in:
parent
9351b3fd42
commit
68204a4f22
26 changed files with 122 additions and 1335 deletions
|
@ -1,132 +0,0 @@
|
|||
package logger
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Level int8
|
||||
|
||||
var (
|
||||
IncludeTrace = false
|
||||
)
|
||||
|
||||
const (
|
||||
LevelDebug Level = iota
|
||||
LevelInfo
|
||||
LevelError
|
||||
LevelFatal
|
||||
LevelOff
|
||||
)
|
||||
|
||||
func (l Level) String() string {
|
||||
switch l {
|
||||
case LevelDebug:
|
||||
return "DEBUG"
|
||||
case LevelInfo:
|
||||
return "INFO"
|
||||
case LevelError:
|
||||
return "ERROR"
|
||||
case LevelFatal:
|
||||
return "FATAL"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
type Props map[string]string
|
||||
|
||||
type Logger struct {
|
||||
out io.Writer
|
||||
minLevel Level
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func New(out io.Writer, minLevel Level) *Logger {
|
||||
return &Logger{
|
||||
out: out,
|
||||
minLevel: minLevel,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Logger) Debug(message string, properties map[string]string) {
|
||||
l.print(LevelDebug, message, properties)
|
||||
}
|
||||
|
||||
func (l *Logger) Info(message string, properties map[string]string) {
|
||||
l.print(LevelInfo, message, properties)
|
||||
}
|
||||
|
||||
func (l *Logger) Error(err error, properties map[string]string) {
|
||||
l.print(LevelError, err.Error(), properties)
|
||||
}
|
||||
|
||||
func (l *Logger) Fatal(err error, properties map[string]string) {
|
||||
l.print(LevelFatal, err.Error(), properties)
|
||||
os.Exit(1) // For entries at the FATAL level, we also terminate the application.
|
||||
}
|
||||
|
||||
func (l *Logger) print(level Level, message string, properties map[string]string) (int, error) {
|
||||
// If the severity level of the log entry is below the minimum severity for the
|
||||
// logger, then return with no further action.
|
||||
if level < l.minLevel {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// Declare an anonymous struct holding the data for the log entry.
|
||||
aux := struct {
|
||||
Level string `json:"level"`
|
||||
Time string `json:"time"`
|
||||
Message string `json:"message"`
|
||||
Properties map[string]string `json:"properties,omitempty"`
|
||||
Trace string `json:"trace,omitempty"`
|
||||
}{
|
||||
Level: level.String(),
|
||||
Time: time.Now().UTC().Format(time.RFC3339),
|
||||
Message: message,
|
||||
Properties: properties,
|
||||
}
|
||||
|
||||
// Include a stack trace for entries at the ERROR and FATAL levels.
|
||||
dumpTrace := false
|
||||
if level >= LevelError {
|
||||
dumpTrace = true
|
||||
if IncludeTrace {
|
||||
aux.Trace = string(debug.Stack())
|
||||
}
|
||||
}
|
||||
|
||||
// Declare a line variable for holding the actual log entry text.
|
||||
var line []byte
|
||||
|
||||
// Marshal the anonymous struct to JSON and store it in the line variable. If there
|
||||
// was a problem creating the JSON, set the contents of the log entry to be that
|
||||
// plain-text error message instead.”
|
||||
line, err := json.Marshal(aux)
|
||||
if err != nil {
|
||||
line = []byte(LevelError.String() + ": unable to marshal log message:" + err.Error())
|
||||
}
|
||||
|
||||
// Lock the mutex so that no two writes to the output destination cannot happen
|
||||
// concurrently. If we don't do this, it's possible that the text for two or more
|
||||
// log entries will be intermingled in the output.
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
|
||||
n, err := l.out.Write(line)
|
||||
if dumpTrace {
|
||||
n, err = l.out.Write(debug.Stack())
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
// We also implement a Write() method on our Logger type so that it satisfies the
|
||||
// io.Writer interface. This writes a log entry at the ERROR level with no additional
|
||||
// properties.
|
||||
func (l *Logger) Write(message []byte) (n int, err error) {
|
||||
return l.print(LevelError, string(message), nil)
|
||||
}
|
|
@ -1,124 +0,0 @@
|
|||
package logger
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func init() {
|
||||
IncludeTrace = true
|
||||
}
|
||||
|
||||
var lastWrite = []byte{}
|
||||
|
||||
type testLogRecorder struct {
|
||||
t *testing.T
|
||||
}
|
||||
|
||||
func (tlr testLogRecorder) Write(p []byte) (n int, err error) {
|
||||
lastWrite = p
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
type logEntry struct {
|
||||
Level string `json:"level"`
|
||||
Message string `json:"message"`
|
||||
Props *Props `json:"properties"`
|
||||
}
|
||||
|
||||
func (lr *logEntry) Unmarshal(t *testing.T, jbytes []byte) {
|
||||
err := json.Unmarshal(jbytes, lr)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_LevelString(t *testing.T) {
|
||||
assert.Equal(t, "DEBUG", LevelDebug.String())
|
||||
assert.Equal(t, "INFO", LevelInfo.String())
|
||||
assert.Equal(t, "ERROR", LevelError.String())
|
||||
assert.Equal(t, "FATAL", LevelFatal.String())
|
||||
assert.Equal(t, "", LevelOff.String())
|
||||
}
|
||||
|
||||
func Test_NewLogger(t *testing.T) {
|
||||
logRecorder := testLogRecorder{t: t}
|
||||
|
||||
logger := New(logRecorder, LevelInfo)
|
||||
assert.NotNil(t, logger)
|
||||
}
|
||||
|
||||
func getTestLogger(t *testing.T, level Level) *Logger {
|
||||
logRecorder := testLogRecorder{t: t}
|
||||
|
||||
logger := New(logRecorder, level)
|
||||
assert.NotNil(t, logger)
|
||||
|
||||
return logger
|
||||
}
|
||||
|
||||
func checkLastEntry(t *testing.T, level Level, message string, props *Props) {
|
||||
t.Helper()
|
||||
entry := &logEntry{}
|
||||
entry.Unmarshal(t, lastWrite)
|
||||
|
||||
assert.Equal(t, level.String(), entry.Level)
|
||||
assert.Equal(t, message, entry.Message)
|
||||
assert.Equal(t, props, entry.Props)
|
||||
|
||||
}
|
||||
|
||||
func Test_LoggerDebug(t *testing.T) {
|
||||
lgr := getTestLogger(t, LevelDebug)
|
||||
|
||||
lgr.Debug("Test Debug", Props{"Hello": "World"})
|
||||
checkLastEntry(t, LevelDebug, "Test Debug", &Props{"Hello": "World"})
|
||||
|
||||
lastWrite = []byte{}
|
||||
}
|
||||
|
||||
func Test_LoggerInfo(t *testing.T) {
|
||||
lgr := getTestLogger(t, LevelInfo)
|
||||
|
||||
lgr.Info("Test Info", Props{"Hello": "World"})
|
||||
checkLastEntry(t, LevelInfo, "Test Info", &Props{"Hello": "World"})
|
||||
lastWrite = []byte{}
|
||||
|
||||
}
|
||||
|
||||
func Test_LoggerError(t *testing.T) {
|
||||
lgr := getTestLogger(t, LevelError)
|
||||
|
||||
myerror := errors.New("Test Error")
|
||||
|
||||
lgr.Error(myerror, Props{"Hello": "World"})
|
||||
checkLastEntry(t, LevelError, "Test Error", &Props{"Hello": "World"})
|
||||
lastWrite = []byte{}
|
||||
|
||||
}
|
||||
|
||||
func Test_LoggerLevelScale(t *testing.T) {
|
||||
lgr := getTestLogger(t, LevelInfo)
|
||||
lastWrite = []byte{}
|
||||
lgr.Debug("Test Debug", Props{"Hello": "World"})
|
||||
|
||||
assert.Equal(t, []byte{}, lastWrite)
|
||||
|
||||
lgr = getTestLogger(t, LevelError)
|
||||
lastWrite = []byte{}
|
||||
lgr.Info("Test Debug", Props{"Hello": "World"})
|
||||
lgr.Debug("Test Debug", Props{"Hello": "World"})
|
||||
|
||||
assert.Equal(t, []byte{}, lastWrite)
|
||||
|
||||
lgr = getTestLogger(t, LevelFatal)
|
||||
|
||||
lgr.Info("Test Debug", Props{"Hello": "World"})
|
||||
lgr.Debug("Test Debug", Props{"Hello": "World"})
|
||||
lgr.Error(errors.New("Test Error"), Props{"Hello": "World"})
|
||||
|
||||
assert.Equal(t, []byte{}, lastWrite)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue