feat: choice whether to log to file

Signed-off-by: ztelliot <ztell@foxmail.com>
pull/357/head
ztelliot 1 year ago
parent 7d918575a0
commit 0b6e206a29

@ -84,6 +84,7 @@ func NewConfig() *Config {
initDirectory(conf)
mode = conf.Sonic.Mode
logMode = conf.Sonic.LogMode
return conf
}
@ -104,7 +105,19 @@ func initDirectory(conf *Config) {
}
var mode string
var logMode LogMode
func IsDev() bool {
return mode == "development"
}
func LogToConsole() bool {
switch logMode {
case Console:
return true
case File:
return false
default:
return IsDev()
}
}

@ -41,9 +41,17 @@ type Levels struct {
Gorm string `mapstructure:"gorm"`
}
type LogMode string
const (
Console LogMode = "console"
File LogMode = "file"
)
type Sonic struct {
Mode string `mapstructure:"mode"`
WorkDir string `mapstructure:"work_dir"`
Mode string `mapstructure:"mode"`
LogMode LogMode `mapstructure:"log_mode"`
WorkDir string `mapstructure:"work_dir"`
UploadDir string
LogDir string `mapstructure:"log_dir"`
TemplateDir string `mapstructure:"template_dir"`

@ -28,7 +28,7 @@ func NewGormLogger(conf *config.Config, zapLogger *zap.Logger) logger.Interface
SlowThreshold: 200 * time.Millisecond,
LogLevel: GetGormLogLevel(conf.Log.Levels.Gorm),
IgnoreRecordNotFoundError: true,
Colorful: config.IsDev(),
Colorful: config.LogToConsole(),
}
gl := &gormLogger{
Config: logConfig,

@ -14,7 +14,7 @@ import (
func NewLogger(conf *config.Config) *zap.Logger {
_, err := os.Stat(conf.Sonic.LogDir)
if err != nil {
if os.IsNotExist(err) && !config.IsDev() {
if os.IsNotExist(err) && !config.LogToConsole() {
err := os.MkdirAll(conf.Sonic.LogDir, os.ModePerm)
if err != nil {
panic("mkdir failed![%v]")
@ -24,7 +24,7 @@ func NewLogger(conf *config.Config) *zap.Logger {
var core zapcore.Core
if config.IsDev() {
if config.LogToConsole() {
core = zapcore.NewCore(getDevEncoder(), os.Stdout, getLogLevel(conf.Log.Levels.App))
} else {
core = zapcore.NewCore(getProdEncoder(), getWriter(conf), zap.DebugLevel)

Loading…
Cancel
Save