-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
92c2619
commit e2ef0f5
Showing
8 changed files
with
184 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package rotatelogs | ||
|
||
import ( | ||
"github.com/lestrrat/go-file-rotatelogs" | ||
"io" | ||
"time" | ||
) | ||
|
||
func GetWriter(logLocation string, rotationTime time.Duration) (io.Writer, func() error){ | ||
writer, _ := rotatelogs.New( | ||
logLocation, | ||
rotatelogs.WithRotationTime(rotationTime), | ||
) | ||
return writer, writer.Close | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package log | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func GenerateFileNameLog(pathFileNameFormat string) string { | ||
path := pathFileNameFormat | ||
if strings.Contains(pathFileNameFormat, "$") { | ||
s := getLayoutsFormat(pathFileNameFormat) | ||
if len(s) > 1 { | ||
timeFormat := time.Now().Format(s[1]) | ||
path = strings.ReplaceAll(pathFileNameFormat, s[0], timeFormat) | ||
} | ||
} | ||
CreatePath(path) | ||
return path | ||
} | ||
|
||
func CreatePath(path string) error { | ||
dir := filepath.Dir(path) | ||
if _, err := os.Stat(dir); errors.Is(err, os.ErrNotExist) { | ||
err2 := os.Mkdir(dir, os.ModePerm) | ||
if err2 != nil { | ||
return err2 | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func getLayoutsFormat(s string) []string { | ||
re := regexp.MustCompile("\\$\\{(.*?)\\}") | ||
return re.FindStringSubmatch(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package log | ||
|
||
import ( | ||
"errors" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func NewWriter(ws zapcore.WriteSyncer, conf zap.Config) zap.Option { | ||
var enc zapcore.Encoder | ||
switch conf.Encoding { | ||
case "json": | ||
enc = zapcore.NewJSONEncoder(conf.EncoderConfig) | ||
case "console": | ||
enc = zapcore.NewConsoleEncoder(conf.EncoderConfig) | ||
default: | ||
panic("unknown encoding") | ||
} | ||
return zap.WrapCore(func(core zapcore.Core) zapcore.Core { | ||
return zapcore.NewCore(enc, ws, conf.Level) | ||
}) | ||
} | ||
|
||
func GetWriteSyncer(path string) zapcore.WriteSyncer { | ||
dir := filepath.Dir(path) | ||
if _, err := os.Stat(dir); errors.Is(err, os.ErrNotExist) { | ||
err2 := os.Mkdir(dir, os.ModePerm) | ||
if err2 != nil { | ||
panic(err2) | ||
} | ||
} | ||
file, err := os.OpenFile(path, os.O_RDWR|os.O_APPEND|os.O_CREATE, 644) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return zapcore.AddSync(file) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package log | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func GenerateFileNameLog(pathFileNameFormat string) string { | ||
path := pathFileNameFormat | ||
if strings.Contains(pathFileNameFormat, "$") { | ||
s := getLayoutsFormat(pathFileNameFormat) | ||
if len(s) > 1 { | ||
timeFormat := time.Now().Format(s[1]) | ||
path = strings.ReplaceAll(pathFileNameFormat, s[0], timeFormat) | ||
} | ||
} | ||
CreatePath(path) | ||
return path | ||
} | ||
|
||
func CreatePath(path string) error { | ||
dir := filepath.Dir(path) | ||
if _, err := os.Stat(dir); errors.Is(err, os.ErrNotExist) { | ||
err2 := os.Mkdir(dir, os.ModePerm) | ||
if err2 != nil { | ||
return err2 | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func getLayoutsFormat(s string) []string { | ||
re := regexp.MustCompile("\\$\\{(.*?)\\}") | ||
return re.FindStringSubmatch(s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters