Skip to content

Commit 46997d6

Browse files
authored
Merge pull request #7 from helmwave/strings-replacer
Use strings.Replacer
2 parents b749d16 + f79dd0b commit 46997d6

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
repos:
2+
- repo: https://github.com/dnephin/pre-commit-golang
3+
rev: v0.4.0
4+
hooks:
5+
- id: go-fmt
6+
- id: golangci-lint
7+
- id: go-mod-tidy

func.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ const (
2121

2222
// Format building log message.
2323
func (f *Config) Format(entry *logrus.Entry) ([]byte, error) {
24-
output := f.LogFormat
25-
if output == "" {
26-
output = defaultLogFormat
24+
format := f.LogFormat
25+
if format == "" {
26+
format = defaultLogFormat
2727
}
2828

2929
timestampFormat := f.TimestampFormat
@@ -45,10 +45,14 @@ func (f *Config) Format(entry *logrus.Entry) ([]byte, error) {
4545
fieldPattern = start + logFieldColor + fieldPattern + end
4646
}
4747

48-
output = strings.Replace(output, "%time%", entry.Time.Format(timestampFormat), 1)
49-
output = strings.Replace(output, "%msg%", m, 1)
50-
output = strings.Replace(output, "%lvl%", l, 1)
51-
output = strings.Replace(output, "%emoji%", emoji, 1)
48+
replacer := strings.NewReplacer(
49+
"%time%", entry.Time.Format(timestampFormat),
50+
"%msg%", m,
51+
"%lvl%", l,
52+
"%emoji%", emoji,
53+
)
54+
55+
output := replacer.Replace(format)
5256

5357
for k, val := range entry.Data {
5458
switch val := val.(type) {

0 commit comments

Comments
 (0)