Code walkthrough: Gitea logger

This is one of the Code walkthrough I wrote while exploring the Gitea codebase.

Gitea logging configuration has extenstive documentation and is implemented in modules/log. It is mostly used by calling log.Error, log.Warn, etc. which all call log.Log with the corresponding log level.

The log.Log function does not do much except calling the EventLog method of MultiChannelledLogger. The object on which this method is called is found in a map at the name log.DEFAULT and loaded. This object is created from the configuration when Gitea starts. It contains multiple (hence the name) EventLogger created from the configuration and each of them will receive an Event representing the log message.

The LogEvent method of ChannelledLog is called with the Event matching the log message and passes it to a queue that is read from the Start method of ChannelledLog and then sent to the LoggerProvider that will write it to a file, send it via mail, etc. The queuing of log messages ensures that goroutines won’t race with each other. In addition, the Write method of each provider (see FileLogger for an example) is guarded by mutexes so that messages are not intermixed.

1 Like