github.com/bytedance/gopkg@v0.0.0-20240514070511-01b2cbcf35e1/util/logger/default.go (about) 1 // Copyright 2021 ByteDance Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package logger 16 17 import ( 18 "context" 19 "fmt" 20 "log" 21 "os" 22 ) 23 24 var ( 25 _ Logger = (*localLogger)(nil) 26 ) 27 28 // SetDefaultLogger sets the default logger. 29 // This is not concurrency safe, which means it should only be called during init. 30 func SetDefaultLogger(l Logger) { 31 if l == nil { 32 panic("logger must not be nil") 33 } 34 defaultLogger = l 35 } 36 37 var defaultLogger Logger = &localLogger{ 38 logger: log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile|log.Lmicroseconds), 39 } 40 41 type localLogger struct { 42 logger *log.Logger 43 } 44 45 func (ll *localLogger) logf(lv Level, format *string, v ...interface{}) { 46 if level > lv { 47 return 48 } 49 msg := lv.toString() 50 if format != nil { 51 msg += fmt.Sprintf(*format, v...) 52 } else { 53 msg += fmt.Sprint(v...) 54 } 55 ll.logger.Output(3, msg) 56 if lv == LevelFatal { 57 os.Exit(1) 58 } 59 } 60 61 func (ll *localLogger) Fatal(v ...interface{}) { 62 ll.logf(LevelFatal, nil, v...) 63 } 64 65 func (ll *localLogger) Error(v ...interface{}) { 66 ll.logf(LevelError, nil, v...) 67 } 68 69 func (ll *localLogger) Warn(v ...interface{}) { 70 ll.logf(LevelWarn, nil, v...) 71 } 72 73 func (ll *localLogger) Notice(v ...interface{}) { 74 ll.logf(LevelNotice, nil, v...) 75 } 76 77 func (ll *localLogger) Info(v ...interface{}) { 78 ll.logf(LevelInfo, nil, v...) 79 } 80 81 func (ll *localLogger) Debug(v ...interface{}) { 82 ll.logf(LevelDebug, nil, v...) 83 } 84 85 func (ll *localLogger) Trace(v ...interface{}) { 86 ll.logf(LevelTrace, nil, v...) 87 } 88 89 func (ll *localLogger) Fatalf(format string, v ...interface{}) { 90 ll.logf(LevelFatal, &format, v...) 91 } 92 93 func (ll *localLogger) Errorf(format string, v ...interface{}) { 94 ll.logf(LevelError, &format, v...) 95 } 96 97 func (ll *localLogger) Warnf(format string, v ...interface{}) { 98 ll.logf(LevelWarn, &format, v...) 99 } 100 101 func (ll *localLogger) Noticef(format string, v ...interface{}) { 102 ll.logf(LevelNotice, &format, v...) 103 } 104 105 func (ll *localLogger) Infof(format string, v ...interface{}) { 106 ll.logf(LevelInfo, &format, v...) 107 } 108 109 func (ll *localLogger) Debugf(format string, v ...interface{}) { 110 ll.logf(LevelDebug, &format, v...) 111 } 112 113 func (ll *localLogger) Tracef(format string, v ...interface{}) { 114 ll.logf(LevelTrace, &format, v...) 115 } 116 117 func (ll *localLogger) CtxFatalf(ctx context.Context, format string, v ...interface{}) { 118 ll.logf(LevelFatal, &format, v...) 119 } 120 121 func (ll *localLogger) CtxErrorf(ctx context.Context, format string, v ...interface{}) { 122 ll.logf(LevelError, &format, v...) 123 } 124 125 func (ll *localLogger) CtxWarnf(ctx context.Context, format string, v ...interface{}) { 126 ll.logf(LevelWarn, &format, v...) 127 } 128 129 func (ll *localLogger) CtxNoticef(ctx context.Context, format string, v ...interface{}) { 130 ll.logf(LevelNotice, &format, v...) 131 } 132 133 func (ll *localLogger) CtxInfof(ctx context.Context, format string, v ...interface{}) { 134 ll.logf(LevelInfo, &format, v...) 135 } 136 137 func (ll *localLogger) CtxDebugf(ctx context.Context, format string, v ...interface{}) { 138 ll.logf(LevelDebug, &format, v...) 139 } 140 141 func (ll *localLogger) CtxTracef(ctx context.Context, format string, v ...interface{}) { 142 ll.logf(LevelTrace, &format, v...) 143 }