mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-24 08:57:03 -05:00
012a1e0497
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>
114 lines
3.6 KiB
Go
114 lines
3.6 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package log
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestItoa(t *testing.T) {
|
|
b := itoa(nil, 0, 0)
|
|
assert.Equal(t, "0", string(b))
|
|
|
|
b = itoa(nil, 0, 1)
|
|
assert.Equal(t, "0", string(b))
|
|
|
|
b = itoa(nil, 0, 2)
|
|
assert.Equal(t, "00", string(b))
|
|
}
|
|
|
|
func TestEventFormatTextMessage(t *testing.T) {
|
|
res := EventFormatTextMessage(&WriterMode{Prefix: "[PREFIX] ", Colorize: false, Flags: Flags{defined: true, flags: 0xffffffff}},
|
|
&Event{
|
|
Time: time.Date(2020, 1, 2, 3, 4, 5, 6, time.UTC),
|
|
Caller: "caller",
|
|
Filename: "filename",
|
|
Line: 123,
|
|
GoroutinePid: "pid",
|
|
Level: ERROR,
|
|
Stacktrace: "stacktrace",
|
|
},
|
|
"msg format: %v %v", "arg0", NewColoredValue("arg1", FgBlue),
|
|
)
|
|
|
|
assert.Equal(t, `<3>[PREFIX] 2020/01/02 03:04:05.000000 filename:123:caller [E] [pid] msg format: arg0 arg1
|
|
stacktrace
|
|
|
|
`, string(res))
|
|
|
|
res = EventFormatTextMessage(&WriterMode{Prefix: "[PREFIX] ", Colorize: true, Flags: Flags{defined: true, flags: 0xffffffff}},
|
|
&Event{
|
|
Time: time.Date(2020, 1, 2, 3, 4, 5, 6, time.UTC),
|
|
Caller: "caller",
|
|
Filename: "filename",
|
|
Line: 123,
|
|
GoroutinePid: "pid",
|
|
Level: ERROR,
|
|
Stacktrace: "stacktrace",
|
|
},
|
|
"msg format: %v %v", "arg0", NewColoredValue("arg1", FgBlue),
|
|
)
|
|
|
|
assert.Equal(t, "<3>[PREFIX] \x1b[36m2020/01/02 03:04:05.000000 \x1b[0m\x1b[32mfilename:123:\x1b[32mcaller\x1b[0m \x1b[1;31m[E]\x1b[0m [\x1b[93mpid\x1b[0m] msg format: arg0 \x1b[34marg1\x1b[0m\n\tstacktrace\n\n", string(res))
|
|
}
|
|
|
|
func TestEventFormatTextMessageStd(t *testing.T) {
|
|
res := EventFormatTextMessage(&WriterMode{Prefix: "[PREFIX] ", Colorize: false, Flags: Flags{defined: true, flags: LstdFlags}},
|
|
&Event{
|
|
Time: time.Date(2020, 1, 2, 3, 4, 5, 6, time.UTC),
|
|
Caller: "caller",
|
|
Filename: "filename",
|
|
Line: 123,
|
|
GoroutinePid: "pid",
|
|
Level: ERROR,
|
|
Stacktrace: "stacktrace",
|
|
},
|
|
"msg format: %v %v", "arg0", NewColoredValue("arg1", FgBlue),
|
|
)
|
|
|
|
assert.Equal(t, `[PREFIX] 2020/01/02 03:04:05 filename:123:caller [E] msg format: arg0 arg1
|
|
stacktrace
|
|
|
|
`, string(res))
|
|
|
|
res = EventFormatTextMessage(&WriterMode{Prefix: "[PREFIX] ", Colorize: true, Flags: Flags{defined: true, flags: LstdFlags}},
|
|
&Event{
|
|
Time: time.Date(2020, 1, 2, 3, 4, 5, 6, time.UTC),
|
|
Caller: "caller",
|
|
Filename: "filename",
|
|
Line: 123,
|
|
GoroutinePid: "pid",
|
|
Level: ERROR,
|
|
Stacktrace: "stacktrace",
|
|
},
|
|
"msg format: %v %v", "arg0", NewColoredValue("arg1", FgBlue),
|
|
)
|
|
|
|
assert.Equal(t, "[PREFIX] \x1b[36m2020/01/02 03:04:05 \x1b[0m\x1b[32mfilename:123:\x1b[32mcaller\x1b[0m \x1b[1;31m[E]\x1b[0m msg format: arg0 \x1b[34marg1\x1b[0m\n\tstacktrace\n\n", string(res))
|
|
}
|
|
|
|
func TestEventFormatTextMessageJournal(t *testing.T) {
|
|
// TODO: it makes no sense to emit \n-containing messages to journal as they will get mangled
|
|
// the proper way here is to attach the backtrace as structured metadata, but we can't do that via stderr
|
|
res := EventFormatTextMessage(&WriterMode{Prefix: "[PREFIX] ", Colorize: false, Flags: Flags{defined: true, flags: LjournaldFlags}},
|
|
&Event{
|
|
Time: time.Date(2020, 1, 2, 3, 4, 5, 6, time.UTC),
|
|
Caller: "caller",
|
|
Filename: "filename",
|
|
Line: 123,
|
|
GoroutinePid: "pid",
|
|
Level: ERROR,
|
|
Stacktrace: "stacktrace",
|
|
},
|
|
"msg format: %v %v", "arg0", NewColoredValue("arg1", FgBlue),
|
|
)
|
|
|
|
assert.Equal(t, `<3>[PREFIX] msg format: arg0 arg1
|
|
stacktrace
|
|
|
|
`, string(res))
|
|
}
|