Documentation ¶
Index ¶
- func PrintJson(v interface{})
- type Alarm
- type Fields
- func (f *Fields) Debug(args ...interface{}) bool
- func (f *Fields) Debugf(format string, args ...interface{})
- func (f *Fields) Error(args ...interface{})
- func (f *Fields) Errorf(format string, args ...interface{})
- func (f *Fields) Fatal(args ...interface{})
- func (f *Fields) Fatalf(format string, args ...interface{})
- func (f *Fields) Info(args ...interface{}) bool
- func (f *Fields) Infof(format string, args ...interface{})
- func (f *Fields) Panic(args ...interface{})
- func (f *Fields) Panicf(format string, args ...interface{})
- func (f *Fields) Recover()
- func (f *Fields) With(key string, value interface{}) *Fields
- type Formatter
- type Level
- type Logger
- func (l *Logger) Debug(args ...interface{}) bool
- func (l *Logger) Debugf(format string, args ...interface{})
- func (l *Logger) Error(args ...interface{})
- func (l *Logger) Errorf(format string, args ...interface{})
- func (l *Logger) Fatal(args ...interface{})
- func (l *Logger) Fatalf(format string, args ...interface{})
- func (l *Logger) Info(args ...interface{}) bool
- func (l *Logger) Infof(format string, args ...interface{})
- func (l *Logger) Panic(args ...interface{})
- func (l *Logger) Panicf(format string, args ...interface{})
- func (l *Logger) Record(workFunc func(context.Context) error, recoverFunc func(), ...)
- func (l *Logger) RecordWithContext(ctx context.Context, workFunc func(context.Context) error, recoverFunc func(), ...)
- func (l *Logger) Recover()
- func (l *Logger) Set(key string, value interface{}) *Logger
- func (l *Logger) SetAlarm(alarm Alarm) *Logger
- func (l *Logger) SetLevel(level Level) *Logger
- func (l *Logger) SetMachineIP() *Logger
- func (l *Logger) SetPid() *Logger
- func (l *Logger) With(key string, value interface{}) *Fields
- func (l *Logger) WithTracer(t *tracer.Tracer) *Fields
Examples ¶
- Fields.Debug
- Fields.Debugf
- Fields.Error
- Fields.Errorf
- Fields.Fatal
- Fields.Fatalf
- Fields.Info
- Fields.Infof
- Fields.Panic
- Fields.Panicf
- Fields.Recover
- Fields.With
- Level.String
- Logger (DoAlarm_1)
- Logger (DoAlarm_2)
- Logger (Format_1)
- Logger (Format_2)
- Logger (GetFields_1)
- Logger (Output)
- Logger (Output_1)
- Logger (Output_2)
- Logger (Output_3)
- Logger.SetLevel
- Logger.WithTracer
- New
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Fields ¶
type Fields struct { *Logger // contains filtered or unexported fields }
func (*Fields) Debug ¶
Example ¶
writer := bytes.NewBuffer(nil) New(writer).SetLevel(Debug).With("key", "value").With("key2", "value2").Debug(`the `, `message`) fmt.Println(strings.HasSuffix(writer.String(), `,"key":"value","key2":"value2","level":"debug",`+machineName+`"msg":"the message"} `))
Output: true
func (*Fields) Debugf ¶
Example ¶
writer := bytes.NewBuffer(nil) New(writer).SetLevel(Debug).With("key", "value").With("key2", "value2"). Debugf("%s %s", `the`, `message`) fmt.Println(strings.HasSuffix(writer.String(), `,"key":"value","key2":"value2","level":"debug",`+machineName+`"msg":"the message"} `))
Output: true
func (*Fields) Error ¶
func (f *Fields) Error(args ...interface{})
Example ¶
writer := bytes.NewBuffer(nil) New(writer).With("key", "value").With("key2", "value2").Error(`the `, `message`) fmt.Println(strings.Contains(writer.String(), `,"key":"value","key2":"value2","level":"error",`+machineName+`"msg":"the message",`+ `"stack":"gitee.com/go-better/dev/debug/logger.ExampleFields_Error\n\t`, ))
Output: true
func (*Fields) Errorf ¶
Example ¶
writer := bytes.NewBuffer(nil) New(writer).With("key", "value").With("key2", "value2").Errorf("%s %s", `the`, `message`) fmt.Println(strings.Contains(writer.String(), `,"key":"value","key2":"value2","level":"error",`+machineName+`"msg":"the message",`+ `"stack":"gitee.com/go-better/dev/debug/logger.ExampleFields_Errorf\n\t`, ))
Output: true
func (*Fields) Fatal ¶
func (f *Fields) Fatal(args ...interface{})
Example ¶
var exitStatus int exitFunc = func(status int) { exitStatus = status } defer func() { exitFunc = os.Exit }() writer := bytes.NewBuffer(nil) New(writer).With("key", "value").With("key2", "value2").Fatal("the message") if exitStatus != 1 { fmt.Printf("unexpected exit status: %d", exitStatus) return } fmt.Println(strings.Contains(writer.String(), `,"key":"value","key2":"value2","level":"fatal",`+machineName+`"msg":"the message",`+ `"stack":"gitee.com/go-better/dev/debug/logger.ExampleFields_Fatal\n\t`, ))
Output: true
func (*Fields) Fatalf ¶
Example ¶
var exitStatus int exitFunc = func(status int) { exitStatus = status } defer func() { exitFunc = os.Exit }() writer := bytes.NewBuffer(nil) New(writer).With("key", "value").With("key2", "value2").Fatalf("%s %s", "the", "message") if exitStatus != 1 { fmt.Printf("unexpected exit status: %d", exitStatus) return } fmt.Println(strings.Contains(writer.String(), `,"key":"value","key2":"value2","level":"fatal",`+machineName+`"msg":"the message",`+ `"stack":"gitee.com/go-better/dev/debug/logger.ExampleFields_Fatalf\n\t`, ))
Output: true
func (*Fields) Info ¶
Example ¶
writer := bytes.NewBuffer(nil) New(writer).With("key", "value").With("key2", "value2").Info(`the `, `message`) fmt.Println(strings.HasSuffix(writer.String(), `,"key":"value","key2":"value2","level":"info",`+machineName+`"msg":"the message"} `))
Output: true
func (*Fields) Infof ¶
Example ¶
writer := bytes.NewBuffer(nil) New(writer).With("key", "value").With("key2", "value2").Infof("%s %s", `the`, `message`) fmt.Println(strings.HasSuffix(writer.String(), `,"key":"value","key2":"value2","level":"info",`+machineName+`"msg":"the message"} `))
Output: true
func (*Fields) Panic ¶
func (f *Fields) Panic(args ...interface{})
Example ¶
writer := bytes.NewBuffer(nil) defer func() { err := recover() if err != "the message" { fmt.Printf("unexpected err: %v", err) return } fmt.Println(strings.Contains(writer.String(), `,"key":"value","key2":"value2","level":"panic",`+machineName+`"msg":"the message",`+ `"stack":"gitee.com/go-better/dev/debug/logger.ExampleFields_Panic\n\t`, )) }() New(writer).With("key", "value").With("key2", "value2").Panic("the message")
Output: true
func (*Fields) Panicf ¶
Example ¶
writer := bytes.NewBuffer(nil) defer func() { err := recover() if err != "the message" { fmt.Printf("unexpected err: %v", err) return } fmt.Println(strings.Contains(writer.String(), `,"key":"value","key2":"value2","level":"panic",`+machineName+`"msg":"the message",`+ `"stack":"gitee.com/go-better/dev/debug/logger.ExampleFields_Panicf\n\t`, )) }() New(writer).With("key", "value").With("key2", "value2").Panicf("%s %s", "the", "message")
Output: true
func (*Fields) Recover ¶
func (f *Fields) Recover()
Example ¶
writer := bytes.NewBuffer(nil) func() { defer New(writer).With("key", "value").With("key2", "value2").Recover() panic("the message") }() fmt.Println(strings.Contains(writer.String(), `,"key":"value","key2":"value2","level":"recover",`+machineName+`"msg":"the message",`+ `"stack":"gitee.com/go-better/dev/debug/logger.(*Fields).Recover\n\t`, ))
Output: true
func (*Fields) With ¶
don't use (level, at, msg, stack, duration) as key, they will be overwritten.
Example ¶
writer := bytes.NewBuffer(nil) New(writer).With("key", "value").With("key2", "value2").Info(`the `, `message`) str := writer.String() fmt.Println(strings.HasSuffix( str, `,"key":"value","key2":"value2","level":"info",`+machineName+`"msg":"the message"} `))
Output: true
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Example (DoAlarm_1) ¶
writer, alarm := bytes.NewBuffer(nil), &testAlarm{} logger := New(writer) logger.SetAlarm(alarm) logger.doAlarm(Panic, nil) fmt.Println(alarm.title, alarm.content)
Output: null
Example (DoAlarm_2) ¶
writer := bytes.NewBuffer(nil) logger := New(writer) var mapIn = make(map[interface{}]interface{}) logger.doAlarm(Panic, map[string]interface{}{"test": mapIn}) fmt.Println(strings.Contains(writer.String(), `"level":"error",`+machineName+`"msg":"logger format: json: unsupported type: map[interface {}]interface {}`, ))
Output: true
Example (Format_1) ¶
writer, alarm := bytes.NewBuffer(nil), &testAlarm{} logger := New(writer) logger.SetAlarm(alarm) fmt.Println(string(logger.format(map[string]interface{}{"key": true}, true)))
Output: { "key": true }
Example (Format_2) ¶
writer := bytes.NewBuffer(nil) logger := New(writer) var testIn = make(map[interface{}]interface{}) var fields = make(map[string]interface{}) fields[`key`] = testIn fmt.Println(logger.format(fields, true) == nil)
Output: true
Example (GetFields_1) ¶
writer := bytes.NewBuffer(nil) logger := New(writer) logger.fields = map[string]interface{}{"key": true, "key1": "value1"} got := logger.getFields(Recover, "message", nil) fmt.Println(got[`level`], got[`msg`], got[`key`], got[`key1`])
Output: recover message true value1
Example (Output) ¶
New(os.Stderr).output(Info, "", nil)
Output:
Example (Output_1) ¶
writer := bytes.NewBuffer(nil) New(writer).SetPid().output(Info, "message", map[string]interface{}{"key": "value"}) expect := fmt.Sprintf(`,"key":"value","level":"info",`+machineName+`"msg":"message","pid":%d}`, os.Getpid()) fmt.Println(strings.Contains(writer.String(), expect))
Output: true
Example (Output_2) ¶
writer := bytes.NewBuffer(nil) New(writer).output(Error, "message", map[string]interface{}{"key": "value"}) fmt.Println(strings.Contains(writer.String(), `,"key":"value","level":"error",`+machineName+`"msg":"message"}`))
Output: true
Example (Output_3) ¶
writer := bytes.NewBuffer(nil) logger := New(writer) logger.writer = os.Stderr logger.output(Panic, "message", map[string]interface{}{"key": "value"}) fmt.Println(writer.String())
Output:
func (*Logger) RecordWithContext ¶
func (*Logger) Set ¶
Set a default field by key and value. Don't use "level", "at", "msg", "stack", "duration" they will be overwritten.
func (*Logger) SetLevel ¶
Example ¶
logger := New(nil) logger.SetLevel(Panic) fmt.Println(logger.level) var level Level = 10 logger.SetLevel(level) fmt.Println(logger.level) logger.SetLevel(Info) fmt.Println(logger.level) // Ouput: // panic // debug // info
Output:
func (*Logger) WithTracer ¶
Example ¶
logger := New(nil) var t = &tracer.Tracer{ At: time.Now(), Children: []*tracer.Tracer{{At: time.Now()}}, Tags: map[string]interface{}{"key": "value"}, Logs: []string{"the message"}, } got := logger.WithTracer(t) expect := &Fields{ Logger: logger, data: map[string]interface{}{ "at": t.At, "duration": t.Duration, "children": t.Children, "tags": t.Tags, "logs": []string{"the message"}, }, } fmt.Println(reflect.DeepEqual(got, expect))
Output: true
Click to show internal directories.
Click to hide internal directories.