Skip to content

Commit

Permalink
feat: expose log function
Browse files Browse the repository at this point in the history
Exposing `logger.Log` makes it possible to use a custom log level to log
messages. Simply define a new level and use `Log` and `Logf` to log
messages using the newly defined level.

Fixes: #89
  • Loading branch information
aymanbagabas committed Nov 27, 2023
1 parent 595fffe commit df1abf1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
32 changes: 19 additions & 13 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...))

Check warning on line 54 in logger.go

View check run for this annotation

Codecov / codecov/patch

logger.go#L53-L54

Added lines #L53 - L54 were not covered by tests
}

// 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
}
Expand Down Expand Up @@ -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...)

Check warning on line 362 in logger.go

View check run for this annotation

Codecov / codecov/patch

logger.go#L362

Added line #L362 was not covered by tests
}

// 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...))

Check warning on line 403 in logger.go

View check run for this annotation

Codecov / codecov/patch

logger.go#L403

Added line #L403 was not covered by tests
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...))

Check warning on line 409 in logger.go

View check run for this annotation

Codecov / codecov/patch

logger.go#L409

Added line #L409 was not covered by tests
}
34 changes: 22 additions & 12 deletions pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)

Check warning on line 165 in pkg.go

View check run for this annotation

Codecov / codecov/patch

pkg.go#L164-L165

Added lines #L164 - L165 were not covered by tests
}

// 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...)

Check warning on line 180 in pkg.go

View check run for this annotation

Codecov / codecov/patch

pkg.go#L180

Added line #L180 was not covered by tests
}

// 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...)

Check warning on line 201 in pkg.go

View check run for this annotation

Codecov / codecov/patch

pkg.go#L200-L201

Added lines #L200 - L201 were not covered by tests
}

// 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.
Expand Down

0 comments on commit df1abf1

Please sign in to comment.