diff --git a/backend/init/log/log.go b/backend/init/log/log.go index 053302e3d..132a89070 100644 --- a/backend/init/log/log.go +++ b/backend/init/log/log.go @@ -3,6 +3,8 @@ package log import ( "fmt" "github.com/1Panel-dev/1Panel/backend/log" + "io" + "os" "strings" "time" @@ -12,20 +14,26 @@ import ( "github.com/sirupsen/logrus" ) +const ( + TimeFormat = "2006-01-02 15:04:05" + RollingTimePattern = "0 0 * * *" +) + func Init() { l := logrus.New() setOutput(l, global.CONF.LogConfig) global.LOG = l - global.LOG.Info("init success") + global.LOG.Info("init logger successfully") } func setOutput(logger *logrus.Logger, config configs.LogConfig) { writer, err := log.NewWriterFromConfig(&log.Config{ - LogPath: config.Path, - FileName: config.LogName, - TimeTagFormat: "2006-01-02-15-04-05", - MaxRemain: config.LogBackup, + LogPath: config.Path, + FileName: config.LogName, + TimeTagFormat: TimeFormat, + MaxRemain: config.LogBackup, + RollingTimePattern: RollingTimePattern, }) if err != nil { panic(err) @@ -34,15 +42,15 @@ func setOutput(logger *logrus.Logger, config configs.LogConfig) { if err != nil { panic(err) } - logger.SetOutput(writer) + fileAndStdoutWriter := io.MultiWriter(writer, os.Stdout) + + logger.SetOutput(fileAndStdoutWriter) logger.SetLevel(level) logger.SetFormatter(new(MineFormatter)) } type MineFormatter struct{} -const TimeFormat = "2006-01-02 15:04:05" - func (s *MineFormatter) Format(entry *logrus.Entry) ([]byte, error) { var cstSh, _ = time.LoadLocation(global.CONF.LogConfig.TimeZone) detailInfo := "" diff --git a/backend/init/router/router.go b/backend/init/router/router.go index 159ac3df7..b3bd4e45f 100644 --- a/backend/init/router/router.go +++ b/backend/init/router/router.go @@ -38,8 +38,8 @@ func setWebStatic(rootRouter *gin.Engine) { func Routers() *gin.Engine { Router := gin.Default() - //Router.Use(middleware.CSRF()) - //Router.Use(middleware.LoadCsrfToken()) + Router.Use(middleware.CSRF()) + Router.Use(middleware.LoadCsrfToken()) setWebStatic(Router) docs.SwaggerInfo.BasePath = "/api/v1" @@ -64,7 +64,7 @@ func Routers() *gin.Engine { } PrivateGroup := Router.Group("/api/v1") - // PrivateGroup.Use(middleware.SafetyAuth()) + //PrivateGroup.Use(middleware.SafetyAuth()) { systemRouter.InitBaseRouter(PrivateGroup) systemRouter.InitDashboardRouter(PrivateGroup) diff --git a/backend/log/config.go b/backend/log/config.go index 22a2e5508..438840bb2 100644 --- a/backend/log/config.go +++ b/backend/log/config.go @@ -7,10 +7,6 @@ import ( "path" ) -const ( - RollingTimePattern = "0 0 * * *" -) - var ( BufferSize = 0x100000 DefaultFileMode = os.FileMode(0644) @@ -21,10 +17,11 @@ var ( ) type Config struct { - TimeTagFormat string - LogPath string - FileName string - MaxRemain int + TimeTagFormat string + LogPath string + FileName string + MaxRemain int + RollingTimePattern string } type Manager interface { diff --git a/backend/log/manager.go b/backend/log/manager.go index b03f1b51f..e451084d4 100644 --- a/backend/log/manager.go +++ b/backend/log/manager.go @@ -35,7 +35,7 @@ func NewManager(c *Config) (Manager, error) { wg: sync.WaitGroup{}, } - if _, err := m.cr.AddFunc(RollingTimePattern, func() { + if _, err := m.cr.AddFunc(c.RollingTimePattern, func() { m.fire <- m.GenLogFileName(c) }); err != nil { return nil, err