Skip to content

Commit a6668e7

Browse files
author
Lisa Ugray
committed
Add Generic Log functions with level via argument
1 parent 44067ab commit a6668e7

File tree

2 files changed

+77
-175
lines changed

2 files changed

+77
-175
lines changed

entry.go

Lines changed: 37 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -240,162 +240,136 @@ func (entry *Entry) write() {
240240
}
241241
}
242242

243-
func (entry *Entry) Trace(args ...interface{}) {
244-
if entry.Logger.IsLevelEnabled(TraceLevel) {
245-
entry.log(TraceLevel, fmt.Sprint(args...))
243+
func (entry *Entry) Log(level Level, args ...interface{}) {
244+
if entry.Logger.IsLevelEnabled(level) {
245+
entry.log(level, fmt.Sprint(args...))
246246
}
247247
}
248248

249+
func (entry *Entry) Trace(args ...interface{}) {
250+
entry.Log(TraceLevel, args...)
251+
}
252+
249253
func (entry *Entry) Debug(args ...interface{}) {
250-
if entry.Logger.IsLevelEnabled(DebugLevel) {
251-
entry.log(DebugLevel, fmt.Sprint(args...))
252-
}
254+
entry.Log(DebugLevel, args...)
253255
}
254256

255257
func (entry *Entry) Print(args ...interface{}) {
256258
entry.Info(args...)
257259
}
258260

259261
func (entry *Entry) Info(args ...interface{}) {
260-
if entry.Logger.IsLevelEnabled(InfoLevel) {
261-
entry.log(InfoLevel, fmt.Sprint(args...))
262-
}
262+
entry.Log(InfoLevel, args...)
263263
}
264264

265265
func (entry *Entry) Warn(args ...interface{}) {
266-
if entry.Logger.IsLevelEnabled(WarnLevel) {
267-
entry.log(WarnLevel, fmt.Sprint(args...))
268-
}
266+
entry.Log(WarnLevel, args...)
269267
}
270268

271269
func (entry *Entry) Warning(args ...interface{}) {
272270
entry.Warn(args...)
273271
}
274272

275273
func (entry *Entry) Error(args ...interface{}) {
276-
if entry.Logger.IsLevelEnabled(ErrorLevel) {
277-
entry.log(ErrorLevel, fmt.Sprint(args...))
278-
}
274+
entry.Log(ErrorLevel, args...)
279275
}
280276

281277
func (entry *Entry) Fatal(args ...interface{}) {
282-
if entry.Logger.IsLevelEnabled(FatalLevel) {
283-
entry.log(FatalLevel, fmt.Sprint(args...))
284-
}
278+
entry.Log(FatalLevel, args...)
285279
entry.Logger.Exit(1)
286280
}
287281

288282
func (entry *Entry) Panic(args ...interface{}) {
289-
if entry.Logger.IsLevelEnabled(PanicLevel) {
290-
entry.log(PanicLevel, fmt.Sprint(args...))
291-
}
283+
entry.Log(PanicLevel, args...)
292284
panic(fmt.Sprint(args...))
293285
}
294286

295287
// Entry Printf family functions
296288

289+
func (entry *Entry) Logf(level Level, format string, args ...interface{}) {
290+
entry.Log(level, fmt.Sprintf(format, args...))
291+
}
292+
297293
func (entry *Entry) Tracef(format string, args ...interface{}) {
298-
if entry.Logger.IsLevelEnabled(TraceLevel) {
299-
entry.Trace(fmt.Sprintf(format, args...))
300-
}
294+
entry.Logf(TraceLevel, format, args...)
301295
}
302296

303297
func (entry *Entry) Debugf(format string, args ...interface{}) {
304-
if entry.Logger.IsLevelEnabled(DebugLevel) {
305-
entry.Debug(fmt.Sprintf(format, args...))
306-
}
298+
entry.Logf(DebugLevel, format, args...)
307299
}
308300

309301
func (entry *Entry) Infof(format string, args ...interface{}) {
310-
if entry.Logger.IsLevelEnabled(InfoLevel) {
311-
entry.Info(fmt.Sprintf(format, args...))
312-
}
302+
entry.Logf(InfoLevel, format, args...)
313303
}
314304

315305
func (entry *Entry) Printf(format string, args ...interface{}) {
316306
entry.Infof(format, args...)
317307
}
318308

319309
func (entry *Entry) Warnf(format string, args ...interface{}) {
320-
if entry.Logger.IsLevelEnabled(WarnLevel) {
321-
entry.Warn(fmt.Sprintf(format, args...))
322-
}
310+
entry.Logf(WarnLevel, format, args...)
323311
}
324312

325313
func (entry *Entry) Warningf(format string, args ...interface{}) {
326314
entry.Warnf(format, args...)
327315
}
328316

329317
func (entry *Entry) Errorf(format string, args ...interface{}) {
330-
if entry.Logger.IsLevelEnabled(ErrorLevel) {
331-
entry.Error(fmt.Sprintf(format, args...))
332-
}
318+
entry.Logf(ErrorLevel, format, args...)
333319
}
334320

335321
func (entry *Entry) Fatalf(format string, args ...interface{}) {
336-
if entry.Logger.IsLevelEnabled(FatalLevel) {
337-
entry.Fatal(fmt.Sprintf(format, args...))
338-
}
322+
entry.Logf(FatalLevel, format, args...)
339323
entry.Logger.Exit(1)
340324
}
341325

342326
func (entry *Entry) Panicf(format string, args ...interface{}) {
343-
if entry.Logger.IsLevelEnabled(PanicLevel) {
344-
entry.Panic(fmt.Sprintf(format, args...))
345-
}
327+
entry.Logf(PanicLevel, format, args...)
346328
}
347329

348330
// Entry Println family functions
349331

350-
func (entry *Entry) Traceln(args ...interface{}) {
351-
if entry.Logger.IsLevelEnabled(TraceLevel) {
352-
entry.Trace(entry.sprintlnn(args...))
332+
func (entry *Entry) Logln(level Level, args ...interface{}) {
333+
if entry.Logger.IsLevelEnabled(level) {
334+
entry.Log(level, entry.sprintlnn(args...))
353335
}
354336
}
355337

338+
func (entry *Entry) Traceln(args ...interface{}) {
339+
entry.Logln(TraceLevel, args...)
340+
}
341+
356342
func (entry *Entry) Debugln(args ...interface{}) {
357-
if entry.Logger.IsLevelEnabled(DebugLevel) {
358-
entry.Debug(entry.sprintlnn(args...))
359-
}
343+
entry.Logln(DebugLevel, args...)
360344
}
361345

362346
func (entry *Entry) Infoln(args ...interface{}) {
363-
if entry.Logger.IsLevelEnabled(InfoLevel) {
364-
entry.Info(entry.sprintlnn(args...))
365-
}
347+
entry.Logln(InfoLevel, args...)
366348
}
367349

368350
func (entry *Entry) Println(args ...interface{}) {
369351
entry.Infoln(args...)
370352
}
371353

372354
func (entry *Entry) Warnln(args ...interface{}) {
373-
if entry.Logger.IsLevelEnabled(WarnLevel) {
374-
entry.Warn(entry.sprintlnn(args...))
375-
}
355+
entry.Logln(WarnLevel, args...)
376356
}
377357

378358
func (entry *Entry) Warningln(args ...interface{}) {
379359
entry.Warnln(args...)
380360
}
381361

382362
func (entry *Entry) Errorln(args ...interface{}) {
383-
if entry.Logger.IsLevelEnabled(ErrorLevel) {
384-
entry.Error(entry.sprintlnn(args...))
385-
}
363+
entry.Logln(ErrorLevel, args...)
386364
}
387365

388366
func (entry *Entry) Fatalln(args ...interface{}) {
389-
if entry.Logger.IsLevelEnabled(FatalLevel) {
390-
entry.Fatal(entry.sprintlnn(args...))
391-
}
367+
entry.Logln(FatalLevel, args...)
392368
entry.Logger.Exit(1)
393369
}
394370

395371
func (entry *Entry) Panicln(args ...interface{}) {
396-
if entry.Logger.IsLevelEnabled(PanicLevel) {
397-
entry.Panic(entry.sprintlnn(args...))
398-
}
372+
entry.Logln(PanicLevel, args...)
399373
}
400374

401375
// Sprintlnn => Sprint no newline. This is to get the behavior of how

0 commit comments

Comments
 (0)