mirror of
https://codeberg.org/davrot/forgejo.git
synced 2025-05-29 06:00:03 +02:00
Rewrite logger system (#24726)
## ⚠️ 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>    </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>
This commit is contained in:
parent
65dff8e364
commit
4647660776
109 changed files with 3806 additions and 5337 deletions
|
@ -1,9 +1,14 @@
|
|||
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package log
|
||||
|
||||
import "strings"
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
)
|
||||
|
||||
// These flags define which text to prefix to each log entry generated
|
||||
// by the Logger. Bits are or'ed together to control what's printed.
|
||||
|
@ -15,26 +20,30 @@ import "strings"
|
|||
// The standard is:
|
||||
// 2009/01/23 01:23:23 ...a/logger/c/d.go:23:runtime.Caller() [I]: message
|
||||
const (
|
||||
Ldate = 1 << iota // the date in the local time zone: 2009/01/23
|
||||
Ltime // the time in the local time zone: 01:23:23
|
||||
Lmicroseconds // microsecond resolution: 01:23:23.123123. assumes Ltime.
|
||||
Llongfile // full file name and line number: /a/logger/c/d.go:23
|
||||
Lshortfile // final file name element and line number: d.go:23. overrides Llongfile
|
||||
Lfuncname // function name of the caller: runtime.Caller()
|
||||
Lshortfuncname // last part of the function name
|
||||
LUTC // if Ldate or Ltime is set, use UTC rather than the local time zone
|
||||
Llevelinitial // Initial character of the provided level in brackets eg. [I] for info
|
||||
Llevel // Provided level in brackets [INFO]
|
||||
Ldate uint32 = 1 << iota // the date in the local time zone: 2009/01/23
|
||||
Ltime // the time in the local time zone: 01:23:23
|
||||
Lmicroseconds // microsecond resolution: 01:23:23.123123. assumes Ltime.
|
||||
Llongfile // full file name and line number: /a/logger/c/d.go:23
|
||||
Lshortfile // final file name element and line number: d.go:23. overrides Llongfile
|
||||
Lfuncname // function name of the caller: runtime.Caller()
|
||||
Lshortfuncname // last part of the function name
|
||||
LUTC // if Ldate or Ltime is set, use UTC rather than the local time zone
|
||||
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
|
||||
|
||||
// Last 20 characters of the filename
|
||||
Lmedfile = Lshortfile | Llongfile
|
||||
|
||||
// LstdFlags is the initial value for the standard logger
|
||||
LstdFlags = Ldate | Ltime | Lmedfile | Lshortfuncname | Llevelinitial
|
||||
Lmedfile = Lshortfile | Llongfile // last 20 characters of the filename
|
||||
LstdFlags = Ldate | Ltime | Lmedfile | Lshortfuncname | Llevelinitial // default
|
||||
)
|
||||
|
||||
var flagFromString = map[string]int{
|
||||
"none": 0,
|
||||
const Ldefault = LstdFlags
|
||||
|
||||
type Flags struct {
|
||||
defined bool
|
||||
flags uint32
|
||||
}
|
||||
|
||||
var flagFromString = map[string]uint32{
|
||||
"date": Ldate,
|
||||
"time": Ltime,
|
||||
"microseconds": Lmicroseconds,
|
||||
|
@ -45,22 +54,81 @@ var flagFromString = map[string]int{
|
|||
"utc": LUTC,
|
||||
"levelinitial": Llevelinitial,
|
||||
"level": Llevel,
|
||||
"medfile": Lmedfile,
|
||||
"stdflags": LstdFlags,
|
||||
"gopid": Lgopid,
|
||||
|
||||
"medfile": Lmedfile,
|
||||
"stdflags": LstdFlags,
|
||||
}
|
||||
|
||||
// FlagsFromString takes a comma separated list of flags and returns
|
||||
// the flags for this string
|
||||
func FlagsFromString(from string) int {
|
||||
flags := 0
|
||||
for _, flag := range strings.Split(strings.ToLower(from), ",") {
|
||||
f, ok := flagFromString[strings.TrimSpace(flag)]
|
||||
if ok {
|
||||
flags |= f
|
||||
var flagComboToString = []struct {
|
||||
flag uint32
|
||||
name string
|
||||
}{
|
||||
// name with more bits comes first
|
||||
{LstdFlags, "stdflags"},
|
||||
{Lmedfile, "medfile"},
|
||||
|
||||
{Ldate, "date"},
|
||||
{Ltime, "time"},
|
||||
{Lmicroseconds, "microseconds"},
|
||||
{Llongfile, "longfile"},
|
||||
{Lshortfile, "shortfile"},
|
||||
{Lfuncname, "funcname"},
|
||||
{Lshortfuncname, "shortfuncname"},
|
||||
{LUTC, "utc"},
|
||||
{Llevelinitial, "levelinitial"},
|
||||
{Llevel, "level"},
|
||||
{Lgopid, "gopid"},
|
||||
}
|
||||
|
||||
func (f Flags) Bits() uint32 {
|
||||
if !f.defined {
|
||||
return Ldefault
|
||||
}
|
||||
return f.flags
|
||||
}
|
||||
|
||||
func (f Flags) String() string {
|
||||
flags := f.Bits()
|
||||
var flagNames []string
|
||||
for _, it := range flagComboToString {
|
||||
if flags&it.flag == it.flag {
|
||||
flags &^= it.flag
|
||||
flagNames = append(flagNames, it.name)
|
||||
}
|
||||
}
|
||||
if flags == 0 {
|
||||
return -1
|
||||
if len(flagNames) == 0 {
|
||||
return "none"
|
||||
}
|
||||
return flags
|
||||
sort.Strings(flagNames)
|
||||
return strings.Join(flagNames, ",")
|
||||
}
|
||||
|
||||
func (f *Flags) UnmarshalJSON(bytes []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(bytes, &s); err != nil {
|
||||
return err
|
||||
}
|
||||
*f = FlagsFromString(s)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f Flags) MarshalJSON() ([]byte, error) {
|
||||
return []byte(`"` + f.String() + `"`), nil
|
||||
}
|
||||
|
||||
func FlagsFromString(from string, def ...uint32) Flags {
|
||||
from = strings.TrimSpace(from)
|
||||
if from == "" && len(def) > 0 {
|
||||
return Flags{defined: true, flags: def[0]}
|
||||
}
|
||||
flags := uint32(0)
|
||||
for _, flag := range strings.Split(strings.ToLower(from), ",") {
|
||||
flags |= flagFromString[strings.TrimSpace(flag)]
|
||||
}
|
||||
return Flags{defined: true, flags: flags}
|
||||
}
|
||||
|
||||
func FlagsFromBits(flags uint32) Flags {
|
||||
return Flags{defined: true, flags: flags}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue