fixme: 优化初次启动日志文件创建

This commit is contained in:
linwenxiang committed 2020-11-04 11:26:41 +08:00
1 parent 17b850d0a8
commit 2399824d35
2 files changed
+20 -2

No files matched your search

+10 -2
View File
@@ -1,6 +1,7 @@
package logger
import (
"go-admin/tools"
"path/filepath"
"github.com/go-admin-team/go-admin-core/debug/writer"
@@ -39,9 +40,16 @@ import (
// SetupLogger 日志
func SetupLogger(path string, subPath string) logger.Logger {
var setLogger logger.Logger
output, err := writer.NewFileWriter(filepath.Join(path, subPath), "log")
fullPath := filepath.Join(path, subPath)
if !tools.PathExist(fullPath) {
err := tools.PathCreate(fullPath)
if err != nil {
log.Fatal("create dir error: %s", err.Error())
}
}
output, err := writer.NewFileWriter(fullPath, "log")
if err != nil {
log.Fatal("request logger setup error: %s", err.Error)
log.Fatal("%s logger setup error: %s", subPath, err.Error())
}
setLogger = logger.NewHelper(logger.NewLogger(logger.WithOutput(output)))
return setLogger
+10
View File
@@ -18,6 +18,16 @@ func PathCreate(dir string) error {
return os.MkdirAll(dir, os.ModePerm)
}
// PathExist 判断目录是否存在
func PathExist(addr string) bool {
s, err := os.Stat(addr)
if err != nil {
log.Println(err)
return false
}
return s.IsDir()
}
func FileCreate(content bytes.Buffer, name string) {
file, err := os.Create(name)
if err != nil {