Skip to content

Commit e1e72e9

Browse files
authored
Merge pull request #863 from lugray/generic_log
Add Generic Log functions with level via argument
2 parents 6180652 + bd9534b commit e1e72e9

File tree

3 files changed

+92
-177
lines changed

3 files changed

+92
-177
lines changed

entry.go

Lines changed: 37 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -251,162 +251,136 @@ func (entry *Entry) write() {
251251
}
252252
}
253253

254-
func (entry *Entry) Trace(args ...interface{}) {
255-
if entry.Logger.IsLevelEnabled(TraceLevel) {
256-
entry.log(TraceLevel, fmt.Sprint(args...))
254+
func (entry *Entry) Log(level Level, args ...interface{}) {
255+
if entry.Logger.IsLevelEnabled(level) {
256+
entry.log(level, fmt.Sprint(args...))
257257
}
258258
}
259259

260+
func (entry *Entry) Trace(args ...interface{}) {
261+
entry.Log(TraceLevel, args...)
262+
}
263+
260264
func (entry *Entry) Debug(args ...interface{}) {
261-
if entry.Logger.IsLevelEnabled(DebugLevel) {
262-
entry.log(DebugLevel, fmt.Sprint(args...))
263-
}
265+
entry.Log(DebugLevel, args...)
264266
}
265267

266268
func (entry *Entry) Print(args ...interface{}) {
267269
entry.Info(args...)
268270
}
269271

270272
func (entry *Entry) Info(args ...interface{}) {
271-
if entry.Logger.IsLevelEnabled(InfoLevel) {
272-
entry.log(InfoLevel, fmt.Sprint(args...))
273-
}
273+
entry.Log(InfoLevel, args...)
274274
}
275275

276276
func (entry *Entry) Warn(args ...interface{}) {
277-
if entry.Logger.IsLevelEnabled(WarnLevel) {
278-
entry.log(WarnLevel, fmt.Sprint(args...))
279-
}
277+
entry.Log(WarnLevel, args...)
280278
}
281279

282280
func (entry *Entry) Warning(args ...interface{}) {
283281
entry.Warn(args...)
284282
}
285283

286284
func (entry *Entry) Error(args ...interface{}) {
287-
if entry.Logger.IsLevelEnabled(ErrorLevel) {
288-
entry.log(ErrorLevel, fmt.Sprint(args...))
289-
}
285+
entry.Log(ErrorLevel, args...)
290286
}
291287

292288
func (entry *Entry) Fatal(args ...interface{}) {
293-
if entry.Logger.IsLevelEnabled(FatalLevel) {
294-
entry.log(FatalLevel, fmt.Sprint(args...))
295-
}
289+
entry.Log(FatalLevel, args...)
296290
entry.Logger.Exit(1)
297291
}
298292

299293
func (entry *Entry) Panic(args ...interface{}) {
300-
if entry.Logger.IsLevelEnabled(PanicLevel) {
301-
entry.log(PanicLevel, fmt.Sprint(args...))
302-
}
294+
entry.Log(PanicLevel, args...)
303295
panic(fmt.Sprint(args...))
304296
}
305297

306298
// Entry Printf family functions
307299

300+
func (entry *Entry) Logf(level Level, format string, args ...interface{}) {
301+
entry.Log(level, fmt.Sprintf(format, args...))
302+
}
303+
308304
func (entry *Entry) Tracef(format string, args ...interface{}) {
309-
if entry.Logger.IsLevelEnabled(TraceLevel) {
310-
entry.Trace(fmt.Sprintf(format, args...))
311-
}
305+
entry.Logf(TraceLevel, format, args...)
312306
}
313307

314308
func (entry *Entry) Debugf(format string, args ...interface{}) {
315-
if entry.Logger.IsLevelEnabled(DebugLevel) {
316-
entry.Debug(fmt.Sprintf(format, args...))
317-
}
309+
entry.Logf(DebugLevel, format, args...)
318310
}
319311

320312
func (entry *Entry) Infof(format string, args ...interface{}) {
321-
if entry.Logger.IsLevelEnabled(InfoLevel) {
322-
entry.Info(fmt.Sprintf(format, args...))
323-
}
313+
entry.Logf(InfoLevel, format, args...)
324314
}
325315

326316
func (entry *Entry) Printf(format string, args ...interface{}) {
327317
entry.Infof(format, args...)
328318
}
329319

330320
func (entry *Entry) Warnf(format string, args ...interface{}) {
331-
if entry.Logger.IsLevelEnabled(WarnLevel) {
332-
entry.Warn(fmt.Sprintf(format, args...))
333-
}
321+
entry.Logf(WarnLevel, format, args...)
334322
}
335323

336324
func (entry *Entry) Warningf(format string, args ...interface{}) {
337325
entry.Warnf(format, args...)
338326
}
339327

340328
func (entry *Entry) Errorf(format string, args ...interface{}) {
341-
if entry.Logger.IsLevelEnabled(ErrorLevel) {
342-
entry.Error(fmt.Sprintf(format, args...))
343-
}
329+
entry.Logf(ErrorLevel, format, args...)
344330
}
345331

346332
func (entry *Entry) Fatalf(format string, args ...interface{}) {
347-
if entry.Logger.IsLevelEnabled(FatalLevel) {
348-
entry.Fatal(fmt.Sprintf(format, args...))
349-
}
333+
entry.Logf(FatalLevel, format, args...)
350334
entry.Logger.Exit(1)
351335
}
352336

353337
func (entry *Entry) Panicf(format string, args ...interface{}) {
354-
if entry.Logger.IsLevelEnabled(PanicLevel) {
355-
entry.Panic(fmt.Sprintf(format, args...))
356-
}
338+
entry.Logf(PanicLevel, format, args...)
357339
}
358340

359341
// Entry Println family functions
360342

361-
func (entry *Entry) Traceln(args ...interface{}) {
362-
if entry.Logger.IsLevelEnabled(TraceLevel) {
363-
entry.Trace(entry.sprintlnn(args...))
343+
func (entry *Entry) Logln(level Level, args ...interface{}) {
344+
if entry.Logger.IsLevelEnabled(level) {
345+
entry.Log(level, entry.sprintlnn(args...))
364346
}
365347
}
366348

349+
func (entry *Entry) Traceln(args ...interface{}) {
350+
entry.Logln(TraceLevel, args...)
351+
}
352+
367353
func (entry *Entry) Debugln(args ...interface{}) {
368-
if entry.Logger.IsLevelEnabled(DebugLevel) {
369-
entry.Debug(entry.sprintlnn(args...))
370-
}
354+
entry.Logln(DebugLevel, args...)
371355
}
372356

373357
func (entry *Entry) Infoln(args ...interface{}) {
374-
if entry.Logger.IsLevelEnabled(InfoLevel) {
375-
entry.Info(entry.sprintlnn(args...))
376-
}
358+
entry.Logln(InfoLevel, args...)
377359
}
378360

379361
func (entry *Entry) Println(args ...interface{}) {
380362
entry.Infoln(args...)
381363
}
382364

383365
func (entry *Entry) Warnln(args ...interface{}) {
384-
if entry.Logger.IsLevelEnabled(WarnLevel) {
385-
entry.Warn(entry.sprintlnn(args...))
386-
}
366+
entry.Logln(WarnLevel, args...)
387367
}
388368

389369
func (entry *Entry) Warningln(args ...interface{}) {
390370
entry.Warnln(args...)
391371
}
392372

393373
func (entry *Entry) Errorln(args ...interface{}) {
394-
if entry.Logger.IsLevelEnabled(ErrorLevel) {
395-
entry.Error(entry.sprintlnn(args...))
396-
}
374+
entry.Logln(ErrorLevel, args...)
397375
}
398376

399377
func (entry *Entry) Fatalln(args ...interface{}) {
400-
if entry.Logger.IsLevelEnabled(FatalLevel) {
401-
entry.Fatal(entry.sprintlnn(args...))
402-
}
378+
entry.Logln(FatalLevel, args...)
403379
entry.Logger.Exit(1)
404380
}
405381

406382
func (entry *Entry) Panicln(args ...interface{}) {
407-
if entry.Logger.IsLevelEnabled(PanicLevel) {
408-
entry.Panic(entry.sprintlnn(args...))
409-
}
383+
entry.Logln(PanicLevel, args...)
410384
}
411385

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

0 commit comments

Comments
 (0)