fixme: 优化初次启动日志文件创建
This commit is contained in:
1 parent
17b850d0a8
commit
2399824d35
2 files changed
+20
-2
No files matched your search
+10
-2
@@ -1,6 +1,7 @@
|
|||||||
package logger
|
package logger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"go-admin/tools"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/go-admin-team/go-admin-core/debug/writer"
|
"github.com/go-admin-team/go-admin-core/debug/writer"
|
||||||
@@ -39,9 +40,16 @@ import (
|
|||||||
// SetupLogger 日志
|
// SetupLogger 日志
|
||||||
func SetupLogger(path string, subPath string) logger.Logger {
|
func SetupLogger(path string, subPath string) logger.Logger {
|
||||||
var setLogger 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 {
|
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)))
|
setLogger = logger.NewHelper(logger.NewLogger(logger.WithOutput(output)))
|
||||||
return setLogger
|
return setLogger
|
||||||
|
|||||||
@@ -18,6 +18,16 @@ func PathCreate(dir string) error {
|
|||||||
return os.MkdirAll(dir, os.ModePerm)
|
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) {
|
func FileCreate(content bytes.Buffer, name string) {
|
||||||
file, err := os.Create(name)
|
file, err := os.Create(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in new issue
Block a user