Skip to content

Commit

Permalink
Merge branch 'main' into notif3
Browse files Browse the repository at this point in the history
* main:
  Improvements for action detail page (go-gitea#24718)
  Add CRAN package registry (go-gitea#22331)
  Fix regression: access log template, gitea manager cli command (go-gitea#24838)
  Merge message template support for rebase without merge commit (go-gitea#22669)
  [skip ci] Updated licenses and gitignores
  Support wildcard in email domain allow/block list (go-gitea#24831)
  Change `--font-weight-bold` to `--font-weight-semibold` and 600 value, introduce new font weight variables (go-gitea#24827)
  Rewrite logger system (go-gitea#24726)
  Support Copy Link for video attachments (go-gitea#24833)
  • Loading branch information
silverwind committed May 22, 2023
2 parents 49e710c + da461b5 commit d66db5e
Show file tree
Hide file tree
Showing 180 changed files with 5,566 additions and 5,526 deletions.
2 changes: 1 addition & 1 deletion .stylelintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ rules:
property-no-vendor-prefix: null
rule-empty-line-before: null
rule-selector-property-disallowed-list: null
scale-unlimited/declaration-strict-value: [[color, background-color, border-color], {ignoreValues: /^(inherit|transparent|unset|initial|currentcolor|none)$/, ignoreFunctions: false}]
scale-unlimited/declaration-strict-value: [[color, background-color, border-color, font-weight], {ignoreValues: /^(inherit|transparent|unset|initial|currentcolor|none)$/, ignoreFunctions: false}]
selector-attribute-name-disallowed-list: null
selector-attribute-operator-allowed-list: null
selector-attribute-operator-disallowed-list: null
Expand Down
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)
} 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
Loading

0 comments on commit d66db5e

Please sign in to comment.