1package log
2
3import (
4 "log"
5 "strings"
6)
7
8type stdLogWriter struct {
9 l *Logger
10 opt *StandardLogOptions
11}
12
13func (l *stdLogWriter) Write(p []byte) (n int, err error) {
14 str := strings.TrimSuffix(string(p), "\n")
15
16 if l.opt != nil {
17 switch l.opt.ForceLevel {
18 case DebugLevel:
19 l.l.Debug(str)
20 case InfoLevel:
21 l.l.Info(str)
22 case WarnLevel:
23 l.l.Warn(str)
24 case ErrorLevel:
25 l.l.Error(str)
26 }
27 } else {
28 switch {
29 case strings.HasPrefix(str, "DEBUG"):
30 l.l.Debug(strings.TrimSpace(str[5:]))
31 case strings.HasPrefix(str, "INFO"):
32 l.l.Info(strings.TrimSpace(str[4:]))
33 case strings.HasPrefix(str, "WARN"):
34 l.l.Warn(strings.TrimSpace(str[4:]))
35 case strings.HasPrefix(str, "ERROR"):
36 l.l.Error(strings.TrimSpace(str[5:]))
37 case strings.HasPrefix(str, "ERR"):
38 l.l.Error(strings.TrimSpace(str[3:]))
39 default:
40 l.l.Info(str)
41 }
42 }
43
44 return len(p), nil
45}
46
47// StandardLogOptions can be used to configure the standard log adapter.
48type StandardLogOptions struct {
49 ForceLevel Level
50}
51
52// StandardLog returns a standard logger from Logger. The returned logger
53// can infer log levels from message prefix. Expected prefixes are DEBUG, INFO,
54// WARN, ERROR, and ERR.
55func (l *Logger) StandardLog(opts ...StandardLogOptions) *log.Logger {
56 nl := l.With()
57 // The caller stack is
58 // log.Printf() -> l.Output() -> l.out.Write(stdLogger.Write)
59 nl.callerOffset += 3
60 sl := &stdLogWriter{
61 l: nl,
62 }
63 if len(opts) > 0 {
64 sl.opt = &opts[0]
65 }
66 return log.New(sl, "", 0)
67}