log.Trace("Something very low level.") log.Debug("Useful debugging information.") log.Info("Something noteworthy happened!") log.Warn("You should probably take a look at this.") log.Error("Something failed but I'm not quitting.") // 在输出日志之后调用os.Exit(1) log.Fatal("Bye.") // 在输出日志之后调用panic() log.Panic("I'm bailing.")
// TextFormatter formats logs into text type TextFormatter struct { // Set to true to bypass checking for a TTY before outputting colors. ForceColors bool
// Force disabling colors. DisableColors bool
// Force quoting of all values ForceQuote bool
// DisableQuote disables quoting for all values. // DisableQuote will have a lower priority than ForceQuote. // If both of them are set to true, quote will be forced on all values. DisableQuote bool
// Override coloring based on CLICOLOR and CLICOLOR_FORCE. - https://bixense.com/clicolors/ EnvironmentOverrideColors bool
// Disable timestamp logging. useful when output is redirected to logging // system that already adds timestamps. DisableTimestamp bool
// Enable logging the full timestamp when a TTY is attached instead of just // the time passed since beginning of execution. FullTimestamp bool
// TimestampFormat to use for display when a full timestamp is printed. // The format to use is the same than for time.Format or time.Parse from the standard // library. // The standard Library already provides a set of predefined format. TimestampFormat string
// The fields are sorted by default for a consistent output. For applications // that log extremely frequently and don't use the JSON formatter this may not // be desired. DisableSorting bool
// The keys sorting function, when uninitialized it uses sort.Strings. SortingFunc func([]string)
// Disables the truncation of the level text to 4 characters. DisableLevelTruncation bool
// PadLevelText Adds padding the level text so that all the levels output at the same length // PadLevelText is a superset of the DisableLevelTruncation option PadLevelText bool
// QuoteEmptyFields will wrap empty fields in quotes if true QuoteEmptyFields bool
// Whether the logger's out is to a terminal isTerminal bool
// FieldMap allows users to customize the names of keys for default fields. // As an example: // formatter := &TextFormatter{ // FieldMap: FieldMap{ // FieldKeyTime: "@timestamp", // FieldKeyLevel: "@level", // FieldKeyMsg: "@message"}} FieldMap FieldMap
// CallerPrettyfier can be set by the user to modify the content // of the function and file keys in the data when ReportCaller is // activated. If any of the returned value is the empty string the // corresponding key will be removed from fields. CallerPrettyfier func(*runtime.Frame) (function string, file string)
terminalInitOnce sync.Once
// The max length of the level text, generated dynamically on init levelTextMaxLength int }
~/codeSet/goCode/test » go run main.go {"age":18,"dep":"???","level":"info","msg":"Info!!","name":"Arvin","time":"2022-01-18T15:27:03+08:00"} {"age":18,"dep":"???","level":"warning","msg":"Warn!!!","name":"Arvin","time":"2022-01-18T15:27:03+08:00"}
我们看一下Fields类型的定义:
1 2
// Fields type, used to pass to `WithFields`. type Fields map[string]interface{}
// A hook to be fired when logging on the logging levels returned from // `Levels()` on your implementation of the interface. Note that this is not // fired in a goroutine or a channel with workers, you should handle such // functionality yourself if your call is non-blocking and you don't wish for // the logging calls for levels returned from `Levels()` to block. type Hook interface { Levels() []Level Fire(*Entry) error }