log: journald integration (#2869)

Provide a bit more journald integration. Specifically:

- support emission of printk-style log level prefixes, documented in [`sd-daemon`(3)](https://man7.org/linux/man-pages/man3/sd-daemon.3.html#DESCRIPTION), that allow journald to automatically annotate stderr log lines with their level;
- add a new "journaldflags" item that is supposed to be used in place of "stdflags" when under journald to reduce log clutter (i. e. strip date/time info to avoid duplication, and use log level prefixes instead of textual log levels);
- detect whether stderr and/or stdout are attached to journald by parsing `$JOURNAL_STREAM` environment variable and adjust console logger defaults accordingly.

<!--start release-notes-assistant-->

## Draft release notes
<!--URL:https://codeberg.org/forgejo/forgejo-->
- Features
  - [PR](https://codeberg.org/forgejo/forgejo/pulls/2869): <!--number 2869 --><!--line 0 --><!--description bG9nOiBqb3VybmFsZCBpbnRlZ3JhdGlvbg==-->log: journald integration<!--description-->
<!--end release-notes-assistant-->

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/2869
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Ivan Shapovalov <intelfx@intelfx.name>
Co-committed-by: Ivan Shapovalov <intelfx@intelfx.name>
This commit is contained in:
Ivan Shapovalov 2024-08-09 07:49:13 +00:00 committed by Earl Warren
parent a72763f5a3
commit 012a1e0497
9 changed files with 169 additions and 18 deletions

View file

@ -31,9 +31,11 @@ const (
Llevelinitial // Initial character of the provided level in brackets, eg. [I] for info
Llevel // Provided level in brackets [INFO]
Lgopid // the Goroutine-PID of the context
Llevelprefix // printk-style logging prefixes as documented in sd-daemon(3), used by journald
Lmedfile = Lshortfile | Llongfile // last 20 characters of the filename
LstdFlags = Ldate | Ltime | Lmedfile | Lshortfuncname | Llevelinitial // default
Lmedfile = Lshortfile | Llongfile // last 20 characters of the filename
LstdFlags = Ldate | Ltime | Lmedfile | Lshortfuncname | Llevelinitial // default
LjournaldFlags = Llevelprefix
)
const Ldefault = LstdFlags
@ -54,10 +56,12 @@ var flagFromString = map[string]uint32{
"utc": LUTC,
"levelinitial": Llevelinitial,
"level": Llevel,
"levelprefix": Llevelprefix,
"gopid": Lgopid,
"medfile": Lmedfile,
"stdflags": LstdFlags,
"medfile": Lmedfile,
"stdflags": LstdFlags,
"journaldflags": LjournaldFlags,
}
var flagComboToString = []struct {