dogger
/
dogger.go
144 строки · 2.7 Кб
1package dogger2
3import (4"bytes"5"context"6"fmt"7logCtx "github.com/nikwo/dogger/context"8"github.com/nikwo/dogger/format"9"github.com/nikwo/dogger/level"10"github.com/nikwo/dogger/utility"11"io"12"sync"13)
14
15type Logger interface {16WithContext(ctx context.Context) Logger17WithFields(entry string, value interface{}) Logger18Trace(input interface{})19Debug(input interface{})20Info(input interface{})21Warn(input interface{})22Error(input interface{})23}
24
25type logger struct {26lock sync.Mutex27lvl level.Level28ctx logCtx.LogContext29buffer *bytes.Buffer30formatter format.Format31fields map[string]interface{}32}
33
34func newChildLogger() *logger {35l := new(logger)36lockIO()37defer unlockIO()38l.lvl = log.lvl39l.buffer = bytes.NewBuffer([]byte{})40l.formatter = log.formatter41return l42}
43
44func (l *logger) acceptedLevel(lvl level.Level) bool {45return l.lvl <= lvl46}
47
48func createChildLogger(ctx context.Context, lvl level.Level) *logger {49l := newChildLogger()50l.ctx = logCtx.NewLogContext(ctx, lvl, 1)51
52return l53}
54
55func (l *logger) WithContext(ctx context.Context) Logger {56l.ctx = logCtx.NewLogContext(ctx, level.TRACE)57return l58}
59
60func (l *logger) WithFields(entry string, value interface{}) Logger {61l.fields = make(map[string]interface{})62l.fields[entry] = value63return l64}
65
66func (l *logger) Trace(input interface{}) {67l.ctx.SetLevel(level.TRACE)68if !l.acceptedLevel(level.TRACE) {69return70}71
72l.formOutput(input)73l.flush()74}
75
76func (l *logger) Debug(input interface{}) {77l.ctx.SetLevel(level.DEBUG)78if !l.acceptedLevel(level.DEBUG) {79return80}81
82l.formOutput(input)83l.flush()84}
85
86func (l *logger) Info(input interface{}) {87l.ctx.SetLevel(level.INFO)88if !l.acceptedLevel(level.INFO) {89return90}91
92l.formOutput(input)93l.flush()94}
95
96func (l *logger) Warn(input interface{}) {97l.ctx.SetLevel(level.WARN)98if !l.acceptedLevel(level.WARN) {99return100}101
102l.formOutput(input)103l.flush()104}
105
106func (l *logger) Error(input interface{}) {107l.ctx.SetLevel(level.ERROR)108
109if !l.acceptedLevel(level.ERROR) {110return111}112
113l.formOutput(input)114l.flush()115}
116
117func (l *logger) formOutput(input interface{}) {118inlineBuffer := make([]byte, 0, 100)119inlineBuffer = append(inlineBuffer, utility.Bytes(l.formatter.FormatString(l.ctx))...)120for entry, field := range l.fields {121inlineBuffer = append(inlineBuffer, utility.Bytes(fmt.Sprintf(" entry=\"%s\" value=\"%+v\"", entry, field))...)122}123inlineBuffer = append(inlineBuffer, utility.Bytes(fmt.Sprintf(" message=\"%+v\"\n", input))...)124_, _ = l.buffer.Write(inlineBuffer)125}
126
127func (l *logger) flush() {128lockIO()129defer unlockIO()130_, _ = writer.Write(l.buffer.Bytes())131}
132
133var (134log *logger135writer io.Writer136)
137
138func lockIO() {139log.lock.Lock()140}
141
142func unlockIO() {143log.lock.Unlock()144}
145