summaryrefslogtreecommitdiffstats
path: root/cmd/manager_logging.go
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2023-05-22 00:35:11 +0200
committerGitHub <noreply@github.com>2023-05-22 00:35:11 +0200
commit4647660776436f0a83129b4ceb8426b1fb0599bb (patch)
tree465ae0fc7cb7c4b0b30f6600a701c240cdf2a3a6 /cmd/manager_logging.go
parentSupport Copy Link for video attachments (#24833) (diff)
downloadforgejo-4647660776436f0a83129b4ceb8426b1fb0599bb.tar.xz
forgejo-4647660776436f0a83129b4ceb8426b1fb0599bb.zip
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> ![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>
Diffstat (limited to 'cmd/manager_logging.go')
-rw-r--r--cmd/manager_logging.go120
1 files changed, 21 insertions, 99 deletions
diff --git a/cmd/manager_logging.go b/cmd/manager_logging.go
index 914210d370..dd85cc26d8 100644
--- a/cmd/manager_logging.go
+++ b/cmd/manager_logging.go
@@ -16,13 +16,13 @@ import (
var (
defaultLoggingFlags = []cli.Flag{
cli.StringFlag{
- Name: "group, g",
- Usage: "Group to add logger to - will default to \"default\"",
+ Name: "logger",
+ Usage: `Logger name - will default to "default"`,
}, cli.StringFlag{
- Name: "name, n",
- Usage: "Name of the new logger - will default to mode",
+ Name: "writer",
+ Usage: "Name of the log writer - will default to mode",
}, cli.StringFlag{
- Name: "level, l",
+ Name: "level",
Usage: "Logging level for the new logger",
}, cli.StringFlag{
Name: "stacktrace-level, L",
@@ -83,8 +83,8 @@ var (
cli.BoolFlag{
Name: "debug",
}, cli.StringFlag{
- Name: "group, g",
- Usage: "Group to add logger to - will default to \"default\"",
+ Name: "logger",
+ Usage: `Logger name - will default to "default"`,
},
},
Action: runRemoveLogger,
@@ -93,15 +93,6 @@ var (
Usage: "Add a logger",
Subcommands: []cli.Command{
{
- Name: "console",
- Usage: "Add a console logger",
- Flags: append(defaultLoggingFlags,
- cli.BoolFlag{
- Name: "stderr",
- Usage: "Output console logs to stderr - only relevant for console",
- }),
- Action: runAddConsoleLogger,
- }, {
Name: "file",
Usage: "Add a file logger",
Flags: append(defaultLoggingFlags, []cli.Flag{
@@ -148,28 +139,6 @@ var (
},
}...),
Action: runAddConnLogger,
- }, {
- Name: "smtp",
- Usage: "Add an SMTP logger",
- Flags: append(defaultLoggingFlags, []cli.Flag{
- cli.StringFlag{
- Name: "username, u",
- Usage: "Mail server username",
- }, cli.StringFlag{
- Name: "password, P",
- Usage: "Mail server password",
- }, cli.StringFlag{
- Name: "host, H",
- Usage: "Mail server host (defaults to: 127.0.0.1:25)",
- }, cli.StringSliceFlag{
- Name: "send-to, s",
- Usage: "Email address(es) to send to",
- }, cli.StringFlag{
- Name: "subject, S",
- Usage: "Subject header of sent emails",
- },
- }...),
- Action: runAddSMTPLogger,
},
},
}, {
@@ -194,50 +163,16 @@ func runRemoveLogger(c *cli.Context) error {
defer cancel()
setup(ctx, c.Bool("debug"))
- group := c.String("group")
- if len(group) == 0 {
- group = log.DEFAULT
+ logger := c.String("logger")
+ if len(logger) == 0 {
+ logger = log.DEFAULT
}
- name := c.Args().First()
+ writer := c.Args().First()
- extra := private.RemoveLogger(ctx, group, name)
+ extra := private.RemoveLogger(ctx, logger, writer)
return handleCliResponseExtra(extra)
}
-func runAddSMTPLogger(c *cli.Context) error {
- ctx, cancel := installSignals()
- defer cancel()
-
- setup(ctx, c.Bool("debug"))
- vals := map[string]interface{}{}
- mode := "smtp"
- if c.IsSet("host") {
- vals["host"] = c.String("host")
- } else {
- vals["host"] = "127.0.0.1:25"
- }
-
- if c.IsSet("username") {
- vals["username"] = c.String("username")
- }
- if c.IsSet("password") {
- vals["password"] = c.String("password")
- }
-
- if !c.IsSet("send-to") {
- return fmt.Errorf("Some recipients must be provided")
- }
- vals["sendTos"] = c.StringSlice("send-to")
-
- if c.IsSet("subject") {
- vals["subject"] = c.String("subject")
- } else {
- vals["subject"] = "Diagnostic message from Gitea"
- }
-
- return commonAddLogger(c, mode, vals)
-}
-
func runAddConnLogger(c *cli.Context) error {
ctx, cancel := installSignals()
defer cancel()
@@ -301,25 +236,12 @@ func runAddFileLogger(c *cli.Context) error {
return commonAddLogger(c, mode, vals)
}
-func runAddConsoleLogger(c *cli.Context) error {
- ctx, cancel := installSignals()
- defer cancel()
-
- setup(ctx, c.Bool("debug"))
- vals := map[string]interface{}{}
- mode := "console"
- if c.IsSet("stderr") && c.Bool("stderr") {
- vals["stderr"] = c.Bool("stderr")
- }
- return commonAddLogger(c, mode, vals)
-}
-
func commonAddLogger(c *cli.Context, mode string, vals map[string]interface{}) error {
if len(c.String("level")) > 0 {
- vals["level"] = log.FromString(c.String("level")).String()
+ vals["level"] = log.LevelFromString(c.String("level")).String()
}
if len(c.String("stacktrace-level")) > 0 {
- vals["stacktraceLevel"] = log.FromString(c.String("stacktrace-level")).String()
+ vals["stacktraceLevel"] = log.LevelFromString(c.String("stacktrace-level")).String()
}
if len(c.String("expression")) > 0 {
vals["expression"] = c.String("expression")
@@ -333,18 +255,18 @@ func commonAddLogger(c *cli.Context, mode string, vals map[string]interface{}) e
if c.IsSet("color") {
vals["colorize"] = c.Bool("color")
}
- group := "default"
- if c.IsSet("group") {
- group = c.String("group")
+ logger := log.DEFAULT
+ if c.IsSet("logger") {
+ logger = c.String("logger")
}
- name := mode
- if c.IsSet("name") {
- name = c.String("name")
+ writer := mode
+ if c.IsSet("writer") {
+ writer = c.String("writer")
}
ctx, cancel := installSignals()
defer cancel()
- extra := private.AddLogger(ctx, group, name, mode, vals)
+ extra := private.AddLogger(ctx, logger, writer, mode, vals)
return handleCliResponseExtra(extra)
}