Skip to content

Commit

Permalink
add logger.log_dir config
Browse files Browse the repository at this point in the history
  • Loading branch information
forbearing committed Mar 2, 2024
1 parent 36862d9 commit 7aad884
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
8 changes: 5 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ const (
NoneExpirePass = "admin"
)

var (
App = new(Config)
)
var App = new(Config)

type Mode string

Expand All @@ -38,6 +36,7 @@ func Init() (err error) {
viper.SetDefault("server.port", 9000)
viper.SetDefault("server.token_expire_duration", "24h")

viper.SetDefault("logger.log_dir", "./logs")
viper.SetDefault("logger.log_file", "./logs/server.log")
viper.SetDefault("logger.log_level", "info")
viper.SetDefault("logger.log_format", "json")
Expand Down Expand Up @@ -112,6 +111,9 @@ type ServerConfig struct {
// LoggerConfig represents section "logger" for client-side or server-side configuration,
// and there is only one copy during the application entire lifetime.
type LoggerConfig struct {
// LogDir specifies which direcotory log to.
LogDir string `json:"log_dir" ini:"log_dir" yaml:"log_dir" mapstructure:"log_dir"`

// LogFile specifies the which file log to.
// If value is "/dev/stdout", log to os.Stdout.
// If value is "/dev/stderr", log to os.Stderr.
Expand Down
27 changes: 14 additions & 13 deletions logger/zap/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package zap
import (
"log"
"os"
"path/filepath"
"strings"

"github.com/forbearing/golib/config"
Expand Down Expand Up @@ -40,14 +41,14 @@ func Init() error {
zap.AddStacktrace(zapcore.FatalLevel),
))
logger.Global = New()
logger.Controller = New("logs/controller.log")
logger.Service = New("logs/service.log")
logger.Database = New("logs/database.log")
logger.Redis = New("logs/redis.log")
logger.Task = New("logs/task.log")
logger.Visitor = New("logs/visitor.log")
logger.Cronjob = New("logs/cronjob.log")
logger.Job = New("logs/job.log")
logger.Controller = New(filepath.Join(config.App.LogDir, "controller.log"))
logger.Service = New(filepath.Join(config.App.LogDir, "service.log"))
logger.Database = New(filepath.Join(config.App.LogDir, "database.log"))
logger.Redis = New(filepath.Join(config.App.LogDir, "redis.log"))
logger.Task = New(filepath.Join(config.App.LogDir, "task.log"))
logger.Visitor = New(filepath.Join(config.App.LogDir, "visitor.log"))
logger.Cronjob = New(filepath.Join(config.App.LogDir, "cronjob.log"))
logger.Job = New(filepath.Join(config.App.LogDir, "job.log"))
logger.Gin = NewGin()
logger.Gorm = NewGorm("logs/gorm.log")
// if len(logFile) != 0 {
Expand Down Expand Up @@ -175,7 +176,7 @@ func newLogLevel(_ ...option) zapcore.Level {
if len(logLevel) == 0 {
return zapcore.InfoLevel
}
var level = new(zapcore.Level)
level := new(zapcore.Level)
if err := level.UnmarshalText([]byte(logLevel)); err != nil {
return zapcore.InfoLevel
}
Expand All @@ -185,11 +186,11 @@ func newLogLevel(_ ...option) zapcore.Level {
// newLogEncoder
func newLogEncoder(opt ...option) zapcore.Encoder {
encConfig := zap.NewProductionEncoderConfig()
//encConfig.EncodeTime = zapcore.RFC3339TimeEncoder
// encConfig.EncodeTime = zapcore.RFC3339TimeEncoder
// encConfig.EncodeTime = zapcore.ISO8601TimeEncoder
//encConfig.EncodeDuration = zapcore.MillisDurationEncoder
//encConfig.EncodeCaller = zapcore.ShortCallerEncoder
//encConfig.EncodeLevel = zapcore.LowercaseLevelEncoder
// encConfig.EncodeDuration = zapcore.MillisDurationEncoder
// encConfig.EncodeCaller = zapcore.ShortCallerEncoder
// encConfig.EncodeLevel = zapcore.LowercaseLevelEncoder
encConfig.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05")
if len(opt) > 0 {
o := opt[0]
Expand Down

0 comments on commit 7aad884

Please sign in to comment.