Logs

Overview

go-zero provides a powerful log package, logx and logc can be used by users for log printing.

logx and logc

The difference between these two packages, logc is a packing of logx, and we can use the context to print logs. The following code is equivalent,

  1. logx.WithContext(ctx).Info("hello world")
  2. logc.Info(ctx, "hello world")

Basic Use of Logs

We offer various shortcuts for printing logs.The following is

  1. ctx := context.Background()
  2. logc.Info(ctx, "info message")
  3. logc.Errorf(ctx, "error message: %d", 123)
  4. logc.Debugw(ctx, "info filed", logc.Field("key", "value"))
  5. logc.Slowv(ctx, "object")

For more information see logc and logx

Logging initialization and associated configuration

We provide rich log setup capability, which can be configured by configuration. For details see log configuration

We can initialize through the following modalities.

  1. logx.MustSetup(logx.LogConf{})

Redirect Log Output

In go-zero we can redirect the output of the log, to do so as follows.

  1. func SetWriter(w Writer)

Specific use such as:

  1. logx.SetWriter(logx.NewWriter(os.Stdout))

We redirect the log to stdout, and of course can encapsulate the respective output paths.

The level of log entry.

go-zero has two ways to set level, one set by configuration, seelog configuration

There is another setting via logx.SetLevel() The supported log level is as follows.

  1. const (
  2. // DebugLevel logs everything
  3. DebugLevel uint32 = iota
  4. // InfoLevel does not include debugs
  5. InfoLevel
  6. // ErrorLevel includes errors, slows, stacks
  7. ErrorLevel
  8. // SevereLevel only log severe messages
  9. SevereLevel
  10. )

This method is a secure thread and can be implemented by adjusting log levels during the business execution.

Log closed

Since log printing is asynchronous, we need to close the log when the program exits, otherwise there may be a loss of the log.

  1. logc.Close()

Commentary, we have already closed the log in zrpc,reset

Reset Log

In some special business processes, if we set the writer to reset the writer and can use the following method.

  1. logx.Reset()

In this way, all logs will be turned into default output mode and you can initialize your log again if needed.

Log caller settings

The number of lines that the go-zero will print the current log by default.When we encapsulate some methods, we need to know the last call level, using logx.CixCallerSkip(1) to set the level of the caller, for example:

  1. package main
  2. import (
  3. "github.com/zeromicro/go-zero/core/logx"
  4. )
  5. func main() {
  6. exec()
  7. }
  8. func exec() error {
  9. logx.WithCallerSkip(1).Info("exec info") // {"@timestamp":"2023-04-23T17:30:09.962+08:00","caller":"inherit/main.go:8","content":"exec info","level":"info"}
  10. return nil
  11. }

As above we can print exec locations. Such access to encapsulation methods is particularly effective.

Log File Split

go-zero in file output mode supports split between two files by day and by size. For more information seeRotation

Under DateSplit mode, go-zero will be backed up by access .log, error.log, stat.log, slow.log and create new log files for log printing. The number of logs will also be judged and will be removed from the old configuration file if more than KeepDays settings.

In size split mode, go-zero will record the size of the current log file, more than MaxSize will split the log.

Output to both file and console

We might expect to output to both file and console for debugging purpose. Use it with caution, performance might be affected.

Demo code below:

  1. package main
  2. import (
  3. "os"
  4. "time"
  5. "github.com/zeromicro/go-zero/core/conf"
  6. "github.com/zeromicro/go-zero/core/logx"
  7. "github.com/zeromicro/go-zero/core/proc"
  8. )
  9. func main() {
  10. var c logx.LogConf
  11. conf.MustLoad("config.yaml", &c)
  12. logx.MustSetup(c)
  13. logx.AddWriter(logx.NewWriter(os.Stdout))
  14. for {
  15. select {
  16. case <-proc.Done():
  17. return
  18. default:
  19. time.Sleep(time.Second)
  20. logx.Info(time.Now())
  21. }
  22. }
  23. }

Demo config file:

  1. Mode: file
  2. Encoding: json