David K. Bainbridge | 528b318 | 2017-01-23 08:51:59 -0800 | [diff] [blame] | 1 | // Copyright 2014 Canonical Ltd. |
| 2 | // Licensed under the LGPLv3, see LICENCE file for details. |
| 3 | |
| 4 | package loggo |
| 5 | |
| 6 | import ( |
| 7 | "fmt" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | // DefaultFormatter returns the parameters separated by spaces except for |
| 14 | // filename and line which are separated by a colon. The timestamp is shown |
| 15 | // to second resolution in UTC. For example: |
| 16 | // 2016-07-02 15:04:05 |
| 17 | func DefaultFormatter(entry Entry) string { |
| 18 | ts := entry.Timestamp.In(time.UTC).Format("2006-01-02 15:04:05") |
| 19 | // Just get the basename from the filename |
| 20 | filename := filepath.Base(entry.Filename) |
| 21 | return fmt.Sprintf("%s %s %s %s:%d %s", ts, entry.Level, entry.Module, filename, entry.Line, entry.Message) |
| 22 | } |
| 23 | |
| 24 | // TimeFormat is the time format used for the default writer. |
| 25 | // This can be set with the environment variable LOGGO_TIME_FORMAT. |
| 26 | var TimeFormat = initTimeFormat() |
| 27 | |
| 28 | func initTimeFormat() string { |
| 29 | format := os.Getenv("LOGGO_TIME_FORMAT") |
| 30 | if format != "" { |
| 31 | return format |
| 32 | } |
| 33 | return "15:04:05" |
| 34 | } |
| 35 | |
| 36 | func formatTime(ts time.Time) string { |
| 37 | return ts.Format(TimeFormat) |
| 38 | } |