Skip to content

Commit

Permalink
feat(calleroffset): added option to config custom caller offset
Browse files Browse the repository at this point in the history
  • Loading branch information
waffle69691337 authored and aymanbagabas committed May 23, 2023
1 parent 347b8ef commit 96a7043
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,13 @@ func (l *Logger) SetCallerFormatter(f CallerFormatter) {
l.callerFormatter = f
}

// SetCallerOffset sets the caller offset.
func (l *Logger) SetCallerOffset(offset int) {
l.mu.Lock()
defer l.mu.Unlock()
l.callerOffset = offset
}

// With returns a new logger with the given keyvals added.
func (l *Logger) With(keyvals ...interface{}) *Logger {
sl := *l
Expand Down
3 changes: 3 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ type Options struct {
ReportCaller bool
// CallerFormatter is the caller format for the logger. The default is CallerShort.
CallerFormatter CallerFormatter
// CallerOffset is the caller format for the logger. The default is 0.
CallerOffset int
// Fields is the fields for the logger. The default is no fields.
Fields []interface{}
// Formatter is the formatter for the logger. The default is TextFormatter.
Formatter Formatter

}
6 changes: 6 additions & 0 deletions pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func NewWithOptions(w io.Writer, o Options) *Logger {
formatter: o.Formatter,
fields: o.Fields,
callerFormatter: o.CallerFormatter,
callerOffset: o.CallerOffset,
}

l.SetOutput(w)
Expand Down Expand Up @@ -113,6 +114,11 @@ func SetCallerFormatter(f CallerFormatter) {
defaultLogger.SetCallerFormatter(f)
}

// SetCallerOffset sets the caller offset for the default logger.
func SetCallerOffset(offset int) {
defaultLogger.SetCallerOffset(offset)
}

// SetPrefix sets the prefix for the default logger.
func SetPrefix(prefix string) {
defaultLogger.SetPrefix(prefix)
Expand Down

0 comments on commit 96a7043

Please sign in to comment.