Skip to content

Commit

Permalink
feat: Support skipping timestamps and log levels in the logs.
Browse files Browse the repository at this point in the history
  • Loading branch information
G3 authored and G3 committed Sep 21, 2024
1 parent 00d481b commit 3b5da7b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
35 changes: 20 additions & 15 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"strings"
"time"

"github.com/tuxdude/zzzlogi"
Expand All @@ -27,6 +28,10 @@ type configInternal struct {
maxLevel Level
// levelColors contains the color configuration for each log level.
levelColors levelColorMap
// skipTimestamp set to true skips logging the timestamp in the logs.
skipTimestamp bool
// skipLogLevel seto true skips logging the log level in the logs.
skipLogLevel bool
// skipCallerInfo set to true skips logging the call site information.
skipCallerInfo bool
// timestampLoggingFormat determines the format for logging the timestamps.
Expand Down Expand Up @@ -127,25 +132,25 @@ func (l *loggerImpl) log(lvl Level, skipFrames int, format string, args ...inter
return
}

var f string
var f strings.Builder
var a []interface{}

if l.config.skipCallerInfo {
f = "%s %s " + format + "\n"
a = []interface{}{
time.Now().Format(l.config.timestampLoggingFormat),
l.levelStr[lvl],
}
} else {
f = "%s %s %-40s " + format + "\n"
a = []interface{}{
time.Now().Format(l.config.timestampLoggingFormat),
l.levelStr[lvl],
callerInfo(skipFrames + 1),
}
if !l.config.skipTimestamp {
f.WriteString("%s ")
a = append(a, time.Now().Format(l.config.timestampLoggingFormat))
}
if !l.config.skipLogLevel {
f.WriteString("%s ")
a = append(a, l.levelStr[lvl])
}
if !l.config.skipCallerInfo {
f.WriteString("%-40s ")
a = append(a, callerInfo(skipFrames+1))
}
f.WriteString(format)
f.WriteString("\n")
a = append(a, args...)
l.write(f, a...)
l.write(f.String(), a...)
}

func (l *loggerImpl) logEmpty(lvl Level) {
Expand Down
16 changes: 16 additions & 0 deletions zzzlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ type Config struct {
Dest io.Writer
// Level determines the maximum logging level.
MaxLevel Level
// SkipTimestamp set to true skips logging the timestamp in the logs.
SkipTimestamp bool
// SkipLogLevel seto to true skips logging the log level in the logs.
SkipLogLevel bool
// SkipCallerInfo set to true skips logging the call site information.
SkipCallerInfo bool
// TimestampLoggingFormat determines the format for logging the timestamps.
Expand All @@ -49,6 +53,8 @@ func NewLogger(userConfig *Config) zzzlogi.Logger {
c := defaultLoggingConfig()
c.dest = userConfig.Dest
c.maxLevel = userConfig.MaxLevel
c.skipTimestamp = userConfig.SkipTimestamp
c.skipLogLevel = userConfig.SkipLogLevel
c.skipCallerInfo = userConfig.SkipCallerInfo
if userConfig.TimestampLoggingFormat != "" {
c.timestampLoggingFormat = userConfig.TimestampLoggingFormat
Expand All @@ -66,3 +72,13 @@ func NewConsoleLoggerConfig() *Config {
MaxLevel: LvlInfo,
}
}

func NewVanillaLoggerConfig() *Config {
return &Config{
Dest: os.Stdout,
MaxLevel: LvlInfo,
SkipTimestamp: true,
SkipLogLevel: true,
SkipCallerInfo: true,
}
}

0 comments on commit 3b5da7b

Please sign in to comment.