1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 00:22:41 +08:00
This commit is contained in:
2022-04-22 14:32:29 +08:00
parent efc8210d97
commit f7df1f1668
10 changed files with 276 additions and 82 deletions

View File

@ -1,11 +1,18 @@
package logx
var std = NewLogrus()
var std = defaultLogger()
func StandardLogger() Logger {
return std
}
func defaultLogger() Logger {
return NewLogrus(
WithOptions(LogrusOptions{Level: "Debug"}),
WithOutput(LogrusOutputOptions{}),
WithFormatter(NewNestedFormatter(NestedFormatterOption{Color: true})))
}
// Fields type, used to pass to `WithFields`.
type Fields map[string]any

16
logx/logger_builder.go Normal file
View File

@ -0,0 +1,16 @@
package logx
type loggerBuilder struct {
}
func NewBuilder() *loggerBuilder {
return &loggerBuilder{}
}
func (b *loggerBuilder) WithLogrus() *loggerBuilder {
return b
}
func (b *loggerBuilder) WithLogger() *loggerBuilder {
return b
}

View File

@ -0,0 +1,15 @@
package logx_test
import (
"testing"
"github.com/charlienet/go-mixed/logx"
)
func TestBuilder(t *testing.T) {
logger := logx.NewBuilder().
WithLogrus().
WithLogger()
_ = logger
}

View File

@ -1,40 +1,29 @@
package logx
import (
"fmt"
"path"
"runtime"
nested "github.com/antonfisher/nested-logrus-formatter"
"github.com/sirupsen/logrus"
)
type logrusWrpper struct {
logrus *logrus.Entry
*logrus.Entry
}
func NewLogrus() Logger {
func NewLogrus(opts ...logrusOption) *logrusWrpper {
logger := logrus.New()
logger.SetFormatter(
&nested.Formatter{
TimestampFormat: "2006-01-02 15:04:05.000",
NoColors: false,
CustomCallerFormatter: nestedCallerFormatter,
})
logger.SetFormatter(NewNestedFormatter(NestedFormatterOption{}))
for _, o := range opts {
o(logger)
}
return &logrusWrpper{
logrus: logrus.NewEntry(logger),
Entry: logrus.NewEntry(logger),
}
}
func nestedCallerFormatter(f *runtime.Frame) string {
_, filename := path.Split(f.File)
return fmt.Sprintf(" (%s() %s:%d)", f.Function, filename, f.Line)
}
func (l *logrusWrpper) SetLevel(level Level) {
l.logrus.Logger.SetLevel(logrus.Level(level))
l.Logger.SetLevel(logrus.Level(level))
}
func (l *logrusWrpper) WithField(key string, value any) Logger {
@ -43,66 +32,6 @@ func (l *logrusWrpper) WithField(key string, value any) Logger {
func (l *logrusWrpper) WithFields(fields Fields) Logger {
return &logrusWrpper{
logrus: l.logrus.WithFields(logrus.Fields(fields)),
Entry: l.Logger.WithFields(logrus.Fields(fields)),
}
}
func (l *logrusWrpper) Trace(args ...any) {
l.logrus.Trace(args...)
}
func (l *logrusWrpper) Tracef(format string, args ...any) {
l.logrus.Tracef(format, args...)
}
func (l *logrusWrpper) Debug(args ...any) {
l.logrus.Debug(args...)
}
func (l *logrusWrpper) Debugf(format string, args ...any) {
l.logrus.Debugf(format, args...)
}
func (l *logrusWrpper) Info(args ...any) {
l.logrus.Info(args...)
}
func (l *logrusWrpper) Infof(format string, args ...any) {
l.logrus.Infof(format, args...)
}
func (l *logrusWrpper) Warn(args ...any) {
l.logrus.Warn(args...)
}
func (l *logrusWrpper) Error(args ...any) {
l.logrus.Error(args...)
}
func (l *logrusWrpper) Warnf(format string, args ...any) {
l.logrus.Warnf(format, args...)
}
func (l *logrusWrpper) Errorf(format string, args ...any) {
l.logrus.Errorf(format, args...)
}
func (l *logrusWrpper) Fatal(args ...any) {
l.logrus.Fatal(args...)
}
func (l *logrusWrpper) Fatalf(format string, args ...any) {
l.logrus.Fatalf(format, args...)
}
func (l *logrusWrpper) Println(args ...any) {
l.logrus.Infoln(args...)
}
func (l *logrusWrpper) Print(args ...any) {
l.logrus.Info(args...)
}
func (l *logrusWrpper) Printf(format string, args ...any) {
l.logrus.Infof(format, args...)
}

View File

@ -0,0 +1,28 @@
package logx
import (
"fmt"
"path"
"runtime"
nested "github.com/antonfisher/nested-logrus-formatter"
)
const defaultTimestampFormat = "2006-01-02 15:04:05.000"
type NestedFormatterOption struct {
Color bool
}
func NewNestedFormatter(option NestedFormatterOption) *nested.Formatter {
return &nested.Formatter{
TimestampFormat: defaultTimestampFormat,
NoColors: !option.Color,
CustomCallerFormatter: nestedCallerFormatter,
}
}
func nestedCallerFormatter(f *runtime.Frame) string {
_, filename := path.Split(f.File)
return fmt.Sprintf(" (%s() %s:%d)", f.Function, filename, f.Line)
}

101
logx/logrus_options.go Normal file
View File

@ -0,0 +1,101 @@
package logx
import (
"io"
"log"
"os"
"github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
)
type logrusOption func(logrusLogger *logrus.Logger)
type LogrusOptions struct {
Level string // 日志等级
ShowCaller bool // 是否记录调用者
}
type OutputType int
const (
Console OutputType = iota
File OutputType = iota
Both OutputType = iota
)
type LogrusOutputOptions struct {
FileName string // 存储文件名
Output OutputType // 输出选项
Backup LogrusBackupOptions // 文件备份选项
}
type LogrusBackupOptions struct {
MaxSize int // 默认大小100M
MaxAge int // 备份保留天数
MaxBackups int // 备份保留数量
LocalTime bool // 使用本地时间
Compress bool // 是否压缩备份
}
func (o LogrusBackupOptions) hasBackup() bool {
return o.MaxSize > 0 ||
o.MaxAge > 0 ||
o.MaxBackups > 0 ||
o.LocalTime ||
o.Compress
}
func WithOptions(o LogrusOptions) logrusOption {
return func(l *logrus.Logger) {
level, err := logrus.ParseLevel(o.Level)
if err != nil {
level = logrus.TraceLevel
}
l.SetLevel(level)
l.SetReportCaller(o.ShowCaller)
}
}
func WithFormatter(formatter logrus.Formatter) logrusOption {
return func(logrusLogger *logrus.Logger) {
logrusLogger.SetFormatter(formatter)
}
}
func WithOutput(options LogrusOutputOptions) logrusOption {
return func(l *logrus.Logger) {
var writer io.Writer
switch {
case options.Output == File, len(options.FileName) > 0:
writer = createFileWriter(options)
case options.Output == Both:
writer = io.MultiWriter(os.Stdout, createFileWriter(options))
default:
writer = os.Stdout
}
l.SetOutput(writer)
}
}
func createFileWriter(options LogrusOutputOptions) io.Writer {
if options.Backup.hasBackup() {
return &lumberjack.Logger{
Filename: options.FileName, // 日志文件名
MaxSize: options.Backup.MaxSize, // 单位兆
MaxBackups: options.Backup.MaxBackups, // 最多备份数量
MaxAge: options.Backup.MaxAge, // 保留天数
LocalTime: options.Backup.LocalTime, // 使用本地时间
Compress: options.Backup.Compress, // 是否压缩
}
}
f, err := os.OpenFile(options.FileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Panic(err)
}
return f
}

View File

@ -0,0 +1,26 @@
package logx_test
import (
"os"
"testing"
"github.com/charlienet/go-mixed/logx"
)
func TestNewLogrus(t *testing.T) {
_ = logx.WithOutput(logx.LogrusOutputOptions{
Output: logx.Console,
})
}
func TestFileOutput(t *testing.T) {
t.Log(os.Getwd())
logger := logx.NewLogrus(
// logx.WithFormatter(&logrus.TextFormatter{}),
logx.WithOptions(logx.LogrusOptions{ShowCaller: true}),
logx.WithOutput(logx.LogrusOutputOptions{
FileName: "abc.log",
}))
logger.Info("abc")
}

44
logx/logrus_test.go Normal file
View File

@ -0,0 +1,44 @@
package logx
import (
"testing"
"github.com/sirupsen/logrus"
)
func TestLogrusInfo(t *testing.T) {
l := NewLogrus()
l.Info("abcdef")
}
func TestWithField(t *testing.T) {
var l Logger = NewLogrus(WithFormatter(NewNestedFormatter(NestedFormatterOption{Color: false})))
l.WithField("aaa", "aaa").Info("aaaa")
l.WithField("bbb", "bbb").Info("bbbb")
l = l.WithField("111", "111")
l.WithField("222", "222").Info("222")
}
func TestLogrus(t *testing.T) {
l := logrus.NewEntry(logrus.New())
l.WithField("abc", "bcd").Info("aaaa")
l.WithField("bbb", "bbb").Info("bbbb")
l.WithField("ccc", "ccc").Info("cccc")
}
func TestLevel(t *testing.T) {
logger := NewLogrus()
logger.Info("abcd")
// l, _ := ParseLevel("Warn")
// logger.SetLevel(l)
logger.Info("bcdefg")
}
func TestMutiWriter(t *testing.T) {
l := NewLogger().AppendLogger()
_ = l
}

12
logx/muti_logger.go Normal file
View File

@ -0,0 +1,12 @@
package logx
type mutiLogger struct {
}
func NewLogger() *mutiLogger {
return &mutiLogger{}
}
func (w *mutiLogger) AppendLogger() Logger {
return nil
}

16
logx/std_test.go Normal file
View File

@ -0,0 +1,16 @@
package logx_test
import (
"testing"
"github.com/charlienet/go-mixed/logx"
)
func TestStandardLogger(t *testing.T) {
l := logx.StandardLogger()
l.Debug("abc")
l.Info("abc")
l.Warn("abc")
l.Error("abc")
}