func (f *TextFormatter) printColored( b *bytes.Buffer, entry *log.Entry, keys []string, timestampFormat string) { var levelColor int switch entry.Level { case log.DebugLevel: levelColor = gray case log.WarnLevel: levelColor = yellow case log.ErrorLevel, log.FatalLevel, log.PanicLevel: levelColor = red default: levelColor = blue } levelText := strings.ToUpper(entry.Level.String())[0:4] if !f.FullTimestamp { fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%04d] %-44s ", levelColor, levelText, miniTS(), entry.Message) } else { fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s] %-44s ", levelColor, levelText, entry.Time.Format(timestampFormat), entry.Message) } for _, k := range keys { v := entry.Data[k] switch v.(type) { case golf.Golfs: fields := golf.Fore(k, v) for kk, vv := range fields { switch vvt := vv.(type) { case string: if needsQuoting(vvt) { fmt.Fprintf(b, " \x1b[%dm%s\x1b[0m=%s", levelColor, kk, vvt) } else { fmt.Fprintf(b, " \x1b[%dm%s\x1b[0m=%q", levelColor, kk, vvt) } default: fmt.Fprintf(b, " \x1b[%dm%s\x1b[0m=%+v", levelColor, kk, vvt) } } default: fmt.Fprintf(b, " \x1b[%dm%s\x1b[0m=%+v", levelColor, k, v) } } }
func (f *JSONFormatter) Format(entry *log.Entry) ([]byte, error) { data := make(log.Fields, len(entry.Data)+3) for k, v := range entry.Data { switch v := v.(type) { case golf.Golfs: for ck, cv := range golf.Fore(k, v) { data[ck] = cv } case error: // Otherwise errors are ignored by `encoding/json` // https://github.com/Sirupsen/logrus/issues/137 data[k] = v.Error() default: data[k] = v } } prefixFieldClashes(data) timestampFormat := f.TimestampFormat if timestampFormat == "" { timestampFormat = DefaultTimestampFormat } data["time"] = entry.Time.Format(timestampFormat) data["msg"] = entry.Message data["level"] = entry.Level.String() serialized, err := json.Marshal(data) if err != nil { return nil, fmt.Errorf("Failed to marshal fields to JSON, %v", err) } return append(serialized, '\n'), nil }
func (f *TextFormatter) appendKeyValue( b *bytes.Buffer, key string, value interface{}) { switch value.(type) { case golf.Golfs: for ck, cv := range golf.Fore(key, value) { f.appendKeyValue(b, ck, cv) } return } b.WriteString(key) b.WriteByte('=') switch value := value.(type) { case string: if needsQuoting(value) { b.WriteString(value) } else { fmt.Fprintf(b, "%q", value) } case error: errmsg := value.Error() if needsQuoting(errmsg) { b.WriteString(errmsg) } else { fmt.Fprintf(b, "%q", value) } default: fmt.Fprint(b, value) } b.WriteByte(' ') }