github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/logger/logger.go (about) 1 // Copyright 2015 Keybase, Inc. All rights reserved. Use of 2 // this source code is governed by the included BSD license. 3 4 package logger 5 6 import ( 7 "log" 8 9 keybase1 "github.com/keybase/client/go/protocol/keybase1" 10 "golang.org/x/net/context" 11 ) 12 13 type ExternalHandler interface { 14 Log(level keybase1.LogLevel, format string, args []interface{}) 15 } 16 17 type BaseLogger interface { 18 Debug(format string, args ...interface{}) 19 Info(format string, args ...interface{}) 20 Warning(format string, args ...interface{}) 21 Error(format string, args ...interface{}) 22 } 23 24 type ContextInterface interface { 25 BaseLogger 26 Ctx() context.Context 27 UpdateContextToLoggerContext(context.Context) ContextInterface 28 } 29 30 type Logger interface { 31 BaseLogger 32 // CDebugf logs a message at debug level, with a context and 33 // formatting args. 34 CDebugf(ctx context.Context, format string, args ...interface{}) 35 // CInfo logs a message at info level, with a context and formatting args. 36 CInfof(ctx context.Context, format string, args ...interface{}) 37 // Notice logs a message at notice level, with formatting args. 38 Notice(format string, args ...interface{}) 39 // CNoticef logs a message at notice level, with a context and 40 // formatting args. 41 CNoticef(ctx context.Context, format string, args ...interface{}) 42 // Warning logs a message at warning level, with formatting args. 43 CWarningf(ctx context.Context, format string, args ...interface{}) 44 // Error logs a message at error level, with formatting args 45 Errorf(format string, args ...interface{}) 46 // CErrorf logs a message at error level, with a context and 47 // formatting args. 48 CErrorf(ctx context.Context, format string, args ...interface{}) 49 // Critical logs a message at critical level, with formatting args. 50 Critical(format string, args ...interface{}) 51 // CCriticalf logs a message at critical level, with a context and 52 // formatting args. 53 CCriticalf(ctx context.Context, format string, args ...interface{}) 54 // Fatalf logs a message at fatal level, with formatting args. 55 Fatalf(format string, args ...interface{}) 56 // Fatalf logs a message at fatal level, with a context and formatting args. 57 CFatalf(ctx context.Context, format string, args ...interface{}) 58 // Profile logs a profile message, with formatting args. 59 Profile(fmts string, arg ...interface{}) 60 61 // Returns a logger that is like the current one, except with 62 // more logging depth added on. 63 CloneWithAddedDepth(depth int) Logger 64 // Configure sets the style, debug level, and filename of the 65 // logger. Output isn't redirected to the file until 66 // the log file rotation is configured. 67 Configure(style string, debug bool, filename string) 68 69 // SetExternalHandler sets a handler that will be called with every log message. 70 SetExternalHandler(handler ExternalHandler) 71 } 72 73 type InternalLogger struct { 74 log *log.Logger 75 } 76 77 func NewInternalLogger(log *log.Logger) *InternalLogger { 78 return &InternalLogger{ 79 log: log, 80 } 81 } 82 83 func (l *InternalLogger) Debug(fmt string, arg ...interface{}) { 84 l.log.Printf(fmt, arg...) 85 } 86 87 func (l *InternalLogger) CDebugf(ctx context.Context, fmt string, 88 arg ...interface{}) { 89 l.log.Printf(prepareString(ctx, fmt), arg...) 90 } 91 92 func (l *InternalLogger) Info(fmt string, arg ...interface{}) { 93 l.log.Printf(keybase1.LogLevel_INFO.String()+fmt, arg...) 94 } 95 96 func (l *InternalLogger) CInfof(ctx context.Context, fmt string, 97 arg ...interface{}) { 98 l.log.Printf(keybase1.LogLevel_INFO.String()+prepareString(ctx, fmt), arg...) 99 } 100 101 func (l *InternalLogger) Notice(fmt string, arg ...interface{}) { 102 l.log.Printf(keybase1.LogLevel_NOTICE.String()+fmt, arg...) 103 } 104 105 func (l *InternalLogger) CNoticef(ctx context.Context, fmt string, 106 arg ...interface{}) { 107 l.log.Printf(keybase1.LogLevel_NOTICE.String()+prepareString(ctx, fmt), arg...) 108 } 109 110 func (l *InternalLogger) Warning(fmt string, arg ...interface{}) { 111 l.log.Printf(keybase1.LogLevel_WARN.String()+fmt, arg...) 112 } 113 114 func (l *InternalLogger) CWarningf(ctx context.Context, fmt string, 115 arg ...interface{}) { 116 l.log.Printf(keybase1.LogLevel_WARN.String()+prepareString(ctx, fmt), arg...) 117 } 118 119 func (l *InternalLogger) Error(fmt string, arg ...interface{}) { 120 l.log.Printf(keybase1.LogLevel_ERROR.String()+fmt, arg...) 121 } 122 123 func (l *InternalLogger) Errorf(fmt string, arg ...interface{}) { 124 l.log.Printf(keybase1.LogLevel_ERROR.String()+fmt, arg...) 125 } 126 127 func (l *InternalLogger) CErrorf(ctx context.Context, fmt string, 128 arg ...interface{}) { 129 l.log.Printf(keybase1.LogLevel_ERROR.String()+prepareString(ctx, fmt), arg...) 130 } 131 132 func (l *InternalLogger) Critical(fmt string, arg ...interface{}) { 133 l.log.Printf(keybase1.LogLevel_CRITICAL.String()+fmt, arg...) 134 } 135 136 func (l *InternalLogger) CCriticalf(ctx context.Context, fmt string, 137 arg ...interface{}) { 138 l.log.Printf(keybase1.LogLevel_CRITICAL.String()+prepareString(ctx, fmt), arg...) 139 } 140 141 func (l *InternalLogger) Fatalf(fmt string, arg ...interface{}) { 142 l.log.Fatalf(fmt, arg...) 143 } 144 145 func (l *InternalLogger) CFatalf(ctx context.Context, fmt string, 146 arg ...interface{}) { 147 l.log.Fatalf(prepareString(ctx, fmt), arg...) 148 } 149 150 func (l *InternalLogger) Profile(fmts string, arg ...interface{}) { 151 l.log.Printf(keybase1.LogLevel_DEBUG.String()+fmts, arg...) 152 } 153 func (l *InternalLogger) Configure(style string, debug bool, filename string) {} 154 func (l *InternalLogger) SetExternalHandler(handler ExternalHandler) {} 155 func (l *InternalLogger) CloneWithAddedDepth(depth int) Logger { 156 return l 157 }