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
|
@ -14,67 +14,62 @@ import (
|
|||
|
||||
// XORMLogBridge a logger bridge from Logger to xorm
|
||||
type XORMLogBridge struct {
|
||||
showSQLint *int32
|
||||
logger log.Logger
|
||||
showSQL atomic.Bool
|
||||
logger log.Logger
|
||||
}
|
||||
|
||||
// NewXORMLogger inits a log bridge for xorm
|
||||
func NewXORMLogger(showSQL bool) xormlog.Logger {
|
||||
showSQLint := int32(0)
|
||||
if showSQL {
|
||||
showSQLint = 1
|
||||
}
|
||||
return &XORMLogBridge{
|
||||
showSQLint: &showSQLint,
|
||||
logger: log.GetLogger("xorm"),
|
||||
}
|
||||
l := &XORMLogBridge{logger: log.GetLogger("xorm")}
|
||||
l.showSQL.Store(showSQL)
|
||||
return l
|
||||
}
|
||||
|
||||
const stackLevel = 8
|
||||
|
||||
// Log a message with defined skip and at logging level
|
||||
func (l *XORMLogBridge) Log(skip int, level log.Level, format string, v ...interface{}) error {
|
||||
return l.logger.Log(skip+1, level, format, v...)
|
||||
func (l *XORMLogBridge) Log(skip int, level log.Level, format string, v ...interface{}) {
|
||||
l.logger.Log(skip+1, level, format, v...)
|
||||
}
|
||||
|
||||
// Debug show debug log
|
||||
func (l *XORMLogBridge) Debug(v ...interface{}) {
|
||||
_ = l.Log(stackLevel, log.DEBUG, fmt.Sprint(v...))
|
||||
l.Log(stackLevel, log.DEBUG, "%s", fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
// Debugf show debug log
|
||||
func (l *XORMLogBridge) Debugf(format string, v ...interface{}) {
|
||||
_ = l.Log(stackLevel, log.DEBUG, format, v...)
|
||||
l.Log(stackLevel, log.DEBUG, format, v...)
|
||||
}
|
||||
|
||||
// Error show error log
|
||||
func (l *XORMLogBridge) Error(v ...interface{}) {
|
||||
_ = l.Log(stackLevel, log.ERROR, fmt.Sprint(v...))
|
||||
l.Log(stackLevel, log.ERROR, "%s", fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
// Errorf show error log
|
||||
func (l *XORMLogBridge) Errorf(format string, v ...interface{}) {
|
||||
_ = l.Log(stackLevel, log.ERROR, format, v...)
|
||||
l.Log(stackLevel, log.ERROR, format, v...)
|
||||
}
|
||||
|
||||
// Info show information level log
|
||||
func (l *XORMLogBridge) Info(v ...interface{}) {
|
||||
_ = l.Log(stackLevel, log.INFO, fmt.Sprint(v...))
|
||||
l.Log(stackLevel, log.INFO, "%s", fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
// Infof show information level log
|
||||
func (l *XORMLogBridge) Infof(format string, v ...interface{}) {
|
||||
_ = l.Log(stackLevel, log.INFO, format, v...)
|
||||
l.Log(stackLevel, log.INFO, format, v...)
|
||||
}
|
||||
|
||||
// Warn show warning log
|
||||
func (l *XORMLogBridge) Warn(v ...interface{}) {
|
||||
_ = l.Log(stackLevel, log.WARN, fmt.Sprint(v...))
|
||||
l.Log(stackLevel, log.WARN, "%s", fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
// Warnf show warnning log
|
||||
func (l *XORMLogBridge) Warnf(format string, v ...interface{}) {
|
||||
_ = l.Log(stackLevel, log.WARN, format, v...)
|
||||
l.Log(stackLevel, log.WARN, format, v...)
|
||||
}
|
||||
|
||||
// Level get logger level
|
||||
|
@ -86,10 +81,12 @@ func (l *XORMLogBridge) Level() xormlog.LogLevel {
|
|||
return xormlog.LOG_INFO
|
||||
case log.WARN:
|
||||
return xormlog.LOG_WARNING
|
||||
case log.ERROR, log.CRITICAL:
|
||||
case log.ERROR:
|
||||
return xormlog.LOG_ERR
|
||||
case log.NONE:
|
||||
return xormlog.LOG_OFF
|
||||
}
|
||||
return xormlog.LOG_OFF
|
||||
return xormlog.LOG_UNKNOWN
|
||||
}
|
||||
|
||||
// SetLevel set the logger level
|
||||
|
@ -98,16 +95,13 @@ func (l *XORMLogBridge) SetLevel(lvl xormlog.LogLevel) {
|
|||
|
||||
// ShowSQL set if record SQL
|
||||
func (l *XORMLogBridge) ShowSQL(show ...bool) {
|
||||
showSQL := int32(1)
|
||||
if len(show) > 0 && !show[0] {
|
||||
showSQL = 0
|
||||
if len(show) == 0 {
|
||||
show = []bool{true}
|
||||
}
|
||||
atomic.StoreInt32(l.showSQLint, showSQL)
|
||||
l.showSQL.Store(show[0])
|
||||
}
|
||||
|
||||
// IsShowSQL if record SQL
|
||||
func (l *XORMLogBridge) IsShowSQL() bool {
|
||||
showSQL := atomic.LoadInt32(l.showSQLint)
|
||||
|
||||
return showSQL == 1
|
||||
return l.showSQL.Load()
|
||||
}
|
||||
|
|
|
@ -224,40 +224,27 @@ func DeletePullsByBaseRepoID(ctx context.Context, repoID int64) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// ColorFormat writes a colored string to identify this struct
|
||||
func (pr *PullRequest) ColorFormat(s fmt.State) {
|
||||
func (pr *PullRequest) String() string {
|
||||
if pr == nil {
|
||||
log.ColorFprintf(s, "PR[%d]%s#%d[%s...%s:%s]",
|
||||
log.NewColoredIDValue(0),
|
||||
log.NewColoredValue("<nil>/<nil>"),
|
||||
log.NewColoredIDValue(0),
|
||||
log.NewColoredValue("<nil>"),
|
||||
log.NewColoredValue("<nil>/<nil>"),
|
||||
log.NewColoredValue("<nil>"),
|
||||
)
|
||||
return
|
||||
return "<PullRequest nil>"
|
||||
}
|
||||
|
||||
log.ColorFprintf(s, "PR[%d]", log.NewColoredIDValue(pr.ID))
|
||||
s := new(strings.Builder)
|
||||
fmt.Fprintf(s, "<PullRequest [%d]", pr.ID)
|
||||
if pr.BaseRepo != nil {
|
||||
log.ColorFprintf(s, "%s#%d[%s...", log.NewColoredValue(pr.BaseRepo.FullName()),
|
||||
log.NewColoredIDValue(pr.Index), log.NewColoredValue(pr.BaseBranch))
|
||||
fmt.Fprintf(s, "%s#%d[%s...", pr.BaseRepo.FullName(), pr.Index, pr.BaseBranch)
|
||||
} else {
|
||||
log.ColorFprintf(s, "Repo[%d]#%d[%s...", log.NewColoredIDValue(pr.BaseRepoID),
|
||||
log.NewColoredIDValue(pr.Index), log.NewColoredValue(pr.BaseBranch))
|
||||
fmt.Fprintf(s, "Repo[%d]#%d[%s...", pr.BaseRepoID, pr.Index, pr.BaseBranch)
|
||||
}
|
||||
if pr.HeadRepoID == pr.BaseRepoID {
|
||||
log.ColorFprintf(s, "%s]", log.NewColoredValue(pr.HeadBranch))
|
||||
fmt.Fprintf(s, "%s]", pr.HeadBranch)
|
||||
} else if pr.HeadRepo != nil {
|
||||
log.ColorFprintf(s, "%s:%s]", log.NewColoredValue(pr.HeadRepo.FullName()), log.NewColoredValue(pr.HeadBranch))
|
||||
fmt.Fprintf(s, "%s:%s]", pr.HeadRepo.FullName(), pr.HeadBranch)
|
||||
} else {
|
||||
log.ColorFprintf(s, "Repo[%d]:%s]", log.NewColoredIDValue(pr.HeadRepoID), log.NewColoredValue(pr.HeadBranch))
|
||||
fmt.Fprintf(s, "Repo[%d]:%s]", pr.HeadRepoID, pr.HeadBranch)
|
||||
}
|
||||
}
|
||||
|
||||
// String represents the pr as a simple string
|
||||
func (pr *PullRequest) String() string {
|
||||
return log.ColorFormatAsString(pr)
|
||||
s.WriteByte('>')
|
||||
return s.String()
|
||||
}
|
||||
|
||||
// MustHeadUserName returns the HeadRepo's username if failed return blank
|
||||
|
|
|
@ -24,6 +24,8 @@ import (
|
|||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
// FIXME: this file shouldn't be in a normal package, it should only be compiled for tests
|
||||
|
||||
// PrepareTestEnv prepares the test environment and reset the database. The skip parameter should usually be 0.
|
||||
// Provide models to be sync'd with the database - in particular any models you expect fixtures to be loaded from.
|
||||
//
|
||||
|
@ -110,7 +112,7 @@ func PrepareTestEnv(t *testing.T, skip int, syncModels ...interface{}) (*xorm.En
|
|||
}
|
||||
|
||||
func MainTest(m *testing.M) {
|
||||
log.Register("test", testlogger.NewTestLogger)
|
||||
log.RegisterEventWriter("test", testlogger.NewTestLoggerWriter)
|
||||
|
||||
giteaRoot := base.SetupGiteaRoot()
|
||||
if giteaRoot == "" {
|
||||
|
@ -154,7 +156,7 @@ func MainTest(m *testing.M) {
|
|||
os.Exit(1)
|
||||
}
|
||||
setting.LoadDBSetting()
|
||||
setting.InitLogs(true)
|
||||
setting.InitLoggersForTest()
|
||||
|
||||
exitStatus := m.Run()
|
||||
|
||||
|
|
|
@ -94,21 +94,11 @@ func init() {
|
|||
db.RegisterModel(new(TeamInvite))
|
||||
}
|
||||
|
||||
// ColorFormat provides a basic color format for a Team
|
||||
func (t *Team) ColorFormat(s fmt.State) {
|
||||
func (t *Team) LogString() string {
|
||||
if t == nil {
|
||||
log.ColorFprintf(s, "%d:%s (OrgID: %d) %-v",
|
||||
log.NewColoredIDValue(0),
|
||||
"<nil>",
|
||||
log.NewColoredIDValue(0),
|
||||
0)
|
||||
return
|
||||
return "<Team nil>"
|
||||
}
|
||||
log.ColorFprintf(s, "%d:%s (OrgID: %d) %-v",
|
||||
log.NewColoredIDValue(t.ID),
|
||||
t.Name,
|
||||
log.NewColoredIDValue(t.OrgID),
|
||||
t.AccessMode)
|
||||
return fmt.Sprintf("<Team %d:%s OrgID=%d AccessMode=%s>", t.ID, t.Name, t.OrgID, t.AccessMode.LogString())
|
||||
}
|
||||
|
||||
// LoadUnits load a list of available units for a team
|
||||
|
|
|
@ -102,45 +102,28 @@ func (p *Permission) CanWriteIssuesOrPulls(isPull bool) bool {
|
|||
return p.CanWrite(unit.TypeIssues)
|
||||
}
|
||||
|
||||
// ColorFormat writes a colored string for these Permissions
|
||||
func (p *Permission) ColorFormat(s fmt.State) {
|
||||
noColor := log.ColorBytes(log.Reset)
|
||||
func (p *Permission) LogString() string {
|
||||
format := "<Permission AccessMode=%s, %d Units, %d UnitsMode(s): [ "
|
||||
args := []any{p.AccessMode.String(), len(p.Units), len(p.UnitsMode)}
|
||||
|
||||
format := "perm_model.AccessMode: %-v, %d Units, %d UnitsMode(s): [ "
|
||||
args := []interface{}{
|
||||
p.AccessMode,
|
||||
log.NewColoredValueBytes(len(p.Units), &noColor),
|
||||
log.NewColoredValueBytes(len(p.UnitsMode), &noColor),
|
||||
}
|
||||
if s.Flag('+') {
|
||||
for i, unit := range p.Units {
|
||||
config := ""
|
||||
if unit.Config != nil {
|
||||
configBytes, err := unit.Config.ToDB()
|
||||
config = string(configBytes)
|
||||
if err != nil {
|
||||
config = err.Error()
|
||||
}
|
||||
for i, unit := range p.Units {
|
||||
config := ""
|
||||
if unit.Config != nil {
|
||||
configBytes, err := unit.Config.ToDB()
|
||||
config = string(configBytes)
|
||||
if err != nil {
|
||||
config = err.Error()
|
||||
}
|
||||
format += "\nUnits[%d]: ID: %d RepoID: %d Type: %-v Config: %s"
|
||||
args = append(args,
|
||||
log.NewColoredValueBytes(i, &noColor),
|
||||
log.NewColoredIDValue(unit.ID),
|
||||
log.NewColoredIDValue(unit.RepoID),
|
||||
unit.Type,
|
||||
config)
|
||||
}
|
||||
for key, value := range p.UnitsMode {
|
||||
format += "\nUnitMode[%-v]: %-v"
|
||||
args = append(args,
|
||||
key,
|
||||
value)
|
||||
}
|
||||
} else {
|
||||
format += "..."
|
||||
format += "\nUnits[%d]: ID: %d RepoID: %d Type: %s Config: %s"
|
||||
args = append(args, i, unit.ID, unit.RepoID, unit.Type.LogString(), config)
|
||||
}
|
||||
format += " ]"
|
||||
log.ColorFprintf(s, format, args...)
|
||||
for key, value := range p.UnitsMode {
|
||||
format += "\nUnitMode[%-v]: %-v"
|
||||
args = append(args, key.LogString(), value.LogString())
|
||||
}
|
||||
format += " ]>"
|
||||
return fmt.Sprintf(format, args...)
|
||||
}
|
||||
|
||||
// GetUserRepoPermission returns the user permissions to the repository
|
||||
|
|
|
@ -5,8 +5,6 @@ package perm
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
)
|
||||
|
||||
// AccessMode specifies the users access mode
|
||||
|
@ -40,11 +38,8 @@ func (mode AccessMode) String() string {
|
|||
}
|
||||
}
|
||||
|
||||
// ColorFormat provides a ColorFormatted version of this AccessMode
|
||||
func (mode AccessMode) ColorFormat(s fmt.State) {
|
||||
log.ColorFprintf(s, "%d:%s",
|
||||
log.NewColoredIDValue(mode),
|
||||
mode)
|
||||
func (mode AccessMode) LogString() string {
|
||||
return fmt.Sprintf("<AccessMode:%d:%s>", mode, mode.String())
|
||||
}
|
||||
|
||||
// ParseAccessMode returns corresponding access mode to given permission string.
|
||||
|
|
|
@ -196,19 +196,11 @@ func (repo *Repository) SanitizedOriginalURL() string {
|
|||
return u.String()
|
||||
}
|
||||
|
||||
// ColorFormat returns a colored string to represent this repo
|
||||
func (repo *Repository) ColorFormat(s fmt.State) {
|
||||
func (repo *Repository) LogString() string {
|
||||
if repo == nil {
|
||||
log.ColorFprintf(s, "%d:%s/%s",
|
||||
log.NewColoredIDValue(0),
|
||||
"<nil>",
|
||||
"<nil>")
|
||||
return
|
||||
return "<Repository nil>"
|
||||
}
|
||||
log.ColorFprintf(s, "%d:%s/%s",
|
||||
log.NewColoredIDValue(repo.ID),
|
||||
repo.OwnerName,
|
||||
repo.Name)
|
||||
return fmt.Sprintf("<Repository %d:%s/%s>", repo.ID, repo.OwnerName, repo.Name)
|
||||
}
|
||||
|
||||
// IsBeingMigrated indicates that repository is being migrated
|
||||
|
|
|
@ -62,11 +62,8 @@ func (u Type) String() string {
|
|||
return fmt.Sprintf("Unknown Type %d", u)
|
||||
}
|
||||
|
||||
// ColorFormat provides a ColorFormatted version of this Type
|
||||
func (u Type) ColorFormat(s fmt.State) {
|
||||
log.ColorFprintf(s, "%d:%s",
|
||||
log.NewColoredIDValue(u),
|
||||
u)
|
||||
func (u Type) LogString() string {
|
||||
return fmt.Sprintf("<UnitType:%d:%s>", u, u.String())
|
||||
}
|
||||
|
||||
var (
|
||||
|
|
|
@ -151,17 +151,11 @@ type SearchOrganizationsOptions struct {
|
|||
All bool
|
||||
}
|
||||
|
||||
// ColorFormat writes a colored string to identify this struct
|
||||
func (u *User) ColorFormat(s fmt.State) {
|
||||
func (u *User) LogString() string {
|
||||
if u == nil {
|
||||
log.ColorFprintf(s, "%d:%s",
|
||||
log.NewColoredIDValue(0),
|
||||
log.NewColoredValue("<nil>"))
|
||||
return
|
||||
return "<User nil>"
|
||||
}
|
||||
log.ColorFprintf(s, "%d:%s",
|
||||
log.NewColoredIDValue(u.ID),
|
||||
log.NewColoredValue(u.Name))
|
||||
return fmt.Sprintf("<User %d:%s>", u.ID, u.Name)
|
||||
}
|
||||
|
||||
// BeforeUpdate is invoked from XORM before updating this object.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue