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

Make the logger an interface #13294

Merged
merged 3 commits into from
Oct 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion models/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// XORMLogBridge a logger bridge from Logger to xorm
type XORMLogBridge struct {
showSQL bool
logger *log.Logger
logger log.Logger
}

// NewXORMLogger inits a log bridge for xorm
Expand Down
2 changes: 1 addition & 1 deletion modules/indexer/code/elastic_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type ElasticSearchIndexer struct {
}

type elasticLogger struct {
*log.Logger
log.Logger
}

func (l elasticLogger) Printf(format string, args ...interface{}) {
Expand Down
4 changes: 2 additions & 2 deletions modules/indexer/issues/elastic_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ type ElasticSearchIndexer struct {
}

type elasticLogger struct {
*log.Logger
log.LevelLogger
}

func (l elasticLogger) Printf(format string, args ...interface{}) {
_ = l.Logger.Log(2, l.Logger.GetLevel(), format, args...)
_ = l.Log(2, l.GetLevel(), format, args...)
}

// NewElasticSearchIndexer creates a new elasticsearch indexer
Expand Down
18 changes: 9 additions & 9 deletions modules/log/colors.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,15 @@ func ColorBytes(attrs ...ColorAttribute) []byte {
return bytes
}

var levelToColor = map[Level]string{
TRACE: ColorString(Bold, FgCyan),
DEBUG: ColorString(Bold, FgBlue),
INFO: ColorString(Bold, FgGreen),
WARN: ColorString(Bold, FgYellow),
ERROR: ColorString(Bold, FgRed),
CRITICAL: ColorString(Bold, BgMagenta),
FATAL: ColorString(Bold, BgRed),
NONE: ColorString(Reset),
var levelToColor = map[Level][]byte{
TRACE: ColorBytes(Bold, FgCyan),
DEBUG: ColorBytes(Bold, FgBlue),
INFO: ColorBytes(Bold, FgGreen),
WARN: ColorBytes(Bold, FgYellow),
ERROR: ColorBytes(Bold, FgRed),
CRITICAL: ColorBytes(Bold, BgMagenta),
FATAL: ColorBytes(Bold, BgRed),
NONE: ColorBytes(Reset),
}

var resetBytes = ColorBytes(Reset)
Expand Down
10 changes: 10 additions & 0 deletions modules/log/level.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ func (l Level) String() string {
return "info"
}

// Color returns the color string for this Level
func (l Level) Color() *[]byte {
color, ok := levelToColor[l]
if ok {
return &(color)
}
none := levelToColor[NONE]
return &none
}

// MarshalJSON takes a Level and turns it into text
func (l Level) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBufferString(`"`)
Expand Down
22 changes: 11 additions & 11 deletions modules/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ type loggerMap struct {
sync.Map
}

func (m *loggerMap) Load(k string) (*Logger, bool) {
func (m *loggerMap) Load(k string) (*MultiChannelledLogger, bool) {
v, ok := m.Map.Load(k)
if !ok {
return nil, false
}
l, ok := v.(*Logger)
l, ok := v.(*MultiChannelledLogger)
return l, ok
}

func (m *loggerMap) Store(k string, v *Logger) {
func (m *loggerMap) Store(k string, v *MultiChannelledLogger) {
m.Map.Store(k, v)
}

Expand All @@ -42,7 +42,7 @@ var (
)

// NewLogger create a logger for the default logger
func NewLogger(bufLen int64, name, provider, config string) *Logger {
func NewLogger(bufLen int64, name, provider, config string) *MultiChannelledLogger {
err := NewNamedLogger(DEFAULT, bufLen, name, provider, config)
if err != nil {
CriticalWithSkip(1, "Unable to create default logger: %v", err)
Expand Down Expand Up @@ -83,7 +83,7 @@ func DelLogger(name string) error {
}

// GetLogger returns either a named logger or the default logger
func GetLogger(name string) *Logger {
func GetLogger(name string) *MultiChannelledLogger {
logger, ok := NamedLoggers.Load(name)
if ok {
return logger
Expand Down Expand Up @@ -196,7 +196,7 @@ func IsFatal() bool {
// Pause pauses all the loggers
func Pause() {
NamedLoggers.Range(func(key, value interface{}) bool {
logger := value.(*Logger)
logger := value.(*MultiChannelledLogger)
logger.Pause()
logger.Flush()
return true
Expand All @@ -206,7 +206,7 @@ func Pause() {
// Resume resumes all the loggers
func Resume() {
NamedLoggers.Range(func(key, value interface{}) bool {
logger := value.(*Logger)
logger := value.(*MultiChannelledLogger)
logger.Resume()
return true
})
Expand All @@ -216,7 +216,7 @@ func Resume() {
func ReleaseReopen() error {
var accumulatedErr error
NamedLoggers.Range(func(key, value interface{}) bool {
logger := value.(*Logger)
logger := value.(*MultiChannelledLogger)
if err := logger.ReleaseReopen(); err != nil {
if accumulatedErr == nil {
accumulatedErr = fmt.Errorf("Error reopening %s: %v", key.(string), err)
Expand Down Expand Up @@ -250,15 +250,15 @@ func Log(skip int, level Level, format string, v ...interface{}) {

// LoggerAsWriter is a io.Writer shim around the gitea log
type LoggerAsWriter struct {
ourLoggers []*Logger
ourLoggers []*MultiChannelledLogger
level Level
}

// NewLoggerAsWriter creates a Writer representation of the logger with setable log level
func NewLoggerAsWriter(level string, ourLoggers ...*Logger) *LoggerAsWriter {
func NewLoggerAsWriter(level string, ourLoggers ...*MultiChannelledLogger) *LoggerAsWriter {
if len(ourLoggers) == 0 {
l, _ := NamedLoggers.Load(DEFAULT)
ourLoggers = []*Logger{l}
ourLoggers = []*MultiChannelledLogger{l}
}
l := &LoggerAsWriter{
ourLoggers: ourLoggers,
Expand Down
2 changes: 1 addition & 1 deletion modules/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/assert"
)

func baseConsoleTest(t *testing.T, logger *Logger) (chan []byte, chan bool) {
func baseConsoleTest(t *testing.T, logger *MultiChannelledLogger) (chan []byte, chan bool) {
written := make(chan []byte)
closed := make(chan bool)

Expand Down
Loading