Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite logger system #24726

Merged
merged 32 commits into from
May 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2828666
rewrite
wxiaoguang May 13, 2023
9a0dbef
Merge branch 'main' into rewrite-log
wxiaoguang May 16, 2023
12417a8
fix merge
wxiaoguang May 16, 2023
a1c229d
fine tune
wxiaoguang May 16, 2023
6b21f68
fine tune tests and documents
wxiaoguang May 16, 2023
a3b7b28
optimize flags
wxiaoguang May 16, 2023
2aea30b
add one more test
wxiaoguang May 16, 2023
9d59a86
improve legacy testlogger
wxiaoguang May 16, 2023
f54bfd5
revert mailer
wxiaoguang May 16, 2023
0691488
test buggy ini package
wxiaoguang May 16, 2023
090a803
doc & lint
wxiaoguang May 16, 2023
fe67dc4
improve rotatingfilewriter
wxiaoguang May 17, 2023
63e169a
clarify ini package behavior
wxiaoguang May 17, 2023
8b6b2d3
fix rotatingfilewriter error handling
wxiaoguang May 17, 2023
ac094e3
clarify access log behavior, make event writer shareable, improve tests
wxiaoguang May 17, 2023
1bb5e96
add more comments
wxiaoguang May 17, 2023
cc3f7af
fix doc
wxiaoguang May 17, 2023
bef01a1
Update modules/util/rotatingfilewriter/writer.go
wxiaoguang May 18, 2023
4919be9
Merge branch 'main' into rewrite-log
wxiaoguang May 18, 2023
90535c1
Update docs/content/doc/administration/config-cheat-sheet.en-us.md
wxiaoguang May 18, 2023
f05ea76
Update docs/content/doc/administration/logging-config.en-us.md
wxiaoguang May 18, 2023
03d69b9
Update docs/content/doc/administration/config-cheat-sheet.en-us.md
wxiaoguang May 18, 2023
9b5c93e
Apply suggestions from code review
wxiaoguang May 18, 2023
9aceb06
Update modules/log/color_console.go
wxiaoguang May 18, 2023
5bcbdb3
Merge branch 'main' into rewrite-log
wxiaoguang May 19, 2023
f985c0f
add test for ReleaseReopen manager
wxiaoguang May 19, 2023
60079ba
support "%#v"
wxiaoguang May 19, 2023
148cf62
Merge branch 'main' into rewrite-log
wxiaoguang May 21, 2023
985884b
Merge branch 'main' into rewrite-log
GiteaBot May 21, 2023
f2a9a70
Merge branch 'main' into rewrite-log
GiteaBot May 21, 2023
4c12d6f
Merge branch 'main' into rewrite-log
GiteaBot May 21, 2023
c8dce4b
Merge branch 'main' into rewrite-log
GiteaBot May 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"context"
"errors"
"fmt"
"io"
"os"
"os/signal"
"strings"
Expand Down Expand Up @@ -59,7 +60,7 @@ func confirm() (bool, error) {
func initDB(ctx context.Context) error {
setting.Init(&setting.Options{})
setting.LoadDBSetting()
setting.InitSQLLog(false)
setting.InitSQLLoggersForCli(log.INFO)

if setting.Database.Type == "" {
log.Fatal(`Database settings are missing from the configuration file: %q.
Expand Down Expand Up @@ -93,3 +94,17 @@ func installSignals() (context.Context, context.CancelFunc) {

return ctx, cancel
}

func setupConsoleLogger(level log.Level, colorize bool, out io.Writer) {
if out != os.Stdout && out != os.Stderr {
panic("setupConsoleLogger can only be used with os.Stdout or os.Stderr")
}

writeMode := log.WriterMode{
Level: level,
Colorize: colorize,
WriterOption: log.WriterConsoleOption{Stderr: out == os.Stderr},
}
writer := log.NewEventWriterConsole("console-default", writeMode)
log.GetManager().GetLogger(log.DEFAULT).RemoveAllWriters().AddWriters(writer)
}
90 changes: 31 additions & 59 deletions cmd/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
package cmd

import (
"errors"
"fmt"
golog "log"
"os"
"path/filepath"
"strings"
"text/tabwriter"

Expand Down Expand Up @@ -82,23 +82,25 @@ You should back-up your database before doing this and ensure that your database
}

func runRecreateTable(ctx *cli.Context) error {
stdCtx, cancel := installSignals()
defer cancel()

// Redirect the default golog to here
golog.SetFlags(0)
golog.SetPrefix("")
golog.SetOutput(log.NewLoggerAsWriter("INFO", log.GetLogger(log.DEFAULT)))
golog.SetOutput(log.LoggerToWriter(log.GetLogger(log.DEFAULT).Info))

debug := ctx.Bool("debug")
setting.Init(&setting.Options{})
setting.LoadDBSetting()

setting.Log.EnableXORMLog = ctx.Bool("debug")
setting.Database.LogSQL = ctx.Bool("debug")
// FIXME: don't use CfgProvider directly
setting.CfgProvider.Section("log").Key("XORM").SetValue(",")

setting.InitSQLLog(!ctx.Bool("debug"))
stdCtx, cancel := installSignals()
defer cancel()
if debug {
setting.InitSQLLoggersForCli(log.DEBUG)
} else {
setting.InitSQLLoggersForCli(log.INFO)
}

setting.Database.LogSQL = debug
if err := db.InitEngine(stdCtx); err != nil {
fmt.Println(err)
fmt.Println("Check if you are using the right config file. You can use a --config directive to specify one.")
Expand All @@ -125,67 +127,49 @@ func runRecreateTable(ctx *cli.Context) error {
})
}

func setDoctorLogger(ctx *cli.Context) {
func setupDoctorDefaultLogger(ctx *cli.Context, colorize bool) {
// Silence the default loggers
setupConsoleLogger(log.FATAL, log.CanColorStderr, os.Stderr)

logFile := ctx.String("log-file")
if !ctx.IsSet("log-file") {
logFile = "doctor.log"
}
colorize := log.CanColorStdout
if ctx.IsSet("color") {
colorize = ctx.Bool("color")
}

if len(logFile) == 0 {
log.NewLogger(1000, "doctor", "console", fmt.Sprintf(`{"level":"NONE","stacktracelevel":"NONE","colorize":%t}`, colorize))
// if no doctor log-file is set, do not show any log from default logger
return
}

defer func() {
recovered := recover()
if recovered == nil {
return
}

err, ok := recovered.(error)
if !ok {
panic(recovered)
}
if errors.Is(err, os.ErrPermission) {
fmt.Fprintf(os.Stderr, "ERROR: Unable to write logs to provided file due to permissions error: %s\n %v\n", logFile, err)
} else {
fmt.Fprintf(os.Stderr, "ERROR: Unable to write logs to provided file: %s\n %v\n", logFile, err)
}
fmt.Fprintf(os.Stderr, "WARN: Logging will be disabled\n Use `--log-file` to configure log file location\n")
log.NewLogger(1000, "doctor", "console", fmt.Sprintf(`{"level":"NONE","stacktracelevel":"NONE","colorize":%t}`, colorize))
}()

if logFile == "-" {
log.NewLogger(1000, "doctor", "console", fmt.Sprintf(`{"level":"trace","stacktracelevel":"NONE","colorize":%t}`, colorize))
setupConsoleLogger(log.TRACE, colorize, os.Stdout)
} else {
log.NewLogger(1000, "doctor", "file", fmt.Sprintf(`{"filename":%q,"level":"trace","stacktracelevel":"NONE"}`, logFile))
logFile, _ = filepath.Abs(logFile)
writeMode := log.WriterMode{Level: log.TRACE, WriterOption: log.WriterFileOption{FileName: logFile}}
writer, err := log.NewEventWriter("console-to-file", "file", writeMode)
if err != nil {
log.FallbackErrorf("unable to create file log writer: %v", err)
return
}
log.GetManager().GetLogger(log.DEFAULT).RemoveAllWriters().AddWriters(writer)
}
}

func runDoctor(ctx *cli.Context) error {
stdCtx, cancel := installSignals()
defer cancel()

// Silence the default loggers
log.DelNamedLogger("console")
log.DelNamedLogger(log.DEFAULT)

// Now setup our own
setDoctorLogger(ctx)

colorize := log.CanColorStdout
if ctx.IsSet("color") {
colorize = ctx.Bool("color")
}

// Finally redirect the default golog to here
setupDoctorDefaultLogger(ctx, colorize)

// Finally redirect the default golang's log to here
golog.SetFlags(0)
golog.SetPrefix("")
golog.SetOutput(log.NewLoggerAsWriter("INFO", log.GetLogger(log.DEFAULT)))
golog.SetOutput(log.LoggerToWriter(log.GetLogger(log.DEFAULT).Info))

if ctx.IsSet("list") {
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
Expand Down Expand Up @@ -233,17 +217,5 @@ func runDoctor(ctx *cli.Context) error {
}
}

// Now we can set up our own logger to return information about what the doctor is doing
if err := log.NewNamedLogger("doctorouter",
0,
"console",
"console",
fmt.Sprintf(`{"level":"INFO","stacktracelevel":"NONE","colorize":%t,"flags":-1}`, colorize)); err != nil {
fmt.Println(err)
return err
}

logger := log.GetLogger("doctorouter")
defer logger.Close()
return doctor.RunChecks(stdCtx, logger, ctx.Bool("fix"), checks)
return doctor.RunChecks(stdCtx, colorize, ctx.Bool("fix"), checks)
}
5 changes: 1 addition & 4 deletions cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,7 @@ func runDump(ctx *cli.Context) error {
outType := ctx.String("type")
if fileName == "-" {
file = os.Stdout
err := log.DelLogger("console")
if err != nil {
fatal("Deleting default logger failed. Can not write to stdout: %v", err)
}
setupConsoleLogger(log.FATAL, log.CanColorStderr, os.Stderr)
delvh marked this conversation as resolved.
Show resolved Hide resolved
} else {
for _, suffix := range outputTypeEnum.Enum {
if strings.HasSuffix(fileName, "."+suffix) {
Expand Down
8 changes: 1 addition & 7 deletions cmd/embedded.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,7 @@ type assetFile struct {
}

func initEmbeddedExtractor(c *cli.Context) error {
// FIXME: there is a bug, if the user runs `gitea embedded` with a different user or root,
// The setting.Init (loadRunModeFrom) will fail and do log.Fatal
// But the console logger has been deleted, so nothing is printed, the user sees nothing and Gitea just exits.

// Silence the console logger
log.DelNamedLogger("console")
log.DelNamedLogger(log.DEFAULT)
setupConsoleLogger(log.ERROR, log.CanColorStderr, os.Stderr)

// Read configuration file
setting.Init(&setting.Options{
Expand Down
120 changes: 21 additions & 99 deletions cmd/manager_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand All @@ -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{
Expand Down Expand Up @@ -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,
},
},
}, {
Expand All @@ -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()
Expand Down Expand Up @@ -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")
Expand All @@ -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)
}

Expand Down
Loading