1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-17 16:12:42 +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

1
.gitignore vendored
View File

@ -1 +1,2 @@
*.o
fs/logs/**

11
dateconv/duration.go Normal file
View File

@ -0,0 +1,11 @@
package dateconv
import "time"
func ParseDuration(s string) (time.Duration, error) {
if len(s) == 0 {
return time.Duration(0), nil
}
return time.ParseDuration(s)
}

10
dateconv/duration_test.go Normal file
View File

@ -0,0 +1,10 @@
package dateconv
import (
"testing"
)
func TestParseDuration(t *testing.T) {
t.Log(ParseDuration(""))
t.Log(ParseDuration("abc"))
}

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")
}

View File

@ -8,7 +8,7 @@ import (
"github.com/json-iterator/go/extra"
)
func init() {
func RegisterFuzzyDecoders() {
extra.RegisterFuzzyDecoders()
}

View File

@ -5,6 +5,7 @@ import (
"log"
"os"
"github.com/charlienet/go-mixed/fs"
"github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
)
@ -68,9 +69,10 @@ func WithOutput(options LogrusOutputOptions) logrusOption {
return func(l *logrus.Logger) {
var writer io.Writer
switch {
case options.Output == File, len(options.FileName) > 0:
case options.Output == File && len(options.FileName) > 0:
// 设置输出为文件,并且已经设置文件名
writer = createFileWriter(options)
case options.Output == Both:
case options.Output == Both && len(options.FileName) > 0:
writer = io.MultiWriter(os.Stdout, createFileWriter(options))
default:
writer = os.Stdout
@ -92,7 +94,7 @@ func createFileWriter(options LogrusOutputOptions) io.Writer {
}
}
f, err := os.OpenFile(options.FileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
f, err := fs.OpenOrNew(options.FileName)
if err != nil {
log.Panic(err)
}