mirror of
https://codeberg.org/forgejo/forgejo
synced 2024-08-26 09:31:56 +02:00
## ⚠️ Breaking The `log.<mode>.<logger>` style config has been dropped. If you used it, please check the new config manual & app.example.ini to make your instance output logs as expected. Although many legacy options still work, it's encouraged to upgrade to the new options. The SMTP logger is deleted because SMTP is not suitable to collect logs. If you have manually configured Gitea log options, please confirm the logger system works as expected after upgrading. ## Description Close #12082 and maybe more log-related issues, resolve some related FIXMEs in old code (which seems unfixable before) Just like rewriting queue #24505 : make code maintainable, clear legacy bugs, and add the ability to support more writers (eg: JSON, structured log) There is a new document (with examples): `logging-config.en-us.md` This PR is safer than the queue rewriting, because it's just for logging, it won't break other logic. ## The old problems The logging system is quite old and difficult to maintain: * Unclear concepts: Logger, NamedLogger, MultiChannelledLogger, SubLogger, EventLogger, WriterLogger etc * Some code is diffuclt to konw whether it is right: `log.DelNamedLogger("console")` vs `log.DelNamedLogger(log.DEFAULT)` vs `log.DelLogger("console")` * The old system heavily depends on ini config system, it's difficult to create new logger for different purpose, and it's very fragile. * The "color" trick is difficult to use and read, many colors are unnecessary, and in the future structured log could help * It's difficult to add other log formats, eg: JSON format * The log outputer doesn't have full control of its goroutine, it's difficult to make outputer have advanced behaviors * The logs could be lost in some cases: eg: no Fatal error when using CLI. * Config options are passed by JSON, which is quite fragile. * INI package makes the KEY in `[log]` section visible in `[log.sub1]` and `[log.sub1.subA]`, this behavior is quite fragile and would cause more unclear problems, and there is no strong requirement to support `log.<mode>.<logger>` syntax. ## The new design See `logger.go` for documents. ## Screenshot <details> ![image](https://github.com/go-gitea/gitea/assets/2114189/4462d713-ba39-41f5-bb08-de912e67e1ff) ![image](https://github.com/go-gitea/gitea/assets/2114189/b188035e-f691-428b-8b2d-ff7b2199b2f9) ![image](https://github.com/go-gitea/gitea/assets/2114189/132e9745-1c3b-4e00-9e0d-15eaea495dee) </details> ## TODO * [x] add some new tests * [x] fix some tests * [x] test some sub-commands (manually ....) --------- Co-authored-by: Jason Song <i@wolfogre.com> Co-authored-by: delvh <dev.lh@web.de> Co-authored-by: Giteabot <teabot@gitea.io>
247 lines
5.8 KiB
Go
247 lines
5.8 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package log
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Event struct {
|
|
Time time.Time
|
|
|
|
GoroutinePid string
|
|
Caller string
|
|
Filename string
|
|
Line int
|
|
|
|
Level Level
|
|
|
|
MsgSimpleText string
|
|
|
|
msgFormat string // the format and args is only valid in the caller's goroutine
|
|
msgArgs []any // they are discarded before the event is passed to the writer's channel
|
|
|
|
Stacktrace string
|
|
}
|
|
|
|
type EventFormatted struct {
|
|
Origin *Event
|
|
Msg any // the message formatted by the writer's formatter, the writer knows its type
|
|
}
|
|
|
|
type EventFormatter func(mode *WriterMode, event *Event, msgFormat string, msgArgs ...any) []byte
|
|
|
|
type logStringFormatter struct {
|
|
v LogStringer
|
|
}
|
|
|
|
var _ fmt.Formatter = logStringFormatter{}
|
|
|
|
func (l logStringFormatter) Format(f fmt.State, verb rune) {
|
|
if f.Flag('#') && verb == 'v' {
|
|
_, _ = fmt.Fprintf(f, "%#v", l.v)
|
|
return
|
|
}
|
|
_, _ = f.Write([]byte(l.v.LogString()))
|
|
}
|
|
|
|
// Copy of cheap integer to fixed-width decimal to ascii from logger.
|
|
// TODO: legacy bugs: doesn't support negative number, overflow if wid it too large.
|
|
func itoa(buf []byte, i, wid int) []byte {
|
|
var s [20]byte
|
|
bp := len(s) - 1
|
|
for i >= 10 || wid > 1 {
|
|
wid--
|
|
q := i / 10
|
|
s[bp] = byte('0' + i - q*10)
|
|
bp--
|
|
i = q
|
|
}
|
|
// i < 10
|
|
s[bp] = byte('0' + i)
|
|
return append(buf, s[bp:]...)
|
|
}
|
|
|
|
func colorSprintf(colorize bool, format string, args ...any) string {
|
|
hasColorValue := false
|
|
for _, v := range args {
|
|
if _, hasColorValue = v.(*ColoredValue); hasColorValue {
|
|
break
|
|
}
|
|
}
|
|
if colorize || !hasColorValue {
|
|
return fmt.Sprintf(format, args...)
|
|
}
|
|
|
|
noColors := make([]any, len(args))
|
|
copy(noColors, args)
|
|
for i, v := range args {
|
|
if cv, ok := v.(*ColoredValue); ok {
|
|
noColors[i] = cv.v
|
|
}
|
|
}
|
|
return fmt.Sprintf(format, noColors...)
|
|
}
|
|
|
|
// EventFormatTextMessage makes the log message for a writer with its mode. This function is a copy of the original package
|
|
func EventFormatTextMessage(mode *WriterMode, event *Event, msgFormat string, msgArgs ...any) []byte {
|
|
buf := make([]byte, 0, 1024)
|
|
buf = append(buf, mode.Prefix...)
|
|
t := event.Time
|
|
flags := mode.Flags.Bits()
|
|
if flags&(Ldate|Ltime|Lmicroseconds) != 0 {
|
|
if mode.Colorize {
|
|
buf = append(buf, fgCyanBytes...)
|
|
}
|
|
if flags&LUTC != 0 {
|
|
t = t.UTC()
|
|
}
|
|
if flags&Ldate != 0 {
|
|
year, month, day := t.Date()
|
|
buf = itoa(buf, year, 4)
|
|
buf = append(buf, '/')
|
|
buf = itoa(buf, int(month), 2)
|
|
buf = append(buf, '/')
|
|
buf = itoa(buf, day, 2)
|
|
buf = append(buf, ' ')
|
|
}
|
|
if flags&(Ltime|Lmicroseconds) != 0 {
|
|
hour, min, sec := t.Clock()
|
|
buf = itoa(buf, hour, 2)
|
|
buf = append(buf, ':')
|
|
buf = itoa(buf, min, 2)
|
|
buf = append(buf, ':')
|
|
buf = itoa(buf, sec, 2)
|
|
if flags&Lmicroseconds != 0 {
|
|
buf = append(buf, '.')
|
|
buf = itoa(buf, t.Nanosecond()/1e3, 6)
|
|
}
|
|
buf = append(buf, ' ')
|
|
}
|
|
if mode.Colorize {
|
|
buf = append(buf, resetBytes...)
|
|
}
|
|
|
|
}
|
|
if flags&(Lshortfile|Llongfile) != 0 {
|
|
if mode.Colorize {
|
|
buf = append(buf, fgGreenBytes...)
|
|
}
|
|
file := event.Filename
|
|
if flags&Lmedfile == Lmedfile {
|
|
startIndex := len(file) - 20
|
|
if startIndex > 0 {
|
|
file = "..." + file[startIndex:]
|
|
}
|
|
} else if flags&Lshortfile != 0 {
|
|
startIndex := strings.LastIndexByte(file, '/')
|
|
if startIndex > 0 && startIndex < len(file) {
|
|
file = file[startIndex+1:]
|
|
}
|
|
}
|
|
buf = append(buf, file...)
|
|
buf = append(buf, ':')
|
|
buf = itoa(buf, event.Line, -1)
|
|
if flags&(Lfuncname|Lshortfuncname) != 0 {
|
|
buf = append(buf, ':')
|
|
} else {
|
|
if mode.Colorize {
|
|
buf = append(buf, resetBytes...)
|
|
}
|
|
buf = append(buf, ' ')
|
|
}
|
|
}
|
|
if flags&(Lfuncname|Lshortfuncname) != 0 {
|
|
if mode.Colorize {
|
|
buf = append(buf, fgGreenBytes...)
|
|
}
|
|
funcname := event.Caller
|
|
if flags&Lshortfuncname != 0 {
|
|
lastIndex := strings.LastIndexByte(funcname, '.')
|
|
if lastIndex > 0 && len(funcname) > lastIndex+1 {
|
|
funcname = funcname[lastIndex+1:]
|
|
}
|
|
}
|
|
buf = append(buf, funcname...)
|
|
if mode.Colorize {
|
|
buf = append(buf, resetBytes...)
|
|
}
|
|
buf = append(buf, ' ')
|
|
}
|
|
|
|
if flags&(Llevel|Llevelinitial) != 0 {
|
|
level := strings.ToUpper(event.Level.String())
|
|
if mode.Colorize {
|
|
buf = append(buf, ColorBytes(levelToColor[event.Level]...)...)
|
|
}
|
|
buf = append(buf, '[')
|
|
if flags&Llevelinitial != 0 {
|
|
buf = append(buf, level[0])
|
|
} else {
|
|
buf = append(buf, level...)
|
|
}
|
|
buf = append(buf, ']')
|
|
if mode.Colorize {
|
|
buf = append(buf, resetBytes...)
|
|
}
|
|
buf = append(buf, ' ')
|
|
}
|
|
|
|
var msg []byte
|
|
|
|
// if the log needs colorizing, do it
|
|
if mode.Colorize && len(msgArgs) > 0 {
|
|
hasColorValue := false
|
|
for _, v := range msgArgs {
|
|
if _, hasColorValue = v.(*ColoredValue); hasColorValue {
|
|
break
|
|
}
|
|
}
|
|
if hasColorValue {
|
|
msg = []byte(fmt.Sprintf(msgFormat, msgArgs...))
|
|
}
|
|
}
|
|
// try to re-use the pre-formatted simple text message
|
|
if len(msg) == 0 {
|
|
msg = []byte(event.MsgSimpleText)
|
|
}
|
|
// if still no message, do the normal Sprintf for the message
|
|
if len(msg) == 0 {
|
|
msg = []byte(colorSprintf(mode.Colorize, msgFormat, msgArgs...))
|
|
}
|
|
// remove at most one trailing new line
|
|
if len(msg) > 0 && msg[len(msg)-1] == '\n' {
|
|
msg = msg[:len(msg)-1]
|
|
}
|
|
|
|
if flags&Lgopid == Lgopid {
|
|
if event.GoroutinePid != "" {
|
|
buf = append(buf, '[')
|
|
if mode.Colorize {
|
|
buf = append(buf, ColorBytes(FgHiYellow)...)
|
|
}
|
|
buf = append(buf, event.GoroutinePid...)
|
|
if mode.Colorize {
|
|
buf = append(buf, resetBytes...)
|
|
}
|
|
buf = append(buf, ']', ' ')
|
|
}
|
|
}
|
|
buf = append(buf, msg...)
|
|
|
|
if event.Stacktrace != "" && mode.StacktraceLevel <= event.Level {
|
|
lines := bytes.Split([]byte(event.Stacktrace), []byte("\n"))
|
|
for _, line := range lines {
|
|
buf = append(buf, "\n\t"...)
|
|
buf = append(buf, line...)
|
|
}
|
|
buf = append(buf, '\n')
|
|
}
|
|
buf = append(buf, '\n')
|
|
return buf
|
|
}
|