Skip to content

Commit

Permalink
增加Verbose, Trace两个级别的日志
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent.vz committed Mar 27, 2020
1 parent f994215 commit 6484585
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -7,6 +7,7 @@
_obj
_test
vendor
.idea/

# Architecture specific extensions/prefixes
*.[568vq]
Expand Down
4 changes: 4 additions & 0 deletions global.go
Expand Up @@ -139,6 +139,10 @@ func redirectStdLogAt(l *Logger, level zapcore.Level) (func(), error) {

func levelToFunc(logger *Logger, lvl zapcore.Level) (func(string, ...Field), error) {
switch lvl {
case VerboseLevel:
return logger.Verbose, nil
case TraceLevel:
return logger.Trace, nil
case DebugLevel:
return logger.Debug, nil
case InfoLevel:
Expand Down
6 changes: 4 additions & 2 deletions level.go
Expand Up @@ -26,6 +26,8 @@ import (
)

const (
VerboseLevel = zapcore.VerboseLevel
TraceLevel = zapcore.TraceLevel
// DebugLevel logs are typically voluminous, and are usually disabled in
// production.
DebugLevel = zapcore.DebugLevel
Expand Down Expand Up @@ -108,7 +110,7 @@ func (lvl AtomicLevel) String() string {
}

// UnmarshalText unmarshals the text to an AtomicLevel. It uses the same text
// representations as the static zapcore.Levels ("debug", "info", "warn",
// representations as the static zapcore.Levels ("verbose", "trace", "debug", "info", "warn",
// "error", "dpanic", "panic", and "fatal").
func (lvl *AtomicLevel) UnmarshalText(text []byte) error {
if lvl.l == nil {
Expand All @@ -125,7 +127,7 @@ func (lvl *AtomicLevel) UnmarshalText(text []byte) error {
}

// MarshalText marshals the AtomicLevel to a byte slice. It uses the same
// text representation as the static zapcore.Levels ("debug", "info", "warn",
// text representation as the static zapcore.Levels ("verbose", "trace", "debug", "info", "warn",
// "error", "dpanic", "panic", and "fatal").
func (lvl AtomicLevel) MarshalText() (text []byte, err error) {
return lvl.Level().MarshalText()
Expand Down
16 changes: 16 additions & 0 deletions logger.go
Expand Up @@ -172,6 +172,22 @@ func (log *Logger) Check(lvl zapcore.Level, msg string) *zapcore.CheckedEntry {
return log.check(lvl, msg)
}

// Trace 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) Verbose(msg string, fields ...Field) {
if ce := log.check(VerboseLevel, msg); ce != nil {
ce.Write(fields...)
}
}

// Trace 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) 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
2 changes: 2 additions & 0 deletions logger_test.go
Expand Up @@ -174,6 +174,8 @@ func TestLoggerLeveledMethods(t *testing.T) {
method func(string, ...Field)
expectedLevel zapcore.Level
}{
{logger.Verbose, VerboseLevel},
{logger.Trace, TraceLevel},
{logger.Debug, DebugLevel},
{logger.Info, InfoLevel},
{logger.Warn, WarnLevel},
Expand Down
27 changes: 27 additions & 0 deletions sugar.go
Expand Up @@ -92,6 +92,16 @@ func (s *SugaredLogger) With(args ...interface{}) *SugaredLogger {
return &SugaredLogger{base: s.base.With(s.sweetenFields(args)...)}
}

// Trace uses fmt.Sprint to construct and log a message.
func (s *SugaredLogger) Verbose(args ...interface{}) {
s.log(VerboseLevel, "", args, nil)
}

// Trace uses fmt.Sprint to construct and log a message.
func (s *SugaredLogger) Trace(args ...interface{}) {
s.log(TraceLevel, "", args, nil)
}

// Debug uses fmt.Sprint to construct and log a message.
func (s *SugaredLogger) Debug(args ...interface{}) {
s.log(DebugLevel, "", args, nil)
Expand Down Expand Up @@ -128,6 +138,16 @@ func (s *SugaredLogger) Fatal(args ...interface{}) {
s.log(FatalLevel, "", args, nil)
}

// Verbosef uses fmt.Sprintf to log a templated message.
func (s *SugaredLogger) Verbosef(template string, args ...interface{}) {
s.log(VerboseLevel, template, args, nil)
}

// Tracef uses fmt.Sprintf to log a templated message.
func (s *SugaredLogger) Tracef(template string, args ...interface{}) {
s.log(TraceLevel, template, args, nil)
}

// Debugf uses fmt.Sprintf to log a templated message.
func (s *SugaredLogger) Debugf(template string, args ...interface{}) {
s.log(DebugLevel, template, args, nil)
Expand Down Expand Up @@ -164,6 +184,13 @@ func (s *SugaredLogger) Fatalf(template string, args ...interface{}) {
s.log(FatalLevel, template, args, nil)
}

func (s *SugaredLogger) Verbosew(msg string, keysAndValues ...interface{}) {
s.log(VerboseLevel, msg, nil, keysAndValues)
}

func (s *SugaredLogger) Tracew(msg string, keysAndValues ...interface{}) {
s.log(TraceLevel, msg, nil, keysAndValues)
}
// Debugw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
//
Expand Down
4 changes: 4 additions & 0 deletions zapcore/core_test.go
Expand Up @@ -49,6 +49,8 @@ func TestNopCore(t *testing.T) {
ce := &CheckedEntry{}

allLevels := []Level{
VerboseLevel,
TraceLevel,
DebugLevel,
InfoLevel,
WarnLevel,
Expand Down Expand Up @@ -129,6 +131,8 @@ func TestIOCoreSyncsOutput(t *testing.T) {
entry Entry
shouldSync bool
}{
{Entry{Level: VerboseLevel}, false},
{Entry{Level: TraceLevel}, false},
{Entry{Level: DebugLevel}, false},
{Entry{Level: InfoLevel}, false},
{Entry{Level: WarnLevel}, false},
Expand Down
19 changes: 17 additions & 2 deletions zapcore/level.go
Expand Up @@ -32,9 +32,12 @@ var errUnmarshalNilLevel = errors.New("can't unmarshal a nil *Level")
type Level int8

const (
VerboseLevel Level = iota - 1

TraceLevel
// 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 +54,17 @@ const (
// FatalLevel logs a message, then calls os.Exit(1).
FatalLevel

_minLevel = DebugLevel
_minLevel = VerboseLevel
_maxLevel = FatalLevel
)

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

func (l *Level) unmarshalText(text []byte) bool {
switch string(text) {
case "verbose", "VERBOSE":
*l = VerboseLevel
case "trace", "TRACE":
*l = TraceLevel
case "debug", "DEBUG":
*l = DebugLevel
case "info", "INFO", "": // make the zero value useful
Expand Down
2 changes: 2 additions & 0 deletions zapcore/level_strings.go
Expand Up @@ -24,6 +24,8 @@ import "go.uber.org/zap/internal/color"

var (
_levelToColor = map[Level]color.Color{
VerboseLevel: color.Cyan,
TraceLevel: color.Cyan,
DebugLevel: color.Magenta,
InfoLevel: color.Blue,
WarnLevel: color.Yellow,
Expand Down
12 changes: 11 additions & 1 deletion zapcore/level_test.go
Expand Up @@ -31,6 +31,8 @@ import (

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

// What even is...
{"VeRBosE", VerboseLevel},
{"TrAcE", TraceLevel},
{"DeBuG", DebugLevel},
{"InFo", InfoLevel},
{"WaRn", WarnLevel},
Expand Down Expand Up @@ -159,7 +169,7 @@ func TestLevelAsFlagValue(t *testing.T) {
fs.SetOutput(&buf)
fs.Var(&lvl, "level", "log level")

for _, expected := range []Level{DebugLevel, InfoLevel, WarnLevel, ErrorLevel, DPanicLevel, PanicLevel, FatalLevel} {
for _, expected := range []Level{VerboseLevel, TraceLevel, DebugLevel, InfoLevel, WarnLevel, ErrorLevel, DPanicLevel, PanicLevel, FatalLevel} {
assert.NoError(t, fs.Parse([]string{"-level", expected.String()}))
assert.Equal(t, expected, lvl, "Unexpected level after parsing flag.")
assert.Equal(t, expected, lvl.Get(), "Unexpected output using flag.Getter API.")
Expand Down

0 comments on commit 6484585

Please sign in to comment.