Update code comments, reformat

This commit is contained in:
jar3b 2019-02-23 03:06:30 +03:00
parent 8e85f03035
commit 55124a4a8c

View File

@ -30,7 +30,7 @@ type Formatter struct {
// Also can include custom fields but limited to strings. // Also can include custom fields but limited to strings.
// All of fields need to be wrapped inside %% i.e %time% %msg% // All of fields need to be wrapped inside %% i.e %time% %msg%
LogFormat string LogFormat string
LevelPad int LevelPad int
} }
// Format building log message. // Format building log message.
@ -40,21 +40,22 @@ func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) {
output = defaultLogFormat output = defaultLogFormat
} }
timestampFormat := f.TimestampFormat if f.TimestampFormat == "" {
if timestampFormat == "" { f.TimestampFormat = defaultTimestampFormat
timestampFormat = defaultTimestampFormat
} }
output = strings.Replace(output, "%time%", entry.Time.Format(timestampFormat), 1) // replace timestamp
output = strings.Replace(output, "%time%", entry.Time.Format(f.TimestampFormat), 1)
// replace message text
output = strings.Replace(output, "%msg%", entry.Message, 1) output = strings.Replace(output, "%msg%", entry.Message, 1)
// replace level
level := strings.ToUpper(entry.Level.String()) level := strings.ToUpper(entry.Level.String())
if f.LevelPad != 0 { if f.LevelPad != 0 {
level = padRight(level, " ", f.LevelPad) level = padRight(level, " ", f.LevelPad)
} }
output = strings.Replace(output, "%lvl%", level, 1) output = strings.Replace(output, "%lvl%", level, 1)
// replace data
for k, v := range entry.Data { for k, v := range entry.Data {
if s, ok := v.(string); ok { if s, ok := v.(string); ok {
output = strings.Replace(output, "%"+k+"%", s, 1) output = strings.Replace(output, "%"+k+"%", s, 1)