aboutsummaryrefslogtreecommitdiffstats
path: root/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'cmd')
-rw-r--r--cmd/cmd.go17
-rw-r--r--cmd/doctor.go90
-rw-r--r--cmd/dump.go5
-rw-r--r--cmd/embedded.go8
-rw-r--r--cmd/manager_logging.go120
-rw-r--r--cmd/serv.go5
-rw-r--r--cmd/web.go10
7 files changed, 76 insertions, 179 deletions
diff --git a/cmd/cmd.go b/cmd/cmd.go
index cf2d9ef89e..b148007fbe 100644
--- a/cmd/cmd.go
+++ b/cmd/cmd.go
@@ -9,6 +9,7 @@ import (
"context"
"errors"
"fmt"
+ "io"
"os"
"os/signal"
"strings"
@@ -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.
@@ -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)
+}
diff --git a/cmd/doctor.go b/cmd/doctor.go
index 65c028c5ed..b596e9ac0c 100644
--- a/cmd/doctor.go
+++ b/cmd/doctor.go
@@ -4,10 +4,10 @@
package cmd
import (
- "errors"
"fmt"
golog "log"
"os"
+ "path/filepath"
"strings"
"text/tabwriter"
@@ -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.")
@@ -125,44 +127,31 @@ 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)
}
}
@@ -170,22 +159,17 @@ 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)
@@ -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)
}
diff --git a/cmd/dump.go b/cmd/dump.go
index 32ccc5566c..40524f48d4 100644
--- a/cmd/dump.go
+++ b/cmd/dump.go
@@ -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)
} else {
for _, suffix := range outputTypeEnum.Enum {
if strings.HasSuffix(fileName, "."+suffix) {
diff --git a/cmd/embedded.go b/cmd/embedded.go
index 3f849bea0a..e51f8477b4 100644
--- a/cmd/embedded.go
+++ b/cmd/embedded.go
@@ -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{
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)
}
diff --git a/cmd/serv.go b/cmd/serv.go
index a79f314d00..87bf1cce20 100644
--- a/cmd/serv.go
+++ b/cmd/serv.go
@@ -56,11 +56,10 @@ var CmdServ = cli.Command{
}
func setup(ctx context.Context, debug bool) {
- _ = log.DelLogger("console")
if debug {
- _ = log.NewLogger(1000, "console", "console", `{"level":"trace","stacktracelevel":"NONE","stderr":true}`)
+ setupConsoleLogger(log.TRACE, false, os.Stderr)
} else {
- _ = log.NewLogger(1000, "console", "console", `{"level":"fatal","stacktracelevel":"NONE","stderr":true}`)
+ setupConsoleLogger(log.FATAL, false, os.Stderr)
}
setting.Init(&setting.Options{})
if debug {
diff --git a/cmd/web.go b/cmd/web.go
index 3a01d07b05..bc344db540 100644
--- a/cmd/web.go
+++ b/cmd/web.go
@@ -103,11 +103,9 @@ func createPIDFile(pidPath string) {
func runWeb(ctx *cli.Context) error {
if ctx.Bool("verbose") {
- _ = log.DelLogger("console")
- log.NewLogger(0, "console", "console", fmt.Sprintf(`{"level": "trace", "colorize": %t, "stacktraceLevel": "none"}`, log.CanColorStdout))
+ setupConsoleLogger(log.TRACE, log.CanColorStdout, os.Stdout)
} else if ctx.Bool("quiet") {
- _ = log.DelLogger("console")
- log.NewLogger(0, "console", "console", fmt.Sprintf(`{"level": "fatal", "colorize": %t, "stacktraceLevel": "none"}`, log.CanColorStdout))
+ setupConsoleLogger(log.FATAL, log.CanColorStdout, os.Stdout)
}
defer func() {
if panicked := recover(); panicked != nil {
@@ -156,7 +154,7 @@ func runWeb(ctx *cli.Context) error {
case <-graceful.GetManager().IsShutdown():
<-graceful.GetManager().Done()
log.Info("PID: %d Gitea Web Finished", os.Getpid())
- log.Close()
+ log.GetManager().Close()
return err
default:
}
@@ -199,7 +197,7 @@ func runWeb(ctx *cli.Context) error {
err := listen(c, true)
<-graceful.GetManager().Done()
log.Info("PID: %d Gitea Web Finished", os.Getpid())
- log.Close()
+ log.GetManager().Close()
return err
}