Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add New Trace Logging Level #749

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions level.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import (
)

const (
// TraceLevel logs are especially granular log messages that are used in development and debugging
// situations in which tracing of function inputs at outputs are necessary. Almost always disabled in production
// due to performance hits associated with very voluminous logs.
TraceLevel = zapcore.TraceLevel
// DebugLevel logs are typically voluminous, and are usually disabled in
// production.
DebugLevel = zapcore.DebugLevel
Expand Down
2 changes: 2 additions & 0 deletions level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestLevelEnablerFunc(t *testing.T) {
level zapcore.Level
enabled bool
}{
{TraceLevel, false},
{DebugLevel, false},
{InfoLevel, true},
{WarnLevel, false},
Expand Down Expand Up @@ -81,6 +82,7 @@ func TestAtomicLevelText(t *testing.T) {
expect zapcore.Level
err bool
}{
{"trace", TraceLevel, false},
{"debug", DebugLevel, false},
{"info", InfoLevel, false},
{"", InfoLevel, false},
Expand Down
8 changes: 8 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ func (log *Logger) Check(lvl zapcore.Level, msg string) *zapcore.CheckedEntry {
return log.check(lvl, msg)
}

// Trace logs a message at TraceLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (log *Logger) Trace(msg string, fields ...Field) {
if ce := log.check(TraceLevel, msg); ce != nil {
ce.Write(fields...)
}
}

// Debug logs a message at DebugLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (log *Logger) Debug(msg string, fields ...Field) {
Expand Down
5 changes: 4 additions & 1 deletion logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func TestLoggerAtomicLevel(t *testing.T) {
testLevel zapcore.Level
enabled bool
}{
{TraceLevel, TraceLevel, true},
{DebugLevel, TraceLevel, false},
{DebugLevel, DebugLevel, true},
{InfoLevel, DebugLevel, false},
{WarnLevel, PanicLevel, true},
Expand Down Expand Up @@ -169,11 +171,12 @@ func TestLoggerLogFatal(t *testing.T) {
}

func TestLoggerLeveledMethods(t *testing.T) {
withLogger(t, DebugLevel, nil, func(logger *Logger, logs *observer.ObservedLogs) {
withLogger(t, TraceLevel, nil, func(logger *Logger, logs *observer.ObservedLogs) {
tests := []struct {
method func(string, ...Field)
expectedLevel zapcore.Level
}{
{logger.Trace, TraceLevel},
{logger.Debug, DebugLevel},
{logger.Info, InfoLevel},
{logger.Warn, WarnLevel},
Expand Down
14 changes: 12 additions & 2 deletions zapcore/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ var errUnmarshalNilLevel = errors.New("can't unmarshal a nil *Level")
type Level int8

const (
// TraceLevel logs are especially granular log messages that are used in development and debugging
// situations in which tracing of function inputs at outputs are necessary. Almost always disabled in production
// due to performance hits associated with very voluminous logs.
TraceLevel Level = iota - 2
// DebugLevel logs are typically voluminous, and are usually disabled in
// production.
DebugLevel Level = iota - 1
DebugLevel
// InfoLevel is the default logging priority.
InfoLevel
// WarnLevel logs are more important than Info, but don't need individual
Expand All @@ -51,13 +55,15 @@ const (
// FatalLevel logs a message, then calls os.Exit(1).
FatalLevel

_minLevel = DebugLevel
_minLevel = TraceLevel
_maxLevel = FatalLevel
)

// String returns a lower-case ASCII representation of the log level.
func (l Level) String() string {
switch l {
case TraceLevel:
return "trace"
case DebugLevel:
return "debug"
case InfoLevel:
Expand All @@ -82,6 +88,8 @@ func (l Level) CapitalString() string {
// Printing levels in all-caps is common enough that we should export this
// functionality.
switch l {
case TraceLevel:
return "TRACE"
case DebugLevel:
return "DEBUG"
case InfoLevel:
Expand Down Expand Up @@ -125,6 +133,8 @@ func (l *Level) UnmarshalText(text []byte) error {

func (l *Level) unmarshalText(text []byte) bool {
switch string(text) {
case "trace", "TRACE":
*l = TraceLevel
case "debug", "DEBUG":
*l = DebugLevel
case "info", "INFO", "": // make the zero value useful
Expand Down
1 change: 1 addition & 0 deletions zapcore/level_strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import "go.uber.org/zap/internal/color"

var (
_levelToColor = map[Level]color.Color{
TraceLevel: color.Green,
DebugLevel: color.Magenta,
InfoLevel: color.Blue,
WarnLevel: color.Yellow,
Expand Down
5 changes: 5 additions & 0 deletions zapcore/level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

func TestLevelString(t *testing.T) {
tests := map[Level]string{
TraceLevel: "trace",
DebugLevel: "debug",
InfoLevel: "info",
WarnLevel: "warn",
Expand All @@ -52,6 +53,7 @@ func TestLevelText(t *testing.T) {
text string
level Level
}{
{"trace", TraceLevel},
{"debug", DebugLevel},
{"info", InfoLevel},
{"", InfoLevel}, // make the zero value useful
Expand Down Expand Up @@ -81,6 +83,7 @@ func TestCapitalLevelsParse(t *testing.T) {
text string
level Level
}{
{"TRACE", TraceLevel},
{"DEBUG", DebugLevel},
{"INFO", InfoLevel},
{"WARN", WarnLevel},
Expand All @@ -103,6 +106,7 @@ func TestWeirdLevelsParse(t *testing.T) {
level Level
}{
// I guess...
{"Trace", TraceLevel},
{"Debug", DebugLevel},
{"Info", InfoLevel},
{"Warn", WarnLevel},
Expand All @@ -112,6 +116,7 @@ func TestWeirdLevelsParse(t *testing.T) {
{"Fatal", FatalLevel},

// What even is...
{"TrAcE", TraceLevel},
{"DeBuG", DebugLevel},
{"InFo", InfoLevel},
{"WaRn", WarnLevel},
Expand Down