[F3] util.Logger

(cherry picked from commit 79741e56c7)
This commit is contained in:
Earl Warren 2024-01-26 12:40:41 +01:00
parent 0f8376b0cf
commit 1d77f3afee
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
7 changed files with 193 additions and 6 deletions

2
go.mod
View file

@ -117,7 +117,7 @@ require (
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gopkg.in/ini.v1 v1.67.0 gopkg.in/ini.v1 v1.67.0
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
lab.forgefriends.org/friendlyforgeformat/gof3 v1.0.1-0.20240122075607-5d314b108abb lab.forgefriends.org/friendlyforgeformat/gof3 v1.0.1-0.20240126111813-4c40a08fea9f
mvdan.cc/xurls/v2 v2.5.0 mvdan.cc/xurls/v2 v2.5.0
strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251 strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251
xorm.io/builder v0.3.13 xorm.io/builder v0.3.13

4
go.sum
View file

@ -1287,8 +1287,8 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
lab.forgefriends.org/friendlyforgeformat/gof3 v1.0.1-0.20240122075607-5d314b108abb h1:kS8Jkycu2kggyVggjxC7I24iyDLOB2WqAJ8DlV21Fis= lab.forgefriends.org/friendlyforgeformat/gof3 v1.0.1-0.20240126111813-4c40a08fea9f h1:+Q6wWzybQgshhH6VzdjdpvsF/iJRvVk0YCyEgdZiJIQ=
lab.forgefriends.org/friendlyforgeformat/gof3 v1.0.1-0.20240122075607-5d314b108abb/go.mod h1:LxfG8cikPNzmy77CCYM4b9t/NbFefGJ1rUZujqDNa8Y= lab.forgefriends.org/friendlyforgeformat/gof3 v1.0.1-0.20240126111813-4c40a08fea9f/go.mod h1:LxfG8cikPNzmy77CCYM4b9t/NbFefGJ1rUZujqDNa8Y=
lukechampine.com/uint128 v1.2.0 h1:mBi/5l91vocEN8otkC5bDLhi2KdCticRiwbdB0O+rjI= lukechampine.com/uint128 v1.2.0 h1:mBi/5l91vocEN8otkC5bDLhi2KdCticRiwbdB0O+rjI=
lukechampine.com/uint128 v1.2.0/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk= lukechampine.com/uint128 v1.2.0/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk=
modernc.org/cc/v3 v3.40.0 h1:P3g79IUS/93SYhtoeaHW+kRCIrYaxJ27MFPv+7kaTOw= modernc.org/cc/v3 v3.40.0 h1:P3g79IUS/93SYhtoeaHW+kRCIrYaxJ27MFPv+7kaTOw=

View file

@ -7,13 +7,13 @@ package driver
import ( import (
"testing" "testing"
"code.gitea.io/gitea/models/unittest"
_ "code.gitea.io/gitea/models" _ "code.gitea.io/gitea/models"
_ "code.gitea.io/gitea/models/actions" _ "code.gitea.io/gitea/models/actions"
_ "code.gitea.io/gitea/models/activities" _ "code.gitea.io/gitea/models/activities"
_ "code.gitea.io/gitea/models/perm/access" _ "code.gitea.io/gitea/models/perm/access"
"code.gitea.io/gitea/models/unittest"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )

View file

@ -8,6 +8,7 @@ import (
"net/http" "net/http"
forgejo_options "code.gitea.io/gitea/services/f3/driver/options" forgejo_options "code.gitea.io/gitea/services/f3/driver/options"
"lab.forgefriends.org/friendlyforgeformat/gof3/options" "lab.forgefriends.org/friendlyforgeformat/gof3/options"
) )

View file

@ -6,6 +6,7 @@ package driver
import ( import (
forgejo_options "code.gitea.io/gitea/services/f3/driver/options" forgejo_options "code.gitea.io/gitea/services/f3/driver/options"
"lab.forgefriends.org/friendlyforgeformat/gof3/tree/generic" "lab.forgefriends.org/friendlyforgeformat/gof3/tree/generic"
) )
@ -23,7 +24,6 @@ func newTreeDriver(tree generic.TreeInterface, anyOptions any) generic.TreeDrive
driver := &treeDriver{ driver := &treeDriver{
options: anyOptions.(*forgejo_options.Options), options: anyOptions.(*forgejo_options.Options),
} }
driver.SetTree(tree)
driver.Init() driver.Init()
return driver return driver
} }

View file

