Skip to content

Commit

Permalink
adjust log levels to be in the same order as OTEL
Browse files Browse the repository at this point in the history
  • Loading branch information
muir committed Dec 29, 2023
1 parent 0a9ba53 commit e2e16c8
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 80 deletions.
13 changes: 0 additions & 13 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,8 @@

- note that `.Table()` will also be an alternative to `.Msg()`

- (Re)define/document output formats

- xopjs (focuses on human-readability)

- Document the above and also

- xoptest
- xopotel

- Build the above

- Rebuild xopup uploader to use the new xopproto

- Rebuild server to ingest the new xopproto

# Just do it (build ready)

- check for unchecked casts
Expand Down
8 changes: 4 additions & 4 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ func CustomFromContext(getLogFromContext func(context.Context) *Log, adjustSetti
// the current package) and set the minimum log level according
// to the value of that environment variable.
//
// package foo
// package foo
// var adjustLogger = xop.LevelAdjuster()
//
func LevelAdjuster(opts ...AdjusterOption) func(*Log) *Log {
level := adjustConfig(opts)
if level == 0 {
Expand All @@ -95,9 +94,8 @@ func LevelAdjuster(opts ...AdjusterOption) func(*Log) *Log {
// the passed level is not zero.
// (3) The level that the logger already has.
//
// package foo
// package foo
// var getLogger = xop.AdjustedLevelLoger(xop.FromContextOrPanic)
//
func ContextLevelAdjuster(getLogFromContext func(context.Context) *Log, opts ...AdjusterOption) func(context.Context) *Log {
level := adjustConfig(opts)
if level == 0 {
Expand Down Expand Up @@ -132,6 +130,8 @@ func WithEnvironment(environmentVariableName string) AdjusterOption {
// ContextLevelAdjuster. If the environment variable is found then
// that will override this default. This default will override the
// existing minimum logging level.
//
// The default minimum level comes from DefaultSettings
func WithDefault(level xopnum.Level) AdjusterOption {
return func(o *adjustOptions) {
o.level = level
Expand Down
8 changes: 4 additions & 4 deletions sub.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions sub.zzzgo
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ func (settings LogSettings) String() string {
}

// DefaultSettings are the settings that are used if no setting changes
// are made. Debug logs are excluded. Alert and Error level log lines
// are made. Trace logs are excluded. Alert and Error level log lines
// get stack traces.
var DefaultSettings = func() LogSettings {
var settings LogSettings
settings.stackFramesWanted[xopnum.AlertLevel] = 20
settings.stackFramesWanted[xopnum.ErrorLevel] = 10
settings.minimumLogLevel = xopnum.TraceLevel
settings.minimumLogLevel = xopnum.DebugLevel
settings.synchronousFlushWhenDone = true
settings.stackFilenameRewrite = func(s string) string { return s }
return settings
Expand Down Expand Up @@ -239,14 +239,14 @@ func (settings *LogSettings) SynchronousFlush(b bool) {
}

// MinLevel sets the minimum logging level below which logs will
// be discarded. The default is that no logs are discarded.
// be discarded. The default minimum level comes from DefaultSettings.
func (sub *Sub) MinLevel(level xopnum.Level) *Sub {
sub.settings.MinLevel(level)
return sub
}

// MinLevel sets the minimum logging level below which logs will
// be discarded. The default is that no logs are discarded.
// be discarded. The default minimum level comes from DefaultSettings.
func (settings *LogSettings) MinLevel(level xopnum.Level) {
settings.minimumLogLevel = level
}
Expand Down
4 changes: 2 additions & 2 deletions sub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ func TestLog_Settings(t *testing.T) {

func TestSub_StackFrames(t *testing.T) {
sub := Default.Sub()
sub.StackFrames(xopnum.DebugLevel, 100)
sub.StackFrames(xopnum.TraceLevel, 100)
sub.StackFrames(xopnum.ErrorLevel, 100)
sub.StackFrames(xopnum.TraceLevel, 2)
sub.StackFrames(xopnum.DebugLevel, 2)
assert.Equal(t, 100, sub.settings.stackFramesWanted[xopnum.ErrorLevel])
assert.Equal(t, 100, sub.settings.stackFramesWanted[xopnum.AlertLevel])
assert.Equal(t, 2, sub.settings.stackFramesWanted[xopnum.TraceLevel])
Expand Down
9 changes: 5 additions & 4 deletions xopnum/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ type Level int32
const (
// Open Telemetry puts tracing as lower level than debugging. Why?
// https://github.com/open-telemetry/opentelemetry-proto/blob/main/opentelemetry/proto/logs/v1/logs.proto
// Aside from that, we'll mostly map to their numbers.
// TraceLevel is OTEL's "Debug4"
// AlertLevel is OTEL's "Error4"
// Most of the levels correspond to OTEl's levels, but
// TraceLevel is OTEL's "Trace2"
// AlertLevel is OTEL's "Error4"
// There is no fatal level.
TraceLevel Level = 2 // trace
DebugLevel Level = 5 // debug
TraceLevel Level = 8 // trace
InfoLevel Level = 9 // info
WarnLevel Level = 13 // warn
ErrorLevel Level = 17 // error
Expand Down
74 changes: 39 additions & 35 deletions xopnum/level_enumer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions xopproto/xop.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion xopproto/xop_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions xoprecorder/recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ func TestRecorderLogMethods(t *testing.T) {
log.Debug().Msg("basic debug message")
log.Trace().Msg("basic trace message")
log.Info().String("foo", "bar").Int("num", 38).Template("a test {foo} with {num}")
lines := rLog.FindLines(xoprecorder.MessageEquals("basic trace message"))
lines := rLog.FindLines(xoprecorder.MessageEquals("basic debug message"))
if assert.NotEmpty(t, lines, "found some") {
assert.True(t, !lines[0].Timestamp.Before(start), "time seq")
assert.Equal(t, "basic trace message", lines[0].Message, "message")
assert.Equal(t, xopnum.TraceLevel, lines[0].Level, "level")
assert.Equal(t, "basic debug message", lines[0].Message, "message")
assert.Equal(t, xopnum.DebugLevel, lines[0].Level, "level")
}
f := log.Sub().Fork("forkie")
f.Span().Int(xopconst.HTTPStatusCode, 204)
assert.Empty(t, rLog.FindLines(xoprecorder.MessageEquals("basic debug message")), "debug filtered out by log level")
assert.Empty(t, rLog.FindLines(xoprecorder.MessageEquals("basic trace message")), "debug filtered out by log level")
assert.Equal(t, 1, rLog.CountLines(xoprecorder.MessageEquals("basic alert message")), "count alert")
assert.Equal(t, 1, rLog.CountLines(xoprecorder.TextContains("a test")), "count a test")
assert.Equal(t, 1, rLog.CountLines(xoprecorder.TextContains("a test bar")), "count a test foo")
Expand Down
4 changes: 2 additions & 2 deletions xopresty/resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ var cases = []struct {
assert.NoError(t, err, "marshal")
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(enc)
log.Trace().Msg("sent response")
log.Debug().Msg("sent response")
},
expectedText: []string{
`T1.1.1 MODEL:request {"Name":"Joe","Count":38}`,
Expand All @@ -87,7 +87,7 @@ func TestXopResty(t *testing.T) {
seed := xop.NewSeed(xop.WithBase(tLog))
log := seed.Request("client")
log.Info().Msg("i am the base log")
ctx := log.Sub().MinLevel(xopnum.DebugLevel).Log().IntoContext(context.Background())
ctx := log.Sub().MinLevel(xopnum.TraceLevel).Log().IntoContext(context.Background())

var called bool
inbound := xopmiddle.New(seed, func(r *http.Request) string {
Expand Down
8 changes: 4 additions & 4 deletions xoptest/testlogger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ func TestLogMethods(t *testing.T) {
log.Debug().Msg("basic debug message")
log.Trace().Msg("basic trace message")
log.Info().String("foo", "bar").Int("num", 38).Template("a test {foo} with {num}")
lines := tLog.Recorder().FindLines(xoprecorder.MessageEquals("basic trace message"))
lines := tLog.Recorder().FindLines(xoprecorder.MessageEquals("basic debug message"))
if assert.NotEmpty(t, lines, "found some") {
assert.True(t, !lines[0].Timestamp.Before(start), "time seq")
assert.Equal(t, "basic trace message", lines[0].Message, "message")
assert.Equal(t, xopnum.TraceLevel, lines[0].Level, "level")
assert.Equal(t, "basic debug message", lines[0].Message, "message")
assert.Equal(t, xopnum.DebugLevel, lines[0].Level, "level")
}
f := log.Sub().Fork("forkie")
f.Span().Int(xopconst.HTTPStatusCode, 204)
assert.Empty(t, tLog.Recorder().FindLines(xoprecorder.MessageEquals("basic debug message")), "debug filtered out by log level")
assert.Empty(t, tLog.Recorder().FindLines(xoprecorder.MessageEquals("basic trace message")), "debug filtered out by log level")
assert.Equal(t, 1, tLog.Recorder().CountLines(xoprecorder.MessageEquals("basic alert message")), "count alert")
assert.Equal(t, 1, tLog.Recorder().CountLines(xoprecorder.TextContains("a test")), "count a test")
assert.Equal(t, 1, tLog.Recorder().CountLines(xoprecorder.TextContains("a test bar")), "count a test foo")
Expand Down
3 changes: 2 additions & 1 deletion xoptest/xoptestutil/cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,8 @@ var MessageCases = []struct {
Any("null", nil).
Error("no", fmt.Errorf("bar")).
Msg("no foobar")
log.Trace().Stringer("do", sc).Msg("yes, foobar")
log.Trace().Stringer("don't", sc).Msg("yes, foobar")
log.Debug().Stringer("do", sc).Msg("yes, foobar")
assert.Equal(t, 1, callCount, "stringer called once")
MicroNap()
log.Done()
Expand Down

0 comments on commit e2e16c8

Please sign in to comment.