mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 00:22:41 +08:00
fix log output
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
*.o
|
*.o
|
||||||
|
fs/logs/**
|
||||||
|
11
dateconv/duration.go
Normal file
11
dateconv/duration.go
Normal 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
10
dateconv/duration_test.go
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package dateconv
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseDuration(t *testing.T) {
|
||||||
|
t.Log(ParseDuration(""))
|
||||||
|
t.Log(ParseDuration("abc"))
|
||||||
|
}
|
13
fs/fs.go
13
fs/fs.go
@ -1,7 +1,9 @@
|
|||||||
package fs
|
package fs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
func IsExist(path string) bool {
|
func IsExist(path string) bool {
|
||||||
@ -17,3 +19,14 @@ func IsDir(path string) bool {
|
|||||||
|
|
||||||
return file.IsDir()
|
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
7
fs/fs_test.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package fs
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestOpenFile(t *testing.T) {
|
||||||
|
OpenOrNew("logs/aaa.log")
|
||||||
|
}
|
@ -8,7 +8,7 @@ import (
|
|||||||
"github.com/json-iterator/go/extra"
|
"github.com/json-iterator/go/extra"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func RegisterFuzzyDecoders() {
|
||||||
extra.RegisterFuzzyDecoders()
|
extra.RegisterFuzzyDecoders()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/charlienet/go-mixed/fs"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"gopkg.in/natefinch/lumberjack.v2"
|
"gopkg.in/natefinch/lumberjack.v2"
|
||||||
)
|
)
|
||||||
@ -68,9 +69,10 @@ func WithOutput(options LogrusOutputOptions) logrusOption {
|
|||||||
return func(l *logrus.Logger) {
|
return func(l *logrus.Logger) {
|
||||||
var writer io.Writer
|
var writer io.Writer
|
||||||
switch {
|
switch {
|
||||||
case options.Output == File, len(options.FileName) > 0:
|
case options.Output == File && len(options.FileName) > 0:
|
||||||
|
// 设置输出为文件,并且已经设置文件名
|
||||||
writer = createFileWriter(options)
|
writer = createFileWriter(options)
|
||||||
case options.Output == Both:
|
case options.Output == Both && len(options.FileName) > 0:
|
||||||
writer = io.MultiWriter(os.Stdout, createFileWriter(options))
|
writer = io.MultiWriter(os.Stdout, createFileWriter(options))
|
||||||
default:
|
default:
|
||||||
writer = os.Stdout
|
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 {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user