1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 08:32:40 +08:00

fix log output

This commit is contained in:
2022-06-17 15:25:35 +08:00
parent d12c947226
commit 1eeed34adf
7 changed files with 48 additions and 4 deletions

View File

@ -1,7 +1,9 @@
package fs
import (
"io"
"os"
"path/filepath"
)
func IsExist(path string) bool {
@ -17,3 +19,14 @@ func IsDir(path string) bool {
return file.IsDir()
}
// 打开或新建文件,目录不存在时创建目录
func OpenOrNew(filename string) (io.Writer, error) {
dir := filepath.Dir(filename)
if !IsExist(dir) {
os.MkdirAll(dir, 0744)
}
mode := os.FileMode(0644)
return os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, mode)
}

7
fs/fs_test.go Normal file
View File

@ -0,0 +1,7 @@
package fs
import "testing"
func TestOpenFile(t *testing.T) {
OpenOrNew("logs/aaa.log")
}