Skip to content

Commit

Permalink
feat(log): add zap logger
Browse files Browse the repository at this point in the history
  • Loading branch information
cg33 committed Apr 16, 2020
1 parent 4607943 commit 52d1844
Show file tree
Hide file tree
Showing 18 changed files with 536 additions and 82 deletions.
4 changes: 4 additions & 0 deletions adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ type WebFrameWork interface {
// AddHandler inject the route and handlers of GoAdmin to the web framework.
AddHandler(method, path string, handlers context.Handlers)

DisableLog()

Static(prefix, path string)

// Helper functions
// ================================

Expand Down
3 changes: 3 additions & 0 deletions adapter/beego/beego.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func (bee *Beego) Use(app interface{}, plugs []plugins.Plugin) error {
return bee.GetUse(app, plugs, bee)
}

func (bee *Beego) DisableLog() {}
func (bee *Beego) Static(prefix, path string) {}

// Content implements the method Adapter.Content.
func (bee *Beego) Content(ctx interface{}, getPanelFn types.GetPanelFn, navButtons ...types.Button) {
bee.GetContent(ctx, getPanelFn, bee, navButtons)
Expand Down
3 changes: 3 additions & 0 deletions adapter/buffalo/buffalo.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func (bu *Buffalo) Use(app interface{}, plugs []plugins.Plugin) error {
return bu.GetUse(app, plugs, bu)
}

func (bu *Buffalo) DisableLog() {}
func (bu *Buffalo) Static(prefix, path string) {}

// Content implements the method Adapter.Content.
func (bu *Buffalo) Content(ctx interface{}, getPanelFn types.GetPanelFn, btns ...types.Button) {
bu.GetContent(ctx, getPanelFn, bu, btns)
Expand Down
3 changes: 3 additions & 0 deletions adapter/chi/chi.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func (ch *Chi) Use(app interface{}, plugs []plugins.Plugin) error {
return ch.GetUse(app, plugs, ch)
}

func (ch *Chi) DisableLog() {}
func (ch *Chi) Static(prefix, path string) {}

// Content implements the method Adapter.Content.
func (ch *Chi) Content(ctx interface{}, getPanelFn types.GetPanelFn, btns ...types.Button) {
ch.GetContent(ctx, getPanelFn, ch, btns)
Expand Down
3 changes: 3 additions & 0 deletions adapter/echo/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func (e *Echo) Use(app interface{}, plugs []plugins.Plugin) error {
return e.GetUse(app, plugs, e)
}

func (e *Echo) DisableLog() {}
func (e *Echo) Static(prefix, path string) {}

// Content implements the method Adapter.Content.
func (e *Echo) Content(ctx interface{}, getPanelFn types.GetPanelFn, btns ...types.Button) {
e.GetContent(ctx, getPanelFn, e, btns)
Expand Down
3 changes: 3 additions & 0 deletions adapter/fasthttp/fasthttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ func (fast *Fasthttp) Use(app interface{}, plugs []plugins.Plugin) error {
return fast.GetUse(app, plugs, fast)
}

func (fast *Fasthttp) DisableLog() {}
func (fast *Fasthttp) Static(prefix, path string) {}

// Content implements the method Adapter.Content.
func (fast *Fasthttp) Content(ctx interface{}, getPanelFn types.GetPanelFn, btns ...types.Button) {
fast.GetContent(ctx, getPanelFn, fast, btns)
Expand Down
3 changes: 3 additions & 0 deletions adapter/gf/gf.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func (gf *Gf) Use(app interface{}, plugs []plugins.Plugin) error {
return gf.GetUse(app, plugs, gf)
}

func (gf *Gf) DisableLog() {}
func (gf *Gf) Static(prefix, path string) {}

// Content implements the method Adapter.Content.
func (gf *Gf) Content(ctx interface{}, getPanelFn types.GetPanelFn, btns ...types.Button) {
gf.GetContent(ctx, getPanelFn, gf, btns)
Expand Down
3 changes: 3 additions & 0 deletions adapter/gin/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ func Content(handler HandlerFunc) gin.HandlerFunc {
}
}

func (gins *Gin) DisableLog() {}
func (gins *Gin) Static(prefix, path string) {}

// SetApp implements the method Adapter.SetApp.
func (gins *Gin) SetApp(app interface{}) error {
var (
Expand Down
3 changes: 3 additions & 0 deletions adapter/gorilla/gorilla.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func (g *Gorilla) Use(app interface{}, plugs []plugins.Plugin) error {
return g.GetUse(app, plugs, g)
}

func (g *Gorilla) DisableLog() {}
func (g *Gorilla) Static(prefix, path string) {}

// Content implements the method Adapter.Content.
func (g *Gorilla) Content(ctx interface{}, getPanelFn types.GetPanelFn, btns ...types.Button) {
g.GetContent(ctx, getPanelFn, g, btns)
Expand Down
3 changes: 3 additions & 0 deletions adapter/iris/iris.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func (is *Iris) Use(app interface{}, plugs []plugins.Plugin) error {
return is.GetUse(app, plugs, is)
}

func (is *Iris) DisableLog() {}
func (is *Iris) Static(prefix, path string) {}

// Content implements the method Adapter.Content.
func (is *Iris) Content(ctx interface{}, getPanelFn types.GetPanelFn, btns ...types.Button) {
is.GetContent(ctx, getPanelFn, is, btns)
Expand Down
6 changes: 4 additions & 2 deletions examples/gin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import (
)

func main() {
r := gin.Default()

gin.SetMode(gin.ReleaseMode)
gin.DefaultWriter = ioutil.Discard

r := gin.New()

e := engine.Default()

cfg := config.Config{
Expand All @@ -54,6 +54,8 @@ func main() {
Language: language.CN,
IndexUrl: "/",
Debug: true,

AccessAssetsLogOff: true,
Animation: config.PageAnimation{
Type: "fadeInUp",
},
Expand Down
141 changes: 116 additions & 25 deletions modules/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,18 @@ type Config struct {
// Access log path.
AccessLogPath string `json:"access_log",yaml:"access_log",ini:"access_log"`

// Access assets log off
AccessAssetsLogOff bool `json:"access_assets_log_off",yaml:"access_assets_log_off",ini:"access_assets_log_off"`

// Sql operator record log switch.
SqlLog bool `json:"sql_log",yaml:"sql_log",ini:"sql_log"`

AccessLogOff bool `json:"access_log_off",yaml:"access_log_off",ini:"access_log_off"`
InfoLogOff bool `json:"info_log_off",yaml:"info_log_off",ini:"info_log_off"`
ErrorLogOff bool `json:"error_log_off",yaml:"error_log_off",ini:"error_log_off"`

Logger Logger `json:"logger",yaml:"logger",ini:"logger"`

// Color scheme.
ColorScheme string `json:"color_scheme",yaml:"color_scheme",ini:"color_scheme"`

Expand Down Expand Up @@ -270,6 +275,32 @@ type Config struct {
prefix string
}

type Logger struct {
Encoder EncoderCfg `json:"encoder",yaml:"encoder",ini:"encoder"`
Rotate RotateCfg `json:"rotate",yaml:"rotate",ini:"rotate"`
}

type EncoderCfg struct {
TimeKey string `json:"time_key",yaml:"time_key",ini:"time_key"`
LevelKey string `json:"level_key",yaml:"level_key",ini:"level_key"`
NameKey string `json:"name_key",yaml:"name_key",ini:"name_key"`
CallerKey string `json:"caller_key",yaml:"caller_key",ini:"caller_key"`
MessageKey string `json:"message_key",yaml:"message_key",ini:"message_key"`
StacktraceKey string `json:"stacktrace_key",yaml:"stacktrace_key",ini:"stacktrace_key"`
Level string `json:"level",yaml:"level",ini:"level"`
Time string `json:"time",yaml:"time",ini:"time"`
Duration string `json:"duration",yaml:"duration",ini:"duration"`
Caller string `json:"caller",yaml:"caller",ini:"caller"`
Encoding string `json:"encoding",yaml:"encoding",ini:"encoding"`
}

type RotateCfg struct {
MaxSize int `json:"max_size",yaml:"max_size",ini:"max_size"`
MaxBackups int `json:"max_backups",yaml:"max_backups",ini:"max_backups"`
MaxAge int `json:"max_age",yaml:"max_age",ini:"max_age"`
Compress bool `json:"compress",yaml:"compress",ini:"compress"`
}

type ExtraInfo map[string]interface{}

type UpdateConfigProcessFn func(values form.Values) (form.Values, error)
Expand Down Expand Up @@ -444,6 +475,7 @@ func (c *Config) Copy() *Config {
Extra: c.Extra,
Animation: c.Animation,
NoLimitLoginIP: c.NoLimitLoginIP,
Logger: c.Logger,
prefix: c.prefix,
}
}
Expand All @@ -464,13 +496,35 @@ func (c *Config) ToMap() map[string]string {
m["login_url"] = c.LoginUrl
m["debug"] = strconv.FormatBool(c.Debug)
m["env"] = c.Env

// Logger config
// ========================

m["info_log_path"] = c.InfoLogPath
m["error_log_path"] = c.ErrorLogPath
m["access_log_path"] = c.AccessLogPath
m["sql_log"] = strconv.FormatBool(c.SqlLog)
m["access_log_off"] = strconv.FormatBool(c.AccessLogOff)
m["info_log_off"] = strconv.FormatBool(c.InfoLogOff)
m["error_log_off"] = strconv.FormatBool(c.ErrorLogOff)

m["logger_rotate_max_size"] = strconv.Itoa(c.Logger.Rotate.MaxSize)
m["logger_rotate_max_backups"] = strconv.Itoa(c.Logger.Rotate.MaxBackups)
m["logger_rotate_max_age"] = strconv.Itoa(c.Logger.Rotate.MaxAge)
m["logger_rotate_compress"] = strconv.FormatBool(c.Logger.Rotate.Compress)

m["logger_encoder_time_key"] = c.Logger.Encoder.TimeKey
m["logger_encoder_level_key"] = c.Logger.Encoder.LevelKey
m["logger_encoder_name_key"] = c.Logger.Encoder.NameKey
m["logger_encoder_caller_key"] = c.Logger.Encoder.CallerKey
m["logger_encoder_message_key"] = c.Logger.Encoder.MessageKey
m["logger_encoder_stacktrace_key"] = c.Logger.Encoder.StacktraceKey
m["logger_encoder_level"] = c.Logger.Encoder.Level
m["logger_encoder_time"] = c.Logger.Encoder.Time
m["logger_encoder_duration"] = c.Logger.Encoder.Duration
m["logger_encoder_caller"] = c.Logger.Encoder.Caller
m["logger_encoder_encoding"] = c.Logger.Encoder.Encoding

m["color_scheme"] = c.ColorScheme
m["session_life_time"] = strconv.Itoa(c.SessionLifeTime)
m["asset_url"] = c.AssetUrl
Expand Down Expand Up @@ -510,16 +564,33 @@ func (c *Config) Update(m map[string]string) error {

if c.InfoLogPath != m["info_log_path"] {
c.InfoLogPath = m["info_log_path"]
logger.SetInfoLogger(c.InfoLogPath, c.Debug, c.InfoLogOff)
logger.SetInfoLogger(c.InfoLogPath, c.InfoLogOff)
}
if c.ErrorLogPath != m["error_log_path"] {
c.ErrorLogPath = m["error_log_path"]
logger.SetErrorLogger(c.ErrorLogPath, c.Debug, c.ErrorLogOff)
logger.SetErrorLogger(c.ErrorLogPath, c.ErrorLogOff)
}
if c.AccessLogPath != m["access_log_path"] {
c.AccessLogPath = m["access_log_path"]
logger.SetAccessLogger(c.AccessLogPath, c.Debug, c.AccessLogOff)
}
logger.SetAccessLogger(c.AccessLogPath, c.AccessLogOff)
}

c.Logger.Rotate.MaxSize, _ = strconv.Atoi(m["logger_rotate_max_size"])
c.Logger.Rotate.MaxBackups, _ = strconv.Atoi(m["logger_rotate_max_backups"])
c.Logger.Rotate.MaxAge, _ = strconv.Atoi(m["logger_rotate_max_age"])
c.Logger.Rotate.Compress = utils.ParseBool(m["logger_rotate_compress"])

c.Logger.Encoder.TimeKey = m["logger_encoder_time_key"]
c.Logger.Encoder.LevelKey = m["logger_encoder_level_key"]
c.Logger.Encoder.NameKey = m["logger_encoder_name_key"]
c.Logger.Encoder.CallerKey = m["logger_encoder_caller_key"]
c.Logger.Encoder.MessageKey = m["logger_encoder_message_key"]
c.Logger.Encoder.StacktraceKey = m["logger_encoder_stacktrace_key"]
c.Logger.Encoder.Level = m["logger_encoder_level"]
c.Logger.Encoder.Time = m["logger_encoder_time"]
c.Logger.Encoder.Duration = m["logger_encoder_duration"]
c.Logger.Encoder.Caller = m["logger_encoder_caller"]
c.Logger.Encoder.Encoding = m["logger_encoder_encoding"]

c.SqlLog = utils.ParseBool(m["sql_log"])
if c.Theme == "adminlte" {
Expand Down Expand Up @@ -637,17 +708,17 @@ func Set(cfg Config) *Config {
}
atomic.StoreUint32(&count, 1)

cfg.Title = setDefault(cfg.Title, "", "GoAdmin")
cfg.LoginTitle = setDefault(cfg.LoginTitle, "", "GoAdmin")
cfg.Logo = template.HTML(setDefault(string(cfg.Logo), "", "<b>Go</b>Admin"))
cfg.MiniLogo = template.HTML(setDefault(string(cfg.MiniLogo), "", "<b>G</b>A"))
cfg.Theme = setDefault(cfg.Theme, "", "adminlte")
cfg.IndexUrl = setDefault(cfg.IndexUrl, "", "/info/manager")
cfg.LoginUrl = setDefault(cfg.LoginUrl, "", "/login")
cfg.AuthUserTable = setDefault(cfg.AuthUserTable, "", "goadmin_users")
cfg.ColorScheme = setDefault(cfg.ColorScheme, "", "skin-black")
cfg.FileUploadEngine.Name = setDefault(cfg.FileUploadEngine.Name, "", "local")
cfg.Env = setDefault(cfg.Env, "", EnvProd)
cfg.Title = utils.SetDefault(cfg.Title, "", "GoAdmin")
cfg.LoginTitle = utils.SetDefault(cfg.LoginTitle, "", "GoAdmin")
cfg.Logo = template.HTML(utils.SetDefault(string(cfg.Logo), "", "<b>Go</b>Admin"))
cfg.MiniLogo = template.HTML(utils.SetDefault(string(cfg.MiniLogo), "", "<b>G</b>A"))
cfg.Theme = utils.SetDefault(cfg.Theme, "", "adminlte")
cfg.IndexUrl = utils.SetDefault(cfg.IndexUrl, "", "/info/manager")
cfg.LoginUrl = utils.SetDefault(cfg.LoginUrl, "", "/login")
cfg.AuthUserTable = utils.SetDefault(cfg.AuthUserTable, "", "goadmin_users")
cfg.ColorScheme = utils.SetDefault(cfg.ColorScheme, "", "skin-black")
cfg.FileUploadEngine.Name = utils.SetDefault(cfg.FileUploadEngine.Name, "", "local")
cfg.Env = utils.SetDefault(cfg.Env, "", EnvProd)
if cfg.SessionLifeTime == 0 {
// default two hours
cfg.SessionLifeTime = 7200
Expand All @@ -661,9 +732,36 @@ func Set(cfg Config) *Config {
cfg.prefix = cfg.UrlPrefix
}

logger.SetInfoLogger(cfg.InfoLogPath, cfg.Debug, cfg.InfoLogOff)
logger.SetErrorLogger(cfg.ErrorLogPath, cfg.Debug, cfg.ErrorLogOff)
logger.SetAccessLogger(cfg.AccessLogPath, cfg.Debug, cfg.AccessLogOff)
logger.InitWithConfig(logger.Config{
InfoLogOff: cfg.InfoLogOff,
ErrorLogOff: cfg.ErrorLogOff,
AccessLogOff: cfg.AccessLogOff,
SqlLogOpen: cfg.SqlLog,
InfoLogPath: cfg.InfoLogPath,
ErrorLogPath: cfg.ErrorLogPath,
AccessLogPath: cfg.AccessLogPath,
AccessAssetsLogOff: cfg.AccessAssetsLogOff,
Rotate: logger.RotateCfg{
MaxSize: cfg.Logger.Rotate.MaxSize,
MaxBackups: cfg.Logger.Rotate.MaxBackups,
MaxAge: cfg.Logger.Rotate.MaxAge,
Compress: cfg.Logger.Rotate.Compress,
},
Encode: logger.EncoderCfg{
TimeKey: cfg.Logger.Encoder.TimeKey,
LevelKey: cfg.Logger.Encoder.LevelKey,
NameKey: cfg.Logger.Encoder.NameKey,
CallerKey: cfg.Logger.Encoder.CallerKey,
MessageKey: cfg.Logger.Encoder.MessageKey,
StacktraceKey: cfg.Logger.Encoder.StacktraceKey,
Level: cfg.Logger.Encoder.Level,
Time: cfg.Logger.Encoder.Time,
Duration: cfg.Logger.Encoder.Duration,
Caller: cfg.Logger.Encoder.Caller,
Encoding: cfg.Logger.Encoder.Encoding,
},
Debug: cfg.Debug,
})

if cfg.SqlLog {
logger.OpenSQLLog()
Expand Down Expand Up @@ -722,13 +820,6 @@ func Get() Config {
return *c
}

func setDefault(value, condition, def string) string {
if value == condition {
return def
}
return value
}

// Getter methods
// ============================

Expand Down
19 changes: 19 additions & 0 deletions modules/language/cn.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ var cn = LangSet{
"config.info log off": "关闭信息日志",
"config.error log off": "关闭错误日志",
"config.access log off": "关闭访问日志",
"config.sql log on": "打开sql日志",
"config.general": "通用",
"config.log": "日志",
"config.site setting": "网站设置",
Expand All @@ -158,6 +159,24 @@ var cn = LangSet{
"config.animation": "动画",
"config.file upload engine": "文件上传引擎",

"config.logger rotate": "日志切割设置",
"config.logger rotate max size": "存储最大文件大小(m)",
"config.logger rotate max backups": "存储最多文件数",
"config.logger rotate max age": "最长存储时间(天)",
"config.logger rotate compress": "压缩",

"config.logger rotate encoder time key": "Time Key",
"config.logger rotate encoder level key": "Level Key",
"config.logger rotate encoder name key": "Name Key",
"config.logger rotate encoder caller key": "Caller Key",
"config.logger rotate encoder message key": "Message Key",
"config.logger rotate encoder stacktrace key": "Stacktrace Key",
"config.logger rotate encoder level": "Level",
"config.logger rotate encoder time": "Time",
"config.logger rotate encoder duration": "Duration",
"config.logger rotate encoder caller": "Caller",
"config.logger rotate encoder encoding": "Encoding",

"config.do not modify when you have not set up all assets": "不要修改,当你还没有设置好所有资源文件的时候",
"config.it will work when theme is adminlte": "当主题为adminlte时生效",

Expand Down
Loading

0 comments on commit 52d1844

Please sign in to comment.