@ -0,0 +1,97 @@
// Copyright Earl Warren <contact@earl-warren.org>
// SPDX-License-Identifier: MIT
package util
import (
"fmt"
forgejo_log "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/migration"
"lab.forgefriends.org/friendlyforgeformat/gof3/logger"
)
type f3Logger struct {
m migration.Messenger
l forgejo_log.Logger
}
func (o *f3Logger) Message(message string, args ...any) {
if o.m != nil {
o.m(message, args...)
}
}
func (o *f3Logger) SetLevel(level logger.Level) {
}
func forgejoLevelToF3Level(level forgejo_log.Level) logger.Level {
switch level {
case forgejo_log.TRACE:
return logger.Trace
case forgejo_log.DEBUG:
return logger.Debug
case forgejo_log.INFO:
return logger.Info
case forgejo_log.WARN:
return logger.Warn
case forgejo_log.ERROR:
return logger.Error
case forgejo_log.FATAL:
return logger.Fatal
default:
panic(fmt.Errorf("unexpected level %d", level))
}
}
func f3LevelToForgejoLevel(level logger.Level) forgejo_log.Level {
switch level {
case logger.Trace:
return forgejo_log.TRACE
case logger.Debug:
return forgejo_log.DEBUG
case logger.Info:
return forgejo_log.INFO
case logger.Warn:
return forgejo_log.WARN
case logger.Error:
return forgejo_log.ERROR
case logger.Fatal:
return forgejo_log.FATAL
default:
panic(fmt.Errorf("unexpected level %d", level))
}
}
func (o *f3Logger) GetLevel() logger.Level {
return forgejoLevelToF3Level(o.l.GetLevel())
}
func (o *f3Logger) Log(skip int, level logger.Level, format string, args ...any) {
o.l.Log(skip+1, f3LevelToForgejoLevel(level), format, args...)
}
func (o *f3Logger) Trace(message string, args ...any) {
o.l.Log(1, forgejo_log.TRACE, message, args...)
}
func (o *f3Logger) Debug(message string, args ...any) {
o.l.Log(1, forgejo_log.DEBUG, message, args...)
}
func (o *f3Logger) Info(message string, args ...any) { o.l.Log(1, forgejo_log.INFO, message, args...) }
func (o *f3Logger) Warn(message string, args ...any) { o.l.Log(1, forgejo_log.WARN, message, args...) }
func (o *f3Logger) Error(message string, args ...any) {
o.l.Log(1, forgejo_log.ERROR, message, args...)
}
func (o *f3Logger) Fatal(message string, args ...any) {
o.l.Log(1, forgejo_log.FATAL, message, args...)
}
func NewF3Logger(messenger migration.Messenger, logger forgejo_log.Logger) logger.Interface {
return &f3Logger{
m: messenger,
l: logger,
}
}

View file

@ -0,0 +1,89 @@
// Copyright Earl Warren <contact@earl-warren.org>
// SPDX-License-Identifier: MIT
package util
import (
"fmt"
"testing"
"time"
forgejo_log "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"lab.forgefriends.org/friendlyforgeformat/gof3/logger"
)
func TestF3UtilMessage(t *testing.T) {
expected := "EXPECTED MESSAGE"
var actual string
logger := NewF3Logger(func(message string, args ...any) {
actual = fmt.Sprintf(message, args...)
}, nil)
logger.Message("EXPECTED %s", "MESSAGE")
assert.EqualValues(t, expected, actual)
}
func TestF3UtilLogger(t *testing.T) {
for _, testCase := range []struct {
level logger.Level
call func(logger.MessageInterface, string, ...any)
}{
{level: logger.Trace, call: func(logger logger.MessageInterface, message string, args ...any) { logger.Trace(message, args...) }},
{level: logger.Debug, call: func(logger logger.MessageInterface, message string, args ...any) { logger.Debug(message, args...) }},
{level: logger.Info, call: func(logger logger.MessageInterface, message string, args ...any) { logger.Info(message, args...) }},
{level: logger.Warn, call: func(logger logger.MessageInterface, message string, args ...any) { logger.Warn(message, args...) }},
{level: logger.Error, call: func(logger logger.MessageInterface, message string, args ...any) { logger.Error(message, args...) }},
{level: logger.Fatal, call: func(logger logger.MessageInterface, message string, args ...any) { logger.Fatal(message, args...) }},
} {
t.Run(testCase.level.String(), func(t *testing.T) {
testLoggerCase(t, testCase.level, testCase.call)
})
}
}
func testLoggerCase(t *testing.T, level logger.Level, loggerFunc func(logger.MessageInterface, string, ...any)) {
lc, cleanup := test.NewLogChecker(forgejo_log.DEFAULT, f3LevelToForgejoLevel(level))
defer cleanup()
stopMark := "STOP"
lc.StopMark(stopMark)
filtered := []string{
"MESSAGE HERE",
}
moreVerbose := logger.MoreVerbose(level)
if moreVerbose != nil {
filtered = append(filtered, "MESSAGE MORE VERBOSE")
}
lessVerbose := logger.LessVerbose(level)
if lessVerbose != nil {
filtered = append(filtered, "MESSAGE LESS VERBOSE")
}
lc.Filter(filtered...)
logger := NewF3Logger(nil, forgejo_log.GetLogger(forgejo_log.DEFAULT))
loggerFunc(logger, "MESSAGE %s", "HERE")
if moreVerbose != nil {
logger.Log(1, *moreVerbose, "MESSAGE %s", "MORE VERBOSE")
}
if lessVerbose != nil {
logger.Log(1, *lessVerbose, "MESSAGE %s", "LESS VERBOSE")
}
logger.Fatal(stopMark)
logFiltered, logStopped := lc.Check(5 * time.Second)
assert.True(t, logStopped)
i := 0
assert.True(t, logFiltered[i], filtered[i])
if moreVerbose != nil {
i++
require.True(t, len(logFiltered) > i)
assert.False(t, logFiltered[i], filtered[i])
}
if lessVerbose != nil {
i++
require.True(t, len(logFiltered) > i)
assert.True(t, logFiltered[i], filtered[i])
}
}