restructure

This commit is contained in:
yanghao05
2023-04-20 12:11:34 +08:00
parent 6757e00d73
commit 5b8eca5d87
120 changed files with 546 additions and 7303 deletions

28
providers/log/config.go Normal file
View File

@@ -0,0 +1,28 @@
package log
type Config struct {
Level Level
}
type Level int8
const (
// DebugLevel logs are typically voluminous, and are usually disabled in
// production.
DebugLevel Level = iota - 1
// InfoLevel is the default logging priority.
InfoLevel
// WarnLevel logs are more important than Info, but don't need individual
// human review.
WarnLevel
// ErrorLevel logs are high-priority. If an application is running smoothly,
// it shouldn't generate any error-level logs.
ErrorLevel
// DPanicLevel logs are particularly important errors. In development the
// logger panics after writing the message.
DPanicLevel
// PanicLevel logs a message, then panics.
PanicLevel
// FatalLevel logs a message, then calls os.Exit(1).
FatalLevel
)

View File

@@ -2,20 +2,18 @@ package log
import (
"strings"
"go.uber.org/zap/zapcore"
)
type LevelWriter struct {
Level zapcore.Level
Level Level
}
func (w LevelWriter) Write(p []byte) (n int, err error) {
str := strings.TrimSpace(string(p))
switch w.Level {
case zapcore.InfoLevel:
case InfoLevel:
Info(str)
case zapcore.ErrorLevel:
case ErrorLevel:
Error(str)
}
return len(p), nil

View File

@@ -1,99 +1,103 @@
package log
import (
"atom/container"
"log"
"github.com/rogeecn/atom/container"
"go.uber.org/dig"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func init() {
if err := container.Container.Provide(NewZapLogger); err != nil {
log.Fatal(err)
}
func Provide(config *Config, opts ...dig.ProvideOption) error {
return container.Container.Provide(func() (*Logger, error) {
logger, err := newZapLogger(config)
if err != nil {
return nil, err
}
defaultLogger = logger
return logger, nil
}, opts...)
}
var DefaultLogger *Logger
var defaultLogger *Logger
type Logger struct {
logger *zap.SugaredLogger
}
func (l *Logger) LevelWriter(level zapcore.Level) *LevelWriter {
func (l *Logger) LevelWriter(level Level) *LevelWriter {
return &LevelWriter{Level: level}
}
// Debug uses fmt.Sprint to construct and log a message.
func Debug(args ...interface{}) {
DefaultLogger.logger.Debug(args...)
defaultLogger.logger.Debug(args...)
}
// Info uses fmt.Sprint to construct and log a message.
func Info(args ...interface{}) {
DefaultLogger.logger.Info(args...)
defaultLogger.logger.Info(args...)
}
// Warn uses fmt.Sprint to construct and log a message.
func Warn(args ...interface{}) {
DefaultLogger.logger.Warn(args...)
defaultLogger.logger.Warn(args...)
}
// Error uses fmt.Sprint to construct and log a message.
func Error(args ...interface{}) {
DefaultLogger.logger.Error(args...)
defaultLogger.logger.Error(args...)
}
// DPanic uses fmt.Sprint to construct and log a message. In development, the
// logger then panics. (See DPanicLevel for details.)
func DPanic(args ...interface{}) {
DefaultLogger.logger.DPanic(args...)
defaultLogger.logger.DPanic(args...)
}
// Panic uses fmt.Sprint to construct and log a message, then panics.
func Panic(args ...interface{}) {
DefaultLogger.logger.Panic(args...)
defaultLogger.logger.Panic(args...)
}
// Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit.
func Fatal(args ...interface{}) {
DefaultLogger.logger.Fatal(args...)
defaultLogger.logger.Fatal(args...)
}
// Debugf uses fmt.Sprintf to log a templated message.
func Debugf(template string, args ...interface{}) {
DefaultLogger.logger.Debugf(template, args...)
defaultLogger.logger.Debugf(template, args...)
}
// Infof uses fmt.Sprintf to log a templated message.
func Infof(template string, args ...interface{}) {
DefaultLogger.logger.Infof(template, args...)
defaultLogger.logger.Infof(template, args...)
}
// Warnf uses fmt.Sprintf to log a templated message.
func Warnf(template string, args ...interface{}) {
DefaultLogger.logger.Warnf(template, args...)
defaultLogger.logger.Warnf(template, args...)
}
// Errorf uses fmt.Sprintf to log a templated message.
func Errorf(template string, args ...interface{}) {
DefaultLogger.logger.Errorf(template, args...)
defaultLogger.logger.Errorf(template, args...)
}
// DPanicf uses fmt.Sprintf to log a templated message. In development, the
// logger then panics. (See DPanicLevel for details.)
func DPanicf(template string, args ...interface{}) {
DefaultLogger.logger.DPanicf(template, args...)
defaultLogger.logger.DPanicf(template, args...)
}
// Panicf uses fmt.Sprintf to log a templated message, then panics.
func Panicf(template string, args ...interface{}) {
DefaultLogger.logger.Panicf(template, args...)
defaultLogger.logger.Panicf(template, args...)
}
// Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.
func Fatalf(template string, args ...interface{}) {
DefaultLogger.logger.Fatalf(template, args...)
defaultLogger.logger.Fatalf(template, args...)
}
// Debugw logs a message with some additional context. The variadic key-value
@@ -103,47 +107,47 @@ func Fatalf(template string, args ...interface{}) {
//
// s.With(keysAndValues).Debug(msg)
func Debugw(msg string, keysAndValues ...interface{}) {
DefaultLogger.logger.Debugw(msg, keysAndValues...)
defaultLogger.logger.Debugw(msg, keysAndValues...)
}
// Infow logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func Infow(msg string, keysAndValues ...interface{}) {
DefaultLogger.logger.Infow(msg, keysAndValues...)
defaultLogger.logger.Infow(msg, keysAndValues...)
}
// Warnw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func Warnw(msg string, keysAndValues ...interface{}) {
DefaultLogger.logger.Warnw(msg, keysAndValues...)
defaultLogger.logger.Warnw(msg, keysAndValues...)
}
// Errorw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func Errorw(msg string, keysAndValues ...interface{}) {
DefaultLogger.logger.Errorw(msg, keysAndValues...)
defaultLogger.logger.Errorw(msg, keysAndValues...)
}
// DPanicw logs a message with some additional context. In development, the
// logger then panics. (See DPanicLevel for details.) The variadic key-value
// pairs are treated as they are in With.
func DPanicw(msg string, keysAndValues ...interface{}) {
DefaultLogger.logger.DPanicw(msg, keysAndValues...)
defaultLogger.logger.DPanicw(msg, keysAndValues...)
}
// Panicw logs a message with some additional context, then panics. The
// variadic key-value pairs are treated as they are in With.
func Panicw(msg string, keysAndValues ...interface{}) {
DefaultLogger.logger.Panicw(msg, keysAndValues...)
defaultLogger.logger.Panicw(msg, keysAndValues...)
}
// Fatalw logs a message with some additional context, then calls os.Exit. The
// variadic key-value pairs are treated as they are in With.
func Fatalw(msg string, keysAndValues ...interface{}) {
DefaultLogger.logger.Fatalw(msg, keysAndValues...)
defaultLogger.logger.Fatalw(msg, keysAndValues...)
}
// Sync flushes any buffered log entries.
func Sync() error {
return DefaultLogger.logger.Sync()
return defaultLogger.logger.Sync()
}

View File

@@ -1,16 +1,13 @@
package log
import (
"atom/providers/config"
"go.uber.org/zap"
)
func NewZapLogger(conf *config.Config) (*Logger, error) {
func newZapLogger(conf *Config) (*Logger, error) {
logger, err := zap.NewDevelopment()
if err != nil {
return nil, err
}
DefaultLogger = &Logger{logger: logger.Sugar()}
return DefaultLogger, nil
return &Logger{logger: logger.Sugar()}, nil
}