Skip to content

Commit

Permalink
Log Call Site (armadaproject#2909)
Browse files Browse the repository at this point in the history
* allow logger to report caller

* allow logger to report caller

* lint

---------

Co-authored-by: Chris Martin <chris@cmartinit.co.uk>
  • Loading branch information
d80tb7 and d80tb7 authored Aug 23, 2023
1 parent b411037 commit 15ed4ee
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions internal/common/startup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"fmt"
"net/http"
"os"
"path"
"runtime"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -80,6 +83,7 @@ func ConfigureCommandLineLogging() {
func ConfigureLogging() {
log.SetLevel(readEnvironmentLogLevel())
log.SetFormatter(readEnvironmentLogFormat())
log.SetReportCaller(true)
log.SetOutput(os.Stdout)
}

Expand All @@ -99,16 +103,29 @@ func readEnvironmentLogFormat() log.Formatter {
if !ok {
formatStr = "colourful"
}

textFormatter := &log.TextFormatter{
ForceColors: true,
FullTimestamp: true,
TimestampFormat: logTimestampFormat,
CallerPrettyfier: func(frame *runtime.Frame) (function string, file string) {
fileName := path.Base(frame.File) + ":" + strconv.Itoa(frame.Line)
return "", fileName
},
}

switch strings.ToLower(formatStr) {
case "json":
return &log.JSONFormatter{TimestampFormat: logTimestampFormat}
case "colourful":
return &log.TextFormatter{ForceColors: true, FullTimestamp: true, TimestampFormat: logTimestampFormat}
return textFormatter
case "text":
return &log.TextFormatter{DisableColors: true, FullTimestamp: true, TimestampFormat: logTimestampFormat}
textFormatter.ForceColors = false
textFormatter.DisableColors = true
return textFormatter
default:
println(os.Stderr, fmt.Sprintf("Unknown log format %s, defaulting to colourful format", formatStr))
return &log.TextFormatter{ForceColors: true, FullTimestamp: true, TimestampFormat: logTimestampFormat}
return textFormatter
}
}

Expand Down

0 comments on commit 15ed4ee

Please sign in to comment.