Skip to content

Commit

Permalink
feat(log): add callerFormat to set short/long file location
Browse files Browse the repository at this point in the history
Fixes: #22
  • Loading branch information
aymanbagabas committed Mar 1, 2023
1 parent b6dcc9b commit 53d005a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
13 changes: 12 additions & 1 deletion log.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type logger struct {
timeFunc TimeFunction
timeFormat string
callerOffset int
callerFormat CallerFormat
formatter Formatter

caller bool
Expand Down Expand Up @@ -103,7 +104,10 @@ func (l *logger) log(level Level, msg interface{}, keyvals ...interface{}) {
if l.caller {
// Call stack is log.Error -> log.log (2)
file, line, _ := l.fillLoc(l.callerOffset + 2)
caller := fmt.Sprintf("%s:%d", trimCallerPath(file), line)
if l.callerFormat == CallerShort {
file = trimCallerPath(file)
}
caller := fmt.Sprintf("%s:%d", file, line)
kvs = append(kvs, CallerKey, caller)
}

Expand Down Expand Up @@ -220,6 +224,13 @@ func (l *logger) SetReportCaller(report bool) {
l.caller = report
}

// SetCallerFormat sets the caller format.
func (l *logger) SetCallerFormat(format CallerFormat) {
l.mu.Lock()
defer l.mu.Unlock()
l.callerFormat = format
}

// GetLevel returns the current level.
func (l *logger) GetLevel() Level {
l.mu.RLock()
Expand Down
16 changes: 16 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ func NowUTC() time.Time {
return time.Now().UTC()
}

// CallerFormat is the format of the caller location.
type CallerFormat uint8

const (
// CallerShort is the short format of the caller location.
// The short format is the file name and one level of directory, and the
// line number.
CallerShort CallerFormat = iota
// CallerLong is the long format of the caller location.
// The long format is the full path of the file and the line number.
CallerLong
)

// Logger is an interface for logging.
type Logger interface {
// SetLevel sets the allowed level.
Expand All @@ -39,6 +52,9 @@ type Logger interface {
SetReportTimestamp(bool)
// SetReportCaller sets whether the logger should report the caller location.
SetReportCaller(bool)
// SetCallerFormat sets the caller format. The default is CallerShort.
// This is only used when SetReportCaller is true.
SetCallerFormat(format CallerFormat)
// SetTimeFunction sets the time function used to get the time.
// The default is time.Now.
//
Expand Down
8 changes: 8 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ func WithCaller() LoggerOption {
}
}

// WithCallerFormat returns a LoggerOption that sets the caller format for the
// logger.
func WithCallerFormat(format CallerFormat) LoggerOption {
return func(l *logger) {
l.callerFormat = format
}
}

// WithFields returns a LoggerOption that sets the fields for the logger.
func WithFields(keyvals ...interface{}) LoggerOption {
return func(l *logger) {
Expand Down
5 changes: 5 additions & 0 deletions pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ func SetReportCaller(report bool) {
defaultLogger.SetReportCaller(report)
}

// SetCallerFormat sets the caller format. The default is CallerShort.
func SetCallerFormat(format CallerFormat) {
defaultLogger.SetCallerFormat(format)
}

// SetLevel sets the level for the default logger.
func SetLevel(level Level) {
defaultLogger.SetLevel(level)
Expand Down
16 changes: 16 additions & 0 deletions pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package log

import (
"bytes"
"fmt"
"os"
"os/exec"
"runtime"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -51,6 +53,20 @@ func TestGlobal(t *testing.T) {
}
}

func TestFullCallerSource(t *testing.T) {
var buf bytes.Buffer
SetOutput(&buf)
SetCallerFormat(CallerLong)
SetReportCaller(true)
SetReportTimestamp(false)
_, file, line, _ := runtime.Caller(0)
Error("error")
assert.Equal(t, fmt.Sprintf("ERROR <%s:%d> error\n", file, line+1), buf.String())
SetReportTimestamp(true)
SetReportCaller(false)
SetCallerFormat(CallerShort)
}

func TestPrint(t *testing.T) {
var buf bytes.Buffer
SetOutput(&buf)
Expand Down

0 comments on commit 53d005a

Please sign in to comment.