# Logging Output

In Gone, an internal Goner is provided for logging output - logrus, which is implemented by wrapping the github.com/sirupsen/logrus package as a Goner. It is hoped that other logging output packages will be encapsulated by contributors to provide more logging solutions.

tip: To understand the core concepts and terminology of Gone, please read: Core Concepts of Gone (opens new window)

Here, we use the BasePriest from the package github.com/gone-io/gone/tree/main/goner to bury related Goners. In the BasePriest, Goners related to configuration and trace are also buried into the Cemetery, as these three packages are commonly used together.

func MasterPriest(cemetery gone.Cemetery) error {
	_ = goner.BasePriest(cemetery)
	// Bury other Goners
	return nil
}

tip: Supporting Configuration Files through Built-in Goners (opens new window)

Supported Configuration Items

  • log.level: Log level, default is info; supported levels:
    • panic
    • fatal
    • error
    • warn or warning
    • info
    • debug
    • trace
  • log.report-caller: If true, log will print the file name and line number of the log output position, for example: 2024-05-11 09:09:57.784|INFO|/Users/jim/go/pkg/mod/github.com/gone-io/gone@v0.1.4/goner/gin/server.go:46|061ad00f-8c0d-479c-bc4c-393e0cf2cca2|Server Listen At :8080
  • log.output: Log output location, default is stdout (standard output), supports stderr and a file path

    Best Practice: Deploy the application in containers and directly output logs to standard output, collected by a log collection component such as EFK.

# Logging

Use the Info method to log:

//...
type service struct {
    gone.Flag
    log logrus.Logger `gone:"gone-logger"` // Named injection into nested log attributes
}
func (svc *service) Business(input string) (string, error) {
    // Log
    svc.log.Infof("input content is %s", input)
    return input, nil
}
//...

For logging at other levels, refer to the interface code:

// Logger interface for logging
type Logger interface {
    Tracef(format string, args ...any)
    Debugf(format string, args ...any)
    Infof(format string, args ...any)
    Printf(format string, args ...any)
    Warnf(format string, args ...any)
    Warningf(format string, args ...any)
    Errorf(format string, args ...any)
    Fatalf(format string, args ...any)
    Panicf(format string, args ...any)
    Trace(args ...any)
    Debug(args ...any)
    Info(args ...any)
    Print(args ...any)
    Warn(args ...any)
    Warning(args ...any)
    Error(args ...any)
    Fatal(args ...any)
    Panic(args ...any)
    Traceln(args ...any)
    Debugln(args ...any)
    Infoln(args ...any)
    Println(args ...any)
    Warnln(args ...any)
    Warningln(args ...any)
    Errorln(args ...any)
    Fatalln(args ...any)
    Panicln(args ...any)
}

# Log Format

${Log Output Time}|${Log Level}|${Source Code Position of Log Output}|${TraceId}|${Log Content}

For example:

2024-05-11 09:09:57.784|INFO|**/Users/jim/go/pkg/mod/github.com/gone-io/gone@v0.1.4/goner/gin/server.go:46**|061ad00f-8c0d-479c-bc4c-393e0cf2cca2|Server Listen At :8080

# About TraceId

In web applications, we want a unified ID to identify logs generated by the same request. This unified ID is the TraceId. If this ID exists, when troubleshooting, we can search for logs using this ID to obtain all logs related to the request.