14 timeFormat = "2006-01-02T15:04:05-0700"
15 termTimeFormat = "2006/01/02 15:04:05"
16 termSmallTimeFormat = "15:04:05"
18 errorKey = "REVEL_ERROR"
22 levelString = map[LogLevel]string{LvlDebug: "DEBUG",
23 LvlInfo: "INFO", LvlWarn: "WARN", LvlError: "ERROR", LvlCrit: "CRIT"}
26 // Outputs to the terminal in a format like below
27 // INFO 09:11:32 server-engine.go:169: Request Stats
28 func TerminalFormatHandler(noColor bool, smallDate bool) LogFormat {
29 dateFormat := termTimeFormat
31 dateFormat = termSmallTimeFormat
33 return FormatFunc(func(r *Record) []byte {
34 // Bash coloring http://misc.flogisoft.com/bash/tip_colors_and_formatting
55 caller, _ := r.Context["caller"].(string)
56 module, _ := r.Context["module"].(string)
57 if noColor == false && color > 0 {
59 fmt.Fprintf(b, "\x1b[%dm%-5s\x1b[0m %s %6s %13s: %-40s ", color, levelString[r.Level], r.Time.Format(dateFormat), module, caller, r.Message)
61 fmt.Fprintf(b, "\x1b[%dm%-5s\x1b[0m %s %13s: %-40s ", color, levelString[r.Level], r.Time.Format(dateFormat), caller, r.Message)
64 fmt.Fprintf(b, "%-5s %s %6s %13s: %-40s", levelString[r.Level], r.Time.Format(dateFormat), module, caller, r.Message)
68 for k, v := range r.Context {
73 if k == "module" || k == "caller" {
77 v := formatLogfmtValue(v)
79 // TODO: we should probably check that all of your key bytes aren't invalid
80 if noColor == false && color > 0 {
81 fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m=%s", color, k, v)
95 // formatValue formats a value for serialization
96 func formatLogfmtValue(value interface{}) string {
101 if t, ok := value.(time.Time); ok {
102 // Performance optimization: No need for escaping since the provided
103 // timeFormat doesn't have any escape characters, and escaping is
105 return t.Format(termTimeFormat)
107 value = formatShared(value)
108 switch v := value.(type) {
110 return strconv.FormatBool(v)
112 return strconv.FormatFloat(float64(v), floatFormat, 3, 64)
114 return strconv.FormatFloat(v, floatFormat, 7, 64)
115 case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
116 return fmt.Sprintf("%d", value)
118 return escapeString(v)
120 return escapeString(fmt.Sprintf("%+v", value))
124 // Format the value in json format
125 func formatShared(value interface{}) (result interface{}) {
127 if err := recover(); err != nil {
128 if v := reflect.ValueOf(value); v.Kind() == reflect.Ptr && v.IsNil() {
136 switch v := value.(type) {
138 return v.Format(timeFormat)
151 // A reusuable buffer for outputting data
152 var stringBufPool = sync.Pool{
153 New: func() interface{} { return new(bytes.Buffer) },
156 // Escape the string when needed
157 func escapeString(s string) string {
160 for _, r := range s {
161 if r <= ' ' || r == '=' || r == '"' {
164 if r == '\\' || r == '"' || r == '\n' || r == '\r' || r == '\t' {
168 if needsEscape == false && needsQuotes == false {
171 e := stringBufPool.Get().(*bytes.Buffer)
173 for _, r := range s {
193 ret = string(e.Bytes()[1 : e.Len()-1])
200 // JsonFormatEx formats log records as JSON objects. If pretty is true,
201 // records will be pretty-printed. If lineSeparated is true, records
202 // will be logged with a new line between each record.
203 func JsonFormatEx(pretty, lineSeparated bool) LogFormat {
204 jsonMarshal := json.Marshal
206 jsonMarshal = func(v interface{}) ([]byte, error) {
207 return json.MarshalIndent(v, "", " ")
211 return FormatFunc(func(r *Record) []byte {
212 props := make(map[string]interface{})
215 props["lvl"] = levelString[r.Level]
216 props["msg"] = r.Message
217 for k, v := range r.Context {
218 props[k] = formatJsonValue(v)
221 b, err := jsonMarshal(props)
223 b, _ = jsonMarshal(map[string]string{
224 errorKey: err.Error(),
237 func formatJsonValue(value interface{}) interface{} {
238 value = formatShared(value)
239 switch value.(type) {
240 case int, int8, int16, int32, int64, float32, float64, uint, uint8, uint16, uint32, uint64, string:
243 return fmt.Sprintf("%+v", value)