You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

logging-documentation.en-us.md 16 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. ---
  2. date: "2019-04-02T17:06:00+01:00"
  3. title: "Advanced: Logging Configuration"
  4. slug: "logging-configuration"
  5. weight: 55
  6. toc: true
  7. draft: false
  8. menu:
  9. sidebar:
  10. parent: "advanced"
  11. name: "Logging Configuration"
  12. weight: 55
  13. identifier: "logging-configuration"
  14. ---
  15. # Logging Configuration
  16. The logging framework has been revamped in Gitea 1.9.0.
  17. ## Log Groups
  18. The fundamental thing to be aware of in Gitea is that there are several
  19. log groups:
  20. * The "Default" logger
  21. * The Macaron logger
  22. * The Router logger
  23. * The Access logger
  24. * The XORM logger
  25. * A logger called the `GitLogger` which is used during hooks.
  26. There is also the go log logger.
  27. ### The go log logger
  28. Go provides its own extremely basic logger in the `log` package,
  29. however, this is not sufficient for our purposes as it does not provide
  30. a way of logging at multiple levels, nor does it provide a good way of
  31. controlling where these logs are logged except through setting of a
  32. writer.
  33. We have therefore redirected this logger to our Default logger, and we
  34. will log anything that is logged using the go logger at the INFO level.
  35. ### The "Default" logger
  36. Calls to `log.Info`, `log.Debug`, `log.Error` etc. from the `code.gitea.io/gitea/modules/log` package will log to this logger.
  37. You can configure the outputs of this logger by setting the `MODE`
  38. value in the `[log]` section of the configuration.
  39. Each output sublogger is configured in a separate `[log.sublogger]`
  40. section, but there are certain default values. These will not be inherited from the `[log]` section:
  41. * `FLAGS` is `stdflags` (Equal to
  42. `date,time,medfile,shortfuncname,levelinitial`)
  43. * `FILE_NAME` will default to `%(ROOT_PATH)/gitea.log`
  44. * `EXPRESSION` will default to `""`
  45. * `PREFIX` will default to `""`
  46. The provider type of the sublogger can be set using the `MODE` value in
  47. its subsection, but will default to the name. This allows you to have
  48. multiple subloggers that will log to files.
  49. ### The "Macaron" logger
  50. By default Macaron will log to its own go `log` instance. This writes
  51. to `os.Stdout`. You can redirect this log to a Gitea configurable logger
  52. through setting the `REDIRECT_MACARON_LOG` setting in the `[log]`
  53. section which you can configure the outputs of by setting the `MACARON`
  54. value in the `[log]` section of the configuration. `MACARON` defaults
  55. to `file` if unset.
  56. Each output sublogger for this logger is configured in
  57. `[log.sublogger.macaron]` sections. There are certain default values
  58. which will not be inherited from the `[log]` or relevant
  59. `[log.sublogger]` sections:
  60. * `FLAGS` is `stdflags` (Equal to
  61. `date,time,medfile,shortfuncname,levelinitial`)
  62. * `FILE_NAME` will default to `%(ROOT_PATH)/macaron.log`
  63. * `EXPRESSION` will default to `""`
  64. * `PREFIX` will default to `""`
  65. NB: You can redirect the macaron logger to send its events to the gitea
  66. log using the value: `MACARON = ,`
  67. ### The "Router" logger
  68. There are two types of Router log. By default Macaron send its own
  69. router log which will be directed to Macaron's go `log`, however if you
  70. `REDIRECT_MACARON_LOG` you will enable Gitea's router log. You can
  71. disable both types of Router log by setting `DISABLE_ROUTER_LOG`.
  72. If you enable the redirect, you can configure the outputs of this
  73. router log by setting the `ROUTER` value in the `[log]` section of the
  74. configuration. `ROUTER` will default to `console` if unset. The Gitea
  75. Router logs the same data as the Macaron log but has slightly different
  76. coloring. It logs at the `Info` level by default, but this can be
  77. changed if desired by setting the `ROUTER_LOG_LEVEL` value.
  78. Each output sublogger for this logger is configured in
  79. `[log.sublogger.router]` sections. There are certain default values
  80. which will not be inherited from the `[log]` or relevant
  81. `[log.sublogger]` sections:
  82. * `FILE_NAME` will default to `%(ROOT_PATH)/router.log`
  83. * `FLAGS` defaults to `date,time`
  84. * `EXPRESSION` will default to `""`
  85. * `PREFIX` will default to `""`
  86. NB: You can redirect the router logger to send its events to the Gitea
  87. log using the value: `ROUTER = ,`
  88. ### The "Access" logger
  89. The Access logger is a new logger for version 1.9. It provides a NCSA
  90. Common Log compliant log format. It's highly configurable but caution
  91. should be taken when changing its template. The main benefit of this
  92. logger is that Gitea can now log accesses in a standard log format so
  93. standard tools may be used.
  94. You can enable this logger using `ENABLE_ACCESS_LOG`. Its outputs are
  95. configured by setting the `ACCESS` value in the `[log]` section of the
  96. configuration. `ACCESS` defaults to `file` if unset.
  97. Each output sublogger for this logger is configured in
  98. `[log.sublogger.access]` sections. There are certain default values
  99. which will not be inherited from the `[log]` or relevant
  100. `[log.sublogger]` sections:
  101. * `FILE_NAME` will default to `%(ROOT_PATH)/access.log`
  102. * `FLAGS` defaults to `` or None
  103. * `EXPRESSION` will default to `""`
  104. * `PREFIX` will default to `""`
  105. If desired the format of the Access logger can be changed by changing
  106. the value of the `ACCESS_LOG_TEMPLATE`.
  107. NB: You can redirect the access logger to send its events to the Gitea
  108. log using the value: `ACCESS = ,`
  109. #### The ACCESS_LOG_TEMPLATE
  110. This value represent a go template. It's default value is:
  111. `{{.Ctx.RemoteAddr}} - {{.Identity}} {{.Start.Format "[02/Jan/2006:15:04:05 -0700]" }} "{{.Ctx.Req.Method}} {{.Ctx.Req.RequestURI}} {{.Ctx.Req.Proto}}" {{.ResponseWriter.Status}} {{.ResponseWriter.Size}} "{{.Ctx.Req.Referer}}\" \"{{.Ctx.Req.UserAgent}}"`
  112. The template is passed following options:
  113. * `Ctx` is the `macaron.Context`
  114. * `Identity` is the `SignedUserName` or `"-"` if the user is not logged
  115. in
  116. * `Start` is the start time of the request
  117. * `ResponseWriter` is the `macaron.ResponseWriter`
  118. Caution must be taken when changing this template as it runs outside of
  119. the standard panic recovery trap. The template should also be as simple
  120. as it runs for every request.
  121. ### The "XORM" logger
  122. The XORM logger is a long-standing logger that exists to collect XORM
  123. log events. It is enabled by default but can be switched off by setting
  124. `ENABLE_XORM_LOG` to `false` in the `[log]` section. Its outputs are
  125. configured by setting the `XORM` value in the `[log]` section of the
  126. configuration. `XORM` defaults to `,` if unset, meaning it is redirected
  127. to the main Gitea log.
  128. XORM will log SQL events by default. This can be changed by setting
  129. the `LOG_SQL` value to `false` in the `[database]` section.
  130. Each output sublogger for this logger is configured in
  131. `[log.sublogger.xorm]` sections. There are certain default values
  132. which will not be inherited from the `[log]` or relevant
  133. `[log.sublogger]` sections:
  134. * `FILE_NAME` will default to `%(ROOT_PATH)/xorm.log`
  135. * `FLAGS` defaults to `date,time`
  136. * `EXPRESSION` will default to `""`
  137. * `PREFIX` will default to `""`
  138. ### The Hook and Serv "GitLoggers"
  139. These are less well defined loggers. Essentially these should only be
  140. used within Gitea's subsystems and cannot be configured at present.
  141. They will write log files in:
  142. * `%(ROOT_PATH)/hooks/pre-receive.log`
  143. * `%(ROOT_PATH)/hooks/update.log`
  144. * `%(ROOT_PATH)/hooks/post-receive.log`
  145. * `%(ROOT_PATH)/serv.log`
  146. * `%(ROOT_PATH)/http.log`
  147. In the future these logs may be rationalised.
  148. ## Log outputs
  149. Gitea provides 4 possible log outputs:
  150. * `console` - Log to `os.Stdout` or `os.Stderr`
  151. * `file` - Log to a file
  152. * `conn` - Log to a keep-alive TCP connection
  153. * `smtp` - Log via email
  154. Certain configuration is common to all modes of log output:
  155. * `LEVEL` is the lowest level that this output will log. This value
  156. is inherited from `[log]` and in the case of the non-default loggers
  157. from `[log.sublogger]`.
  158. * `STACKTRACE_LEVEL` is the lowest level that this output will print
  159. a stacktrace. This value is inherited.
  160. * `MODE` is the mode of the log output. It will default to the sublogger
  161. name. Thus `[log.console.macaron]` will default to `MODE = console`.
  162. * `COLORIZE` will default to `true` for `console` as
  163. described, otherwise it will default to `false`.
  164. ### Non-inherited default values
  165. There are several values which are not inherited as described above but
  166. rather default to those specific to type of logger, these are:
  167. `EXPRESSION`, `FLAGS`, `PREFIX` and `FILE_NAME`.
  168. #### `EXPRESSION`
  169. `EXPRESSION` represents a regular expression that log events must match to be logged by the sublogger. 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.
  170. Please note this expression will be run in the sublogger's goroutine
  171. not the logging event subroutine. Therefore it can be complicated.
  172. #### `FLAGS`
  173. `FLAGS` represents the preceding logging context information that is
  174. printed before each message. It is a comma-separated string set. The order of values does not matter.
  175. Possible values are:
  176. * `none` or `,` - No flags.
  177. * `date` - the date in the local time zone: `2009/01/23`.
  178. * `time` - the time in the local time zone: `01:23:23`.
  179. * `microseconds` - microsecond resolution: `01:23:23.123123`. Assumes
  180. time.
  181. * `longfile` - full file name and line number: `/a/b/c/d.go:23`.
  182. * `shortfile` - final file name element and line number: `d.go:23`.
  183. * `funcname` - function name of the caller: `runtime.Caller()`.
  184. * `shortfuncname` - last part of the function name. Overrides
  185. `funcname`.
  186. * `utc` - if date or time is set, use UTC rather than the local time
  187. zone.
  188. * `levelinitial` - Initial character of the provided level in brackets eg. `[I]` for info.
  189. * `level` - Provided level in brackets `[INFO]`
  190. * `medfile` - Last 20 characters of the filename - equivalent to
  191. `shortfile,longfile`.
  192. * `stdflags` - Equivalent to `date,time,medfile,shortfuncname,levelinitial`
  193. ### Console mode
  194. For loggers in console mode, `COLORIZE` will default to `true` if not
  195. on windows, or the windows terminal can be set into ANSI mode or is a
  196. cygwin or Msys pipe.
  197. If `STDERR` is set to `true` the logger will use `os.Stderr` instead of
  198. `os.Stdout`.
  199. ### File mode
  200. The `FILE_NAME` defaults as described above. If set it will be relative
  201. to the provided `ROOT_PATH` in the master `[log]` section.
  202. Other values:
  203. * `LOG_ROTATE`: **true**: Rotate the log files.
  204. * `MAX_SIZE_SHIFT`: **28**: Maximum size shift of a single file, 28 represents 256Mb.
  205. * `DAILY_ROTATE`: **true**: Rotate logs daily.
  206. * `MAX_DAYS`: **7**: Delete the log file after n days
  207. * `COMPRESS`: **true**: Compress old log files by default with gzip
  208. * `COMPRESSION_LEVEL`: **-1**: Compression level
  209. ### Conn mode
  210. * `RECONNECT_ON_MSG`: **false**: Reconnect host for every single message.
  211. * `RECONNECT`: **false**: Try to reconnect when connection is lost.
  212. * `PROTOCOL`: **tcp**: Set the protocol, either "tcp", "unix" or "udp".
  213. * `ADDR`: **:7020**: Sets the address to connect to.
  214. ### SMTP mode
  215. It is not recommended to use this logger to send general logging
  216. messages. However, you could perhaps set this logger to work on `FATAL`.
  217. * `USER`: User email address to send from.
  218. * `PASSWD`: Password for the smtp server.
  219. * `HOST`: **127.0.0.1:25**: The SMTP host to connect to.
  220. * `RECEIVERS`: Email addresses to send to.
  221. * `SUBJECT`: **Diagnostic message from Gitea**
  222. ## Default Configuration
  223. The default empty configuration is equivalent to:
  224. ```ini
  225. [log]
  226. ROOT_PATH = %(GITEA_WORK_DIR)/log
  227. MODE = console
  228. LEVEL = Info
  229. STACKTRACE_LEVEL = None
  230. REDIRECT_MACARON_LOG = false
  231. ENABLE_ACCESS_LOG = false
  232. ENABLE_XORM_LOG = true
  233. XORM = ,
  234. [log.console]
  235. MODE = console
  236. LEVEL = %(LEVEL)
  237. STACKTRACE_LEVEL = %(STACKTRACE_LEVEL)
  238. FLAGS = stdflags
  239. PREFIX =
  240. COLORIZE = true # Or false if your windows terminal cannot color
  241. ```
  242. This is equivalent to sending all logs to the console, with default go log being sent to the console log too.
  243. ## Log colorization
  244. Logs to the console will be colorized by default when not running on
  245. Windows. Terminal sniffing will occur on Windows and if it is
  246. determined that we are running on a terminal capable of color we will
  247. colorize.
  248. Further, on *nix it is becoming common to have file logs that are
  249. colored by default. Therefore file logs will be colorised by default
  250. when not running on Windows.
  251. You can switch on or off colorization by using the `COLORIZE` value.
  252. From a development point of view. If you write
  253. `log.Info("A %s string", "formatted")` the `formatted` part of the log
  254. message will be Bolded on colorized logs.
  255. You can change this by either rendering the formatted string yourself.
  256. Or you can wrap the value in a `log.ColoredValue` struct.
  257. The `log.ColoredValue` struct contains a pointer to value, a pointer to
  258. string of bytes which should represent a color and second set of reset
  259. bytes. Pointers were chosen to prevent copying of large numbers of
  260. values. There are several helper methods:
  261. * `log.NewColoredValue` takes a value and 0 or more color attributes
  262. that represent the color. If 0 are provided it will default to a cached
  263. bold. Note, it is recommended that color bytes constructed from
  264. attributes should be cached if this is a commonly used log message.
  265. * `log.NewColoredValuePointer` takes a pointer to a value, and
  266. 0 or more color attributes that represent the color.
  267. * `log.NewColoredValueBytes` takes a value and a pointer to an array
  268. of bytes representing the color.
  269. These functions will not double wrap a `log.ColoredValue`. They will
  270. also set the `resetBytes` to the cached `resetBytes`.
  271. The `colorBytes` and `resetBytes` are not exported to prevent
  272. accidental overwriting of internal values.
  273. ## ColorFormat & ColorFormatted
  274. Structs may implement the `log.ColorFormatted` interface by implementing the `ColorFormat(fmt.State)` function.
  275. If a `log.ColorFormatted` struct is logged with `%-v` format, its `ColorFormat` will be used instead of the usual `%v`. The full `fmt.State` will be passed to allow implementers to look at additional flags.
  276. In order to help implementers provide `ColorFormat` methods. There is a
  277. `log.ColorFprintf(...)` function in the log module that will wrap values in `log.ColoredValue` and recognise `%-v`.
  278. In general it is recommended not to make the results of this function too verbose to help increase its versatility. Usually this should simply be an `ID`:`Name`. If you wish to make a more verbose result, it is recommended to use `%-+v` as your marker.
  279. ## Log Spoofing protection
  280. In order to protect the logs from being spoofed with cleverly
  281. constructed messages. Newlines are now prefixed with a tab and control
  282. characters except those used in an ANSI CSI are escaped with a
  283. preceding `\` and their octal value.
  284. ## Creating a new named logger group
  285. Should a developer wish to create a new named logger, `NEWONE`. It is
  286. recommended to add an `ENABLE_NEWONE_LOG` value to the `[log]`
  287. section, and to add a new `NEWONE` value for the modes.
  288. A function like `func newNewOneLogService()` is recommended to manage
  289. construction of the named logger. e.g.
  290. ```go
  291. func newNewoneLogService() {
  292. EnableNewoneLog = Cfg.Section("log").Key("ENABLE_NEWONE_LOG").MustBool(false)
  293. Cfg.Section("log").Key("NEWONE").MustString("file") // or console? or "," if you want to send this to default logger by default
  294. if EnableNewoneLog {
  295. options := newDefaultLogOptions()
  296. options.filename = filepath.Join(LogRootPath, "newone.log")
  297. options.flags = "stdflags"
  298. options.bufferLength = Cfg.Section("log").Key("BUFFER_LEN").MustInt64(10000)
  299. generateNamedLogger("newone", options)
  300. }
  301. }
  302. ```
  303. You should then add `newOneLogService` to `NewServices()` in
  304. `modules/setting/setting.go`