modify logger -> log
This commit is contained in:
@@ -3,8 +3,8 @@ package mysql
|
|||||||
import (
|
import (
|
||||||
"atom/container"
|
"atom/container"
|
||||||
"atom/providers/config"
|
"atom/providers/config"
|
||||||
"atom/providers/logger"
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"log"
|
||||||
|
|
||||||
"gorm.io/driver/mysql"
|
"gorm.io/driver/mysql"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
if err := container.Container.Provide(NewDatabase); err != nil {
|
if err := container.Container.Provide(NewDatabase); err != nil {
|
||||||
logger.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,9 +3,8 @@ package http
|
|||||||
import (
|
import (
|
||||||
"atom/container"
|
"atom/container"
|
||||||
"atom/providers/config"
|
"atom/providers/config"
|
||||||
"atom/providers/logger"
|
"atom/providers/log"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -30,7 +29,7 @@ func (e *Service) Serve() error {
|
|||||||
return e.Engine.Run(e.conf.Http.PortString())
|
return e.Engine.Run(e.conf.Http.PortString())
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewService(cfg *config.Config, logger *logger.Logger) *Service {
|
func NewService(cfg *config.Config, logger *log.Logger) *Service {
|
||||||
gin.DefaultWriter = logger.LevelWriter(zap.InfoLevel)
|
gin.DefaultWriter = logger.LevelWriter(zap.InfoLevel)
|
||||||
gin.DefaultErrorWriter = logger.LevelWriter(zap.ErrorLevel)
|
gin.DefaultErrorWriter = logger.LevelWriter(zap.ErrorLevel)
|
||||||
|
|
||||||
|
|||||||
@@ -7,8 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type LevelWriter struct {
|
type LevelWriter struct {
|
||||||
Logger *Logger
|
Level zapcore.Level
|
||||||
Level zapcore.Level
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w LevelWriter) Write(p []byte) (n int, err error) {
|
func (w LevelWriter) Write(p []byte) (n int, err error) {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
"go.uber.org/zap/zapcore"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -19,6 +20,10 @@ type Logger struct {
|
|||||||
logger *zap.SugaredLogger
|
logger *zap.SugaredLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *Logger) LevelWriter(level zapcore.Level) *LevelWriter {
|
||||||
|
return &LevelWriter{Level: level}
|
||||||
|
}
|
||||||
|
|
||||||
// Debug uses fmt.Sprint to construct and log a message.
|
// Debug uses fmt.Sprint to construct and log a message.
|
||||||
func Debug(args ...interface{}) {
|
func Debug(args ...interface{}) {
|
||||||
DefaultLogger.logger.Debug(args...)
|
DefaultLogger.logger.Debug(args...)
|
||||||
|
|||||||
@@ -2,13 +2,11 @@ package log
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"atom/providers/config"
|
"atom/providers/config"
|
||||||
"log"
|
|
||||||
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewZapLogger(conf *config.Config) (*Logger, error) {
|
func NewZapLogger(conf *config.Config) (*Logger, error) {
|
||||||
log.Print("init logger")
|
|
||||||
logger, err := zap.NewDevelopment()
|
logger, err := zap.NewDevelopment()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -1,22 +0,0 @@
|
|||||||
package logger
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"go.uber.org/zap/zapcore"
|
|
||||||
)
|
|
||||||
|
|
||||||
type LevelWriter struct {
|
|
||||||
Level zapcore.Level
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w LevelWriter) Write(p []byte) (n int, err error) {
|
|
||||||
str := strings.TrimSpace(string(p))
|
|
||||||
switch w.Level {
|
|
||||||
case zapcore.InfoLevel:
|
|
||||||
Info(str)
|
|
||||||
case zapcore.ErrorLevel:
|
|
||||||
Error(str)
|
|
||||||
}
|
|
||||||
return len(p), nil
|
|
||||||
}
|
|
||||||
@@ -1,149 +0,0 @@
|
|||||||
package logger
|
|
||||||
|
|
||||||
import (
|
|
||||||
"atom/container"
|
|
||||||
"log"
|
|
||||||
|
|
||||||
"go.uber.org/zap"
|
|
||||||
"go.uber.org/zap/zapcore"
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
if err := container.Container.Provide(NewZapLogger); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var DefaultLogger *Logger
|
|
||||||
|
|
||||||
type Logger struct {
|
|
||||||
logger *zap.SugaredLogger
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *Logger) LevelWriter(level zapcore.Level) *LevelWriter {
|
|
||||||
return &LevelWriter{Level: level}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Debug uses fmt.Sprint to construct and log a message.
|
|
||||||
func Debug(args ...interface{}) {
|
|
||||||
DefaultLogger.logger.Debug(args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Info uses fmt.Sprint to construct and log a message.
|
|
||||||
func Info(args ...interface{}) {
|
|
||||||
DefaultLogger.logger.Info(args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Warn uses fmt.Sprint to construct and log a message.
|
|
||||||
func Warn(args ...interface{}) {
|
|
||||||
DefaultLogger.logger.Warn(args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Error uses fmt.Sprint to construct and log a message.
|
|
||||||
func Error(args ...interface{}) {
|
|
||||||
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...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Panic uses fmt.Sprint to construct and log a message, then panics.
|
|
||||||
func Panic(args ...interface{}) {
|
|
||||||
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...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Debugf uses fmt.Sprintf to log a templated message.
|
|
||||||
func Debugf(template string, args ...interface{}) {
|
|
||||||
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...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Warnf uses fmt.Sprintf to log a templated message.
|
|
||||||
func Warnf(template string, args ...interface{}) {
|
|
||||||
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...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Panicf uses fmt.Sprintf to log a templated message, then panics.
|
|
||||||
func Panicf(template string, args ...interface{}) {
|
|
||||||
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...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Debugw logs a message with some additional context. The variadic key-value
|
|
||||||
// pairs are treated as they are in With.
|
|
||||||
//
|
|
||||||
// When debug-level logging is disabled, this is much faster than
|
|
||||||
//
|
|
||||||
// s.With(keysAndValues).Debug(msg)
|
|
||||||
func Debugw(msg string, keysAndValues ...interface{}) {
|
|
||||||
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...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sync flushes any buffered log entries.
|
|
||||||
func Sync() error {
|
|
||||||
return DefaultLogger.logger.Sync()
|
|
||||||
}
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
package logger
|
|
||||||
|
|
||||||
import (
|
|
||||||
"atom/providers/config"
|
|
||||||
|
|
||||||
"go.uber.org/zap"
|
|
||||||
)
|
|
||||||
|
|
||||||
func NewZapLogger(conf *config.Config) (*Logger, error) {
|
|
||||||
logger, err := zap.NewDevelopment()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
DefaultLogger = &Logger{logger: logger.Sugar()}
|
|
||||||
return DefaultLogger, nil
|
|
||||||
}
|
|
||||||
@@ -5,6 +5,6 @@ import (
|
|||||||
_ "atom/providers/database"
|
_ "atom/providers/database"
|
||||||
_ "atom/providers/faker"
|
_ "atom/providers/faker"
|
||||||
_ "atom/providers/http"
|
_ "atom/providers/http"
|
||||||
_ "atom/providers/logger"
|
_ "atom/providers/log"
|
||||||
_ "atom/providers/micro"
|
_ "atom/providers/micro"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"atom/contracts"
|
"atom/contracts"
|
||||||
"atom/providers/config"
|
"atom/providers/config"
|
||||||
"atom/providers/http"
|
"atom/providers/http"
|
||||||
"atom/providers/logger"
|
"atom/providers/log"
|
||||||
|
|
||||||
"go.uber.org/dig"
|
"go.uber.org/dig"
|
||||||
)
|
)
|
||||||
@@ -20,11 +20,11 @@ type Http struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Serve(http Http) error {
|
func Serve(http Http) error {
|
||||||
logger.Infof("http service port %s", http.Conf.Http.Address())
|
log.Infof("http service port %s", http.Conf.Http.Address())
|
||||||
for _, route := range http.Routes {
|
for _, route := range http.Routes {
|
||||||
route.Register()
|
route.Register()
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Infof("starting server on %s", http.Conf.Http.Address())
|
log.Infof("starting server on %s", http.Conf.Http.Address())
|
||||||
return http.Service.Serve()
|
return http.Service.Serve()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user