diff --git a/logger.go b/logger.go index 2912ff0..783db47 100644 --- a/logger.go +++ b/logger.go @@ -49,7 +49,13 @@ type Logger struct { styles *Styles } -func (l *Logger) log(level Level, msg interface{}, keyvals ...interface{}) { +// Logf logs a message with formatting. +func (l *Logger) Logf(level Level, format string, args ...interface{}) { + l.Log(level, fmt.Sprintf(format, args...)) +} + +// Log logs the given message with the given keyvals for the given level. +func (l *Logger) Log(level Level, msg interface{}, keyvals ...interface{}) { if atomic.LoadUint32(&l.isDiscard) != 0 { return } @@ -343,62 +349,62 @@ func (l *Logger) WithPrefix(prefix string) *Logger { // Debug prints a debug message. func (l *Logger) Debug(msg interface{}, keyvals ...interface{}) { - l.log(DebugLevel, msg, keyvals...) + l.Log(DebugLevel, msg, keyvals...) } // Info prints an info message. func (l *Logger) Info(msg interface{}, keyvals ...interface{}) { - l.log(InfoLevel, msg, keyvals...) + l.Log(InfoLevel, msg, keyvals...) } // Warn prints a warning message. func (l *Logger) Warn(msg interface{}, keyvals ...interface{}) { - l.log(WarnLevel, msg, keyvals...) + l.Log(WarnLevel, msg, keyvals...) } // Error prints an error message. func (l *Logger) Error(msg interface{}, keyvals ...interface{}) { - l.log(ErrorLevel, msg, keyvals...) + l.Log(ErrorLevel, msg, keyvals...) } // Fatal prints a fatal message and exits. func (l *Logger) Fatal(msg interface{}, keyvals ...interface{}) { - l.log(FatalLevel, msg, keyvals...) + l.Log(FatalLevel, msg, keyvals...) os.Exit(1) } // Print prints a message with no level. func (l *Logger) Print(msg interface{}, keyvals ...interface{}) { - l.log(noLevel, msg, keyvals...) + l.Log(noLevel, msg, keyvals...) } // Debugf prints a debug message with formatting. func (l *Logger) Debugf(format string, args ...interface{}) { - l.log(DebugLevel, fmt.Sprintf(format, args...)) + l.Log(DebugLevel, fmt.Sprintf(format, args...)) } // Infof prints an info message with formatting. func (l *Logger) Infof(format string, args ...interface{}) { - l.log(InfoLevel, fmt.Sprintf(format, args...)) + l.Log(InfoLevel, fmt.Sprintf(format, args...)) } // Warnf prints a warning message with formatting. func (l *Logger) Warnf(format string, args ...interface{}) { - l.log(WarnLevel, fmt.Sprintf(format, args...)) + l.Log(WarnLevel, fmt.Sprintf(format, args...)) } // Errorf prints an error message with formatting. func (l *Logger) Errorf(format string, args ...interface{}) { - l.log(ErrorLevel, fmt.Sprintf(format, args...)) + l.Log(ErrorLevel, fmt.Sprintf(format, args...)) } // Fatalf prints a fatal message with formatting and exits. func (l *Logger) Fatalf(format string, args ...interface{}) { - l.log(FatalLevel, fmt.Sprintf(format, args...)) + l.Log(FatalLevel, fmt.Sprintf(format, args...)) os.Exit(1) } // Printf prints a message with no level and formatting. func (l *Logger) Printf(format string, args ...interface{}) { - l.log(noLevel, fmt.Sprintf(format, args...)) + l.Log(noLevel, fmt.Sprintf(format, args...)) } diff --git a/pkg.go b/pkg.go index dd558a1..f3696cd 100644 --- a/pkg.go +++ b/pkg.go @@ -160,66 +160,76 @@ func Helper() { defaultLogger.helper(1) } +// Log logs a message with the given level. +func Log(level Level, msg interface{}, keyvals ...interface{}) { + defaultLogger.Log(level, msg, keyvals...) +} + // Debug logs a debug message. func Debug(msg interface{}, keyvals ...interface{}) { - defaultLogger.log(DebugLevel, msg, keyvals...) + defaultLogger.Log(DebugLevel, msg, keyvals...) } // Info logs an info message. func Info(msg interface{}, keyvals ...interface{}) { - defaultLogger.log(InfoLevel, msg, keyvals...) + defaultLogger.Log(InfoLevel, msg, keyvals...) } // Warn logs a warning message. func Warn(msg interface{}, keyvals ...interface{}) { - defaultLogger.log(WarnLevel, msg, keyvals...) + defaultLogger.Log(WarnLevel, msg, keyvals...) } // Error logs an error message. func Error(msg interface{}, keyvals ...interface{}) { - defaultLogger.log(ErrorLevel, msg, keyvals...) + defaultLogger.Log(ErrorLevel, msg, keyvals...) } // Fatal logs a fatal message and exit. func Fatal(msg interface{}, keyvals ...interface{}) { - defaultLogger.log(FatalLevel, msg, keyvals...) + defaultLogger.Log(FatalLevel, msg, keyvals...) os.Exit(1) } // Print logs a message with no level. func Print(msg interface{}, keyvals ...interface{}) { - defaultLogger.log(noLevel, msg, keyvals...) + defaultLogger.Log(noLevel, msg, keyvals...) +} + +// Logf logs a message with formatting and level. +func Logf(level Level, format string, args ...interface{}) { + defaultLogger.Logf(level, format, args...) } // Debugf logs a debug message with formatting. func Debugf(format string, args ...interface{}) { - defaultLogger.log(DebugLevel, fmt.Sprintf(format, args...)) + defaultLogger.Log(DebugLevel, fmt.Sprintf(format, args...)) } // Infof logs an info message with formatting. func Infof(format string, args ...interface{}) { - defaultLogger.log(InfoLevel, fmt.Sprintf(format, args...)) + defaultLogger.Log(InfoLevel, fmt.Sprintf(format, args...)) } // Warnf logs a warning message with formatting. func Warnf(format string, args ...interface{}) { - defaultLogger.log(WarnLevel, fmt.Sprintf(format, args...)) + defaultLogger.Log(WarnLevel, fmt.Sprintf(format, args...)) } // Errorf logs an error message with formatting. func Errorf(format string, args ...interface{}) { - defaultLogger.log(ErrorLevel, fmt.Sprintf(format, args...)) + defaultLogger.Log(ErrorLevel, fmt.Sprintf(format, args...)) } // Fatalf logs a fatal message with formatting and exit. func Fatalf(format string, args ...interface{}) { - defaultLogger.log(FatalLevel, fmt.Sprintf(format, args...)) + defaultLogger.Log(FatalLevel, fmt.Sprintf(format, args...)) os.Exit(1) } // Printf logs a message with formatting and no level. func Printf(format string, args ...interface{}) { - defaultLogger.log(noLevel, fmt.Sprintf(format, args...)) + defaultLogger.Log(noLevel, fmt.Sprintf(format, args...)) } // StandardLog returns a standard logger from the default logger.