github.com/woremacx/kocha@v0.7.1-0.20150731103243-a5889322afc9/log/formatter.go (about) 1 package log 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "time" 8 ) 9 10 // Formatter is an interface that formatter for a log entry. 11 type Formatter interface { 12 // Format formats a log entry. 13 // Format writes formatted entry to the w. 14 Format(w io.Writer, entry *Entry) error 15 } 16 17 // RawFormatter is a formatter that doesn't format. 18 // RawFormatter doesn't output the almost fields of the entry except the 19 // Message. 20 type RawFormatter struct{} 21 22 // Format outputs entry.Message. 23 func (f *RawFormatter) Format(w io.Writer, entry *Entry) error { 24 _, err := io.WriteString(w, entry.Message) 25 return err 26 } 27 28 // LTSVFormatter is the formatter of Labeled Tab-separated Values. 29 // See http://ltsv.org/ for more details. 30 type LTSVFormatter struct { 31 } 32 33 // Format formats an entry to Labeled Tab-separated Values format. 34 func (f *LTSVFormatter) Format(w io.Writer, entry *Entry) error { 35 var buf bytes.Buffer 36 fmt.Fprintf(&buf, "level:%v", entry.Level) 37 if !entry.Time.IsZero() { 38 fmt.Fprintf(&buf, "\ttime:%v", entry.Time.Format(time.RFC3339Nano)) 39 } 40 if entry.Message != "" { 41 fmt.Fprintf(&buf, "\tmessage:%v", entry.Message) 42 } 43 for _, k := range entry.Fields.OrderedKeys() { 44 fmt.Fprintf(&buf, "\t%v:%v", k, entry.Fields.Get(k)) 45 } 46 _, err := io.Copy(w, &buf) 47 return err 48 }