Logging Configuration


目录

The logging configuration of Forgejo mainly consists of 3 types of components:

There is a fully functional log output by default, so it is not necessary to define one.

The [log] section

Configuration of logging facilities in Forgejo happens in the [log] section and its subsections.

The top level section [log] has the following properties:

The [log] section can contain sub-logger modes as well:

Setting a sub-logger's mode to a comma (,) makes it use the global MODE.

Quick samples

Default (empty) Configuration

The empty configuration is equivalent to default:

[log]
ROOT_PATH = %(GITEA_WORK_DIR)/log
MODE = console
LEVEL = Info
STACKTRACE_LEVEL = None
LOGGER_DEFAULT_MODE = ,
LOGGER_ROUTER_MODE = ,
LOGGER_XORM_MODE = ,
LOGGER_ACCESS_MODE =

; this is the config options of "console" mode (used by MODE=console above)
[log.console]
MODE = console
FLAGS = stdflags
PREFIX =
COLORIZE = true

This is equivalent to sending all logs to the console, with default Golang log being sent to the console log too.

This is only a sample, and it is the default, do not need to write it into your configuration file.

Disable Router logs and record some access logs to file

The Router logger is disabled, the access logs (>=Warn) goes into access.log:

[log]
LOGGER_ROUTER_MODE =
LOGGER_ACCESS_MODE = access-file

[log.access-file]
MODE = file
LEVEL = Warn
FILE_NAME = access.log

Set different log levels for different modes

Default logs (>=Warn) goes into forgejo.log, while Error logs goes into file-error.log:

[log]
LEVEL = Warn
MODE = file, file-error

; by default, the "file" mode will record logs to %(log.ROOT_PATH)/forgejo.log, so we don't need to set it
; [log.file]
; by default, the MODE (actually it's the output writer of this logger) is taken from the section name, so we don't need to set it either
; MODE = file

[log.file-error]
MODE = file
LEVEL = Error
FILE_NAME = file-error.log

Log outputs (mode and writer)

Forgejo provides the following log output writers:

Common configuration

Certain configuration is common to all modes of log output:

EXPRESSION

EXPRESSION represents a regular expression that log events must match to be logged by the output writer. Either the log message, (with colors removed), must match or the longfilename:linenumber:functionname must match. NB: the whole message or string doesn't need to completely match.

Please note this expression will be run in the writer's goroutine but not the logging event goroutine.

EXCLUSION

EXCLUSION represents a regular expression that log events must not match in order to be logged by the output writer. Either the log message (with colors removed) or the longfilename:linenumber:functionname must not match the exclusion pattern. NB: the whole message or string doesn't need to completely match.

Please note this exclusion will be run in the writer's goroutine but not the logging event goroutine.

For example, you can collect logs from the Router, but requests to /api/healthz and /api/metrics are excluded:

[log.router]
MODE  = console
LEVEL = INFO
EXCLUSION = (\/api\/healthz|\/metrics)

FLAGS

FLAGS represents the preceding logging context information that is printed before each message. It is a comma-separated string set. The order of values does not matter.

It defaults to stdflags (= date,time,medfile,shortfuncname,levelinitial). Except for the access log which defaults to none

Possible values are:

Console mode

In this mode the logger will forward log messages to the stdout and stderr streams attached to the Forgejo process.

For loggers in console mode, COLORIZE will default to true when appropriate:

Settings:

journald mode

As an extension to the console mode, if Forgejo detects that stdout and/or stderr are connected to systemd-journald (which will happen automatically when Forgejo is running under systemd), the defaults will change:

When FLAGS = journaldflags is used, instead of decorating log entries with color or a textual representation of severity (e.g. [INFO] or [ERROR]), Forgejo will annotate each log entry with a machine-readable log level prefix that is captured by journald, enabling native filtering capabilities (e.g. run journalctl -u forgejo -p err to only show errors or worse).

In short, if Forgejo is running under systemd, it is recommended to leave the logging configuration at their defaults, which will use MODE = console and adjust formatting to reduce clutter and enable native log level filtering.

File mode

In this mode the logger will save log messages to a file.

Settings:

MAX_SIZE_SHIFT defines the maximum size of a file by left shifting 1 the given number of times (1 << x).

The useful values of COMPRESSION_LEVEL are from 1 to (and including) 9, where higher numbers mean better compression. Beware that better compression might come with higher resource usage. Must be preceded with a - sign.

Please note, for backward compatibility, a gitea.log symbolic link to FILE_NAME is created when FILE_NAME is not set in your configuration file. If you want to get rid of this symbolic link, set FILE_NAME to forgejo.log (or any other name) in your configuration file and manually remove the gitea.log symbolic link.

Conn mode

In this mode the logger will send log messages over a network socket.

Settings:

The "Router" logger

The Router logger logs the following message types when Forgejo's route handlers work:

The "XORM" logger

To make XORM outputs SQL logs, the LOG_SQL in [database] section should also be set to true.

The "Access" logger

The Access logger provides a NCSA Common Log compliant log format. It's highly configurable but caution should be taken when changing its template. The main benefit of this logger is that Forgejo can now log accesses in a standard log format so standard tools may be used.

You can enable this logger using LOGGER_ACCESS_MODE = ....

If desired the format of the Access logger can be changed by changing the value of the ACCESS_LOG_TEMPLATE.

Please note, the access logger will log at INFO level, setting the LEVEL of this logger to WARN or above will result in no access logs.

The ACCESS_LOG_TEMPLATE

This value represents a go template. Its default value is

{{.Ctx.RemoteHost}} - {{.Identity}} {{.Start.Format "[02/Jan/2006:15:04:05 -0700]" }} "{{.Ctx.Req.Method}} {{.Ctx.Req.URL.RequestURI}} {{.Ctx.Req.Proto}}" {{.ResponseWriter.Status}} {{.ResponseWriter.Size}} "{{.Ctx.Req.Referer}}" "{{.Ctx.Req.UserAgent}}"`

The template is passed following options:

Caution must be taken when changing this template as it runs outside of the standard panic recovery trap. The template should also be as simple as it runs for every request..

Releasing-and-Reopening, Pausing and Resuming logging

If you are running on Unix you may wish to release-and-reopen logs in order to use logrotate or other tools. It is possible force Forgejo to release and reopen its logging files and connections by sending SIGUSR1 to the running process, or running forgejo manager logging release-and-reopen.

Alternatively, you may wish to pause and resume logging - this can be accomplished through the use of the forgejo manager logging pause and forgejo manager logging resume commands. Please note that whilst logging is paused log events below INFO level will not be stored and only a limited number of events will be stored. Logging may block, albeit temporarily, slowing Forgejo considerably whilst paused - therefore it is recommended that pausing only done for a very short period of time.

Adding and removing logging whilst Forgejo is running

It is possible to add and remove logging whilst Forgejo is running using the forgejo manager logging add and remove subcommands. This functionality can only adjust running log systems and cannot be used to start the access or router loggers if they were not already initialized. If you wish to start these systems you are advised to adjust the app.ini and (gracefully) restart the Forgejo service.

The main intention of these commands is to easily add a temporary logger to investigate problems on running systems where a restart may cause the issue to disappear.

Using logrotate instead of built-in log rotation

Forgejo includes built-in log rotation, which should be enough for most deployments. However, if you instead want to use the logrotate utility:

The next logrotate jobs will include your configurations, so no restart is needed. You can also immediately reload logrotate with logrotate /etc/logrotate.conf --force.


版本号 #2
由 壳壳虫 创建于 2026-10-31 13:34:16 UTC
由 壳壳虫 更新于 2026-10-31 13:34:17 UTC