| 1 | // mauLogger - A logger for Go programs |
| 2 | // Copyright (c) 2016-2021 Tulir Asokan |
| 3 | // |
| 4 | // This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | // License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | // file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 | |
| 8 | package maulogger |
| 9 | |
| 10 | import ( |
| 11 | "fmt" |
| 12 | ) |
| 13 | |
| 14 | type Sublogger struct { |
| 15 | topLevel *BasicLogger |
| 16 | parent Logger |
| 17 | Module string |
| 18 | DefaultLevel Level |
| 19 | metadata map[string]interface{} |
| 20 | } |
| 21 | |
| 22 | // Subm creates a Sublogger |
| 23 | func (log *BasicLogger) Subm(module string, metadata map[string]interface{}) Logger { |
| 24 | return &Sublogger{ |
| 25 | topLevel: log, |
| 26 | parent: log, |
| 27 | Module: module, |
| 28 | DefaultLevel: LevelInfo, |
| 29 | metadata: metadata, |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func (log *BasicLogger) Sub(module string) Logger { |
| 34 | return log.Subm(module, map[string]interface{}{}) |
| 35 | } |
| 36 | |
| 37 | // WithDefaultLevel creates a Sublogger with the same Module but different DefaultLevel |
| 38 | func (log *BasicLogger) WithDefaultLevel(lvl Level) Logger { |
| 39 | return log.DefaultSub.WithDefaultLevel(lvl) |
| 40 | } |
| 41 | |
| 42 | func (log *Sublogger) GetParent() Logger { |
| 43 | return log.parent |
| 44 | } |
| 45 | |
| 46 | // Sub creates a Sublogger |
| 47 | func (log *Sublogger) Subm(module string, metadata map[string]interface{}) Logger { |
| 48 | if len(module) > 0 { |
| 49 | module = fmt.Sprintf("%s/%s", log.Module, module) |
| 50 | } else { |
| 51 | module = log.Module |
| 52 | } |
| 53 | |
| 54 | return &Sublogger{ |
| 55 | topLevel: log.topLevel, |
| 56 | parent: log, |
| 57 | Module: module, |
| 58 | DefaultLevel: log.DefaultLevel, |
| 59 | metadata: metadata, |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func (log *Sublogger) Sub(module string) Logger { |
| 64 | return log.Subm(module, map[string]interface{}{}) |
| 65 | } |
| 66 | |
| 67 | // WithDefaultLevel creates a Sublogger with the same Module but different DefaultLevel |
| 68 | func (log *Sublogger) WithDefaultLevel(lvl Level) Logger { |
| 69 | return &Sublogger{ |
| 70 | topLevel: log.topLevel, |
| 71 | parent: log.parent, |
| 72 | Module: log.Module, |
| 73 | DefaultLevel: lvl, |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // SetModule changes the module name of this Sublogger |
| 78 | func (log *Sublogger) SetModule(mod string) { |
| 79 | log.Module = mod |
| 80 | } |
| 81 | |
| 82 | // SetDefaultLevel changes the default logging level of this Sublogger |
| 83 | func (log *Sublogger) SetDefaultLevel(lvl Level) { |
| 84 | log.DefaultLevel = lvl |
| 85 | } |
| 86 | |
| 87 | // SetParent changes the parent of this Sublogger |
| 88 | func (log *Sublogger) SetParent(parent *BasicLogger) { |
| 89 | log.topLevel = parent |
| 90 | } |
| 91 | |
| 92 | //Write ... |
| 93 | func (log *Sublogger) Write(p []byte) (n int, err error) { |
| 94 | log.topLevel.Raw(log.DefaultLevel, log.metadata, log.Module, string(p)) |
| 95 | return len(p), nil |
| 96 | } |
| 97 | |
| 98 | // Log formats the given parts with fmt.Sprint and logs the result with the given level |
| 99 | func (log *Sublogger) Log(level Level, parts ...interface{}) { |
| 100 | log.topLevel.Raw(level, log.metadata, log.Module, fmt.Sprint(parts...)) |
| 101 | } |
| 102 | |
| 103 | // Logln formats the given parts with fmt.Sprintln and logs the result with the given level |
| 104 | func (log *Sublogger) Logln(level Level, parts ...interface{}) { |
| 105 | log.topLevel.Raw(level, log.metadata, log.Module, fmt.Sprintln(parts...)) |
| 106 | } |
| 107 | |
| 108 | // Logf formats the given message and args with fmt.Sprintf and logs the result with the given level |
| 109 | func (log *Sublogger) Logf(level Level, message string, args ...interface{}) { |
| 110 | log.topLevel.Raw(level, log.metadata, log.Module, fmt.Sprintf(message, args...)) |
| 111 | } |
| 112 | |
| 113 | // Logfln formats the given message and args with fmt.Sprintf, appends a newline and logs the result with the given level |
| 114 | func (log *Sublogger) Logfln(level Level, message string, args ...interface{}) { |
| 115 | log.topLevel.Raw(level, log.metadata, log.Module, fmt.Sprintf(message+"\n", args...)) |
| 116 | } |
| 117 | |
| 118 | // Debug formats the given parts with fmt.Sprint and logs the result with the Debug level |
| 119 | func (log *Sublogger) Debug(parts ...interface{}) { |
| 120 | log.topLevel.Raw(LevelDebug, log.metadata, log.Module, fmt.Sprint(parts...)) |
| 121 | } |
| 122 | |
| 123 | // Debugln formats the given parts with fmt.Sprintln and logs the result with the Debug level |
| 124 | func (log *Sublogger) Debugln(parts ...interface{}) { |
| 125 | log.topLevel.Raw(LevelDebug, log.metadata, log.Module, fmt.Sprintln(parts...)) |
| 126 | } |
| 127 | |
| 128 | // Debugf formats the given message and args with fmt.Sprintf and logs the result with the Debug level |
| 129 | func (log *Sublogger) Debugf(message string, args ...interface{}) { |
| 130 | log.topLevel.Raw(LevelDebug, log.metadata, log.Module, fmt.Sprintf(message, args...)) |
| 131 | } |
| 132 | |
| 133 | // Debugfln formats the given message and args with fmt.Sprintf, appends a newline and logs the result with the Debug level |
| 134 | func (log *Sublogger) Debugfln(message string, args ...interface{}) { |
| 135 | log.topLevel.Raw(LevelDebug, log.metadata, log.Module, fmt.Sprintf(message+"\n", args...)) |
| 136 | } |
| 137 | |
| 138 | // Info formats the given parts with fmt.Sprint and logs the result with the Info level |
| 139 | func (log *Sublogger) Info(parts ...interface{}) { |
| 140 | log.topLevel.Raw(LevelInfo, log.metadata, log.Module, fmt.Sprint(parts...)) |
| 141 | } |
| 142 | |
| 143 | // Infoln formats the given parts with fmt.Sprintln and logs the result with the Info level |
| 144 | func (log *Sublogger) Infoln(parts ...interface{}) { |
| 145 | log.topLevel.Raw(LevelInfo, log.metadata, log.Module, fmt.Sprintln(parts...)) |
| 146 | } |
| 147 | |
| 148 | // Infof formats the given message and args with fmt.Sprintf and logs the result with the Info level |
| 149 | func (log *Sublogger) Infof(message string, args ...interface{}) { |
| 150 | log.topLevel.Raw(LevelInfo, log.metadata, log.Module, fmt.Sprintf(message, args...)) |
| 151 | } |
| 152 | |
| 153 | // Infofln formats the given message and args with fmt.Sprintf, appends a newline and logs the result with the Info level |
| 154 | func (log *Sublogger) Infofln(message string, args ...interface{}) { |
| 155 | log.topLevel.Raw(LevelInfo, log.metadata, log.Module, fmt.Sprintf(message+"\n", args...)) |
| 156 | } |
| 157 | |
| 158 | // Warn formats the given parts with fmt.Sprint and logs the result with the Warn level |
| 159 | func (log *Sublogger) Warn(parts ...interface{}) { |
| 160 | log.topLevel.Raw(LevelWarn, log.metadata, log.Module, fmt.Sprint(parts...)) |
| 161 | } |
| 162 | |
| 163 | // Warnln formats the given parts with fmt.Sprintln and logs the result with the Warn level |
| 164 | func (log *Sublogger) Warnln(parts ...interface{}) { |
| 165 | log.topLevel.Raw(LevelWarn, log.metadata, log.Module, fmt.Sprintln(parts...)) |
| 166 | } |
| 167 | |
| 168 | // Warnf formats the given message and args with fmt.Sprintf and logs the result with the Warn level |
| 169 | func (log *Sublogger) Warnf(message string, args ...interface{}) { |
| 170 | log.topLevel.Raw(LevelWarn, log.metadata, log.Module, fmt.Sprintf(message, args...)) |
| 171 | } |
| 172 | |
| 173 | // Warnfln formats the given message and args with fmt.Sprintf, appends a newline and logs the result with the Warn level |
| 174 | func (log *Sublogger) Warnfln(message string, args ...interface{}) { |
| 175 | log.topLevel.Raw(LevelWarn, log.metadata, log.Module, fmt.Sprintf(message+"\n", args...)) |
| 176 | } |
| 177 | |
| 178 | // Error formats the given parts with fmt.Sprint and logs the result with the Error level |
| 179 | func (log *Sublogger) Error(parts ...interface{}) { |
| 180 | log.topLevel.Raw(LevelError, log.metadata, log.Module, fmt.Sprint(parts...)) |
| 181 | } |
| 182 | |
| 183 | // Errorln formats the given parts with fmt.Sprintln and logs the result with the Error level |
| 184 | func (log *Sublogger) Errorln(parts ...interface{}) { |
| 185 | log.topLevel.Raw(LevelError, log.metadata, log.Module, fmt.Sprintln(parts...)) |
| 186 | } |
| 187 | |
| 188 | // Errorf formats the given message and args with fmt.Sprintf and logs the result with the Error level |
| 189 | func (log *Sublogger) Errorf(message string, args ...interface{}) { |
| 190 | log.topLevel.Raw(LevelError, log.metadata, log.Module, fmt.Sprintf(message, args...)) |
| 191 | } |
| 192 | |
| 193 | // Errorfln formats the given message and args with fmt.Sprintf, appends a newline and logs the result with the Error level |
| 194 | func (log *Sublogger) Errorfln(message string, args ...interface{}) { |
| 195 | log.topLevel.Raw(LevelError, log.metadata, log.Module, fmt.Sprintf(message+"\n", args...)) |
| 196 | } |
| 197 | |
| 198 | // Fatal formats the given parts with fmt.Sprint and logs the result with the Fatal level |
| 199 | func (log *Sublogger) Fatal(parts ...interface{}) { |
| 200 | log.topLevel.Raw(LevelFatal, log.metadata, log.Module, fmt.Sprint(parts...)) |
| 201 | } |
| 202 | |
| 203 | // Fatalln formats the given parts with fmt.Sprintln and logs the result with the Fatal level |
| 204 | func (log *Sublogger) Fatalln(parts ...interface{}) { |
| 205 | log.topLevel.Raw(LevelFatal, log.metadata, log.Module, fmt.Sprintln(parts...)) |
| 206 | } |
| 207 | |
| 208 | // Fatalf formats the given message and args with fmt.Sprintf and logs the result with the Fatal level |
| 209 | func (log *Sublogger) Fatalf(message string, args ...interface{}) { |
| 210 | log.topLevel.Raw(LevelFatal, log.metadata, log.Module, fmt.Sprintf(message, args...)) |
| 211 | } |
| 212 | |
| 213 | // Fatalfln formats the given message and args with fmt.Sprintf, appends a newline and logs the result with the Fatal level |
| 214 | func (log *Sublogger) Fatalfln(message string, args ...interface{}) { |
| 215 | log.topLevel.Raw(LevelFatal, log.metadata, log.Module, fmt.Sprintf(message+"\n", args...)) |
| 216 | } |