Skip to content

Commit

Permalink
chore: improve zap logging
Browse files Browse the repository at this point in the history
  • Loading branch information
moshloop committed Jan 15, 2024
1 parent 88910a0 commit f3eae56
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 131 deletions.
59 changes: 34 additions & 25 deletions console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/flanksource/commons/is"
"github.com/flanksource/commons/logger"
)

var (
Expand Down Expand Up @@ -47,201 +48,209 @@ var (
BrightWhite = "\x1b[38;5;244m"
)

var (
isTTY = is.TTY()
)

func ColorOff() {
isTTY = false
}

// DarkWhitef prints and formats msg as dark white
func DarkWhitef(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return DarkWhite + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// DarkWhitef prints and formats msg as dark white
func LightWhitef(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return white + Light + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// DarkWhitef prints and formats msg as dark white
func BrightWhitef(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return white + Bright + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// DarkWhitef prints and formats msg as dark white
func DarkF(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return DarkWhite + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// DarkWhitef prints and formats msg as dark white
func BrightF(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return DarkWhite + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// Redf prints and formats msg as red text
func Redf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return Red + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// LightRedf prints and formats msg as red text
func LightRedf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return LightRed + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// Yellowf prints and formats msg as red text
func Yellowf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return Yellow + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// BrightYellowf prints and formats msg as red text
func BrightYellowf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return BrightYellow + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// LightYellowf prints and formats msg as red text
func LightYellowf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return LightYellow + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// Grayf prints and formats msg as red text
func Grayf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return Gray + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// BrightGrayf prints and formats msg as red text
func BrightGrayf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return BrightGray + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// LightGrayf prints and formats msg as red text
func LightGrayf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return LightGray + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// Bluef prints and formats msg as red text
func Bluef(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return Blue + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// LightBluef prints and formats msg as red text
func LightBluef(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return LightBlue + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// BrightBluef prints and formats msg as red text
func BrightBluef(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return blue + Light + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// Greenf prints and formats msg as green text
func Greenf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return Green + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// BrightGreenf prints and formats msg as light green text
func BrightGreenf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return green + Bright + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// LightGreenf prints and formats msg as light green text
func LightGreenf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return green + Light + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// LightCyanf prints and formats msg as light cyan text
func LightCyanf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return LightCyan + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// LightCyanf prints and formats msg as light cyan text
func Cyanf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return Cyan + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// LightCyanf prints and formats msg as light cyan text
func BrightCyanf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return BrightCyan + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// Magentaf prints and formats msg as green text
func Magentaf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return Magenta + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// Magentaf prints and formats msg as green text
func LightMagentaf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return LightMagenta + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
}

// Magentaf prints and formats msg as green text
func BrightMagentaf(msg string, args ...interface{}) string {
if is.TTY() {
if isTTY && !logger.IsJsonLogs() {
return BrightMagenta + fmt.Sprintf(msg, args...) + Reset
}
return fmt.Sprintf(msg, args...)
Expand Down
6 changes: 4 additions & 2 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,13 @@ func (c Context) WithTimeout(timeout time.Duration) (Context, gocontext.CancelFu
}

func (c Context) IsDebug() bool {
return (c.debug != nil && *c.debug) || (c.isDebugFn != nil && c.isDebugFn(c))
return c.logger.IsLevelEnabled(5) ||
(c.debug != nil && *c.debug) ||
(c.isDebugFn != nil && c.isDebugFn(c))
}

func (c Context) IsTrace() bool {
return (c.trace != nil && *c.trace) || (c.isTraceFn != nil && c.isTraceFn(c))
return c.logger.IsLevelEnabled(6) || (c.trace != nil && *c.trace) || (c.isTraceFn != nil && c.isTraceFn(c))
}

func (c Context) Debugf(format string, args ...interface{}) {
Expand Down
41 changes: 30 additions & 11 deletions logger/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,37 @@ import (
)

var currentLogger Logger
var color, reportCaller, jsonLogs bool
var level int

func init() {
logger := logsrusapi.StandardLogger()
logger.SetFormatter(&logsrusapi.TextFormatter{
DisableQuote: true,
})
currentLogger = NewLogrusLogger(logger)
currentLogger = newZap(1)
}

func IsJsonLogs() bool {
return jsonLogs
}

func BindFlags(flags *pflag.FlagSet) {
flags.CountP("loglevel", "v", "Increase logging level")
flags.Bool("json-logs", false, "Print logs in json format to stderr")
flags.CountVarP(&level, "loglevel", "v", "Increase logging level")
flags.BoolVar(&jsonLogs, "json-logs", false, "Print logs in json format to stderr")
flags.BoolVar(&color, "color", true, "Print logs using color")
flags.BoolVar(&reportCaller, "report-caller", false, "Report log caller info")
}

func ParseFlags(flags *pflag.FlagSet) {
level, _ := flags.GetCount("loglevel")
func UseLogsrus() {
logger := logsrusapi.StandardLogger()
if jsonLogs {
logger.SetFormatter(&logsrusapi.JSONFormatter{})
} else {
logger.SetFormatter(&logsrusapi.TextFormatter{
DisableColors: !color,
ForceColors: color,
FullTimestamp: true,
DisableQuote: true,
})
}
currentLogger = NewLogrusLogger(logger, level)
currentLogger.SetLogLevel(level)
}

Expand All @@ -37,12 +52,12 @@ func Infof(format string, args ...interface{}) {
currentLogger.Infof(format, args...)
}

//Secretf is like Tracef, but attempts to strip any secrets from the text
// Secretf is like Tracef, but attempts to strip any secrets from the text
func Secretf(format string, args ...interface{}) {
currentLogger.Tracef(stripSecrets(fmt.Sprintf(format, args...)))
}

//Prettyf is like Tracef, but pretty prints the entire struct
// Prettyf is like Tracef, but pretty prints the entire struct
func Prettyf(msg string, obj interface{}) {
pretty.Print(obj)
// currentLogger.Tracef(msg, pretty.Sprint(obj))
Expand Down Expand Up @@ -71,6 +86,10 @@ func IsTraceEnabled() bool {
return currentLogger.IsTraceEnabled()
}

func IsLevelEnabled(level int) bool {
return currentLogger.V(level).Enabled()
}

func IsDebugEnabled() bool {
return currentLogger.IsDebugEnabled()
}
Expand Down
7 changes: 5 additions & 2 deletions logger/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ type Logger interface {
WithValues(keysAndValues ...interface{}) Logger
IsTraceEnabled() bool
IsDebugEnabled() bool
IsLevelEnabled(level int) bool
GetLevel() int
SetLogLevel(level int)
SetMinLogLevel(level int)
V(level int) Verbose
Named(name string) Logger
}

type Verbose interface {
Info(args ...interface{})
Infof(format string, args ...interface{})
Infoln(args ...interface{})
Enabled() bool
}
Loading

0 comments on commit f3eae56

Please sign in to comment.