summaryrefslogtreecommitdiffstats
path: root/modules/log/misc.go
blob: ae4ce04cf32b17a10641302c8606df253f2a1f9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package log

import (
	"io"
)

type baseToLogger struct {
	base BaseLogger
}

// BaseLoggerToGeneralLogger wraps a BaseLogger (which only has Log() function) to a Logger (which has Info() function)
func BaseLoggerToGeneralLogger(b BaseLogger) Logger {
	l := &baseToLogger{base: b}
	return l
}

var _ Logger = (*baseToLogger)(nil)

func (s *baseToLogger) Log(skip int, level Level, format string, v ...any) {
	s.base.Log(skip+1, level, format, v...)
}

func (s *baseToLogger) GetLevel() Level {
	return s.base.GetLevel()
}

func (s *baseToLogger) LevelEnabled(level Level) bool {
	return s.base.GetLevel() <= level
}

func (s *baseToLogger) Trace(format string, v ...any) {
	s.base.Log(1, TRACE, format, v...)
}

func (s *baseToLogger) Debug(format string, v ...any) {
	s.base.Log(1, DEBUG, format, v...)
}

func (s *baseToLogger) Info(format string, v ...any) {
	s.base.Log(1, INFO, format, v...)
}

func (s *baseToLogger) Warn(format string, v ...any) {
	s.base.Log(1, WARN, format, v...)
}

func (s *baseToLogger) Error(format string, v ...any) {
	s.base.Log(1, ERROR, format, v...)
}

func (s *baseToLogger) Critical(format string, v ...any) {
	s.base.Log(1, CRITICAL, format, v...)
}

type PrintfLogger struct {
	Logf func(format string, args ...any)
}

func (p *PrintfLogger) Printf(format string, args ...any) {
	p.Logf(format, args...)
}

type loggerToWriter struct {
	logf func(format string, args ...any)
}

func (p *loggerToWriter) Write(bs []byte) (int, error) {
	p.logf("%s", string(bs))
	return len(bs), nil
}

// LoggerToWriter wraps a log function to an io.Writer
func LoggerToWriter(logf func(format string, args ...any)) io.Writer {
	return &loggerToWriter{logf: logf}
}