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 15 kB

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