blob: ef8aa7a893922099d2ea5a9e45dedeb5fac5dd05 [file] [log] [blame]
David K. Bainbridge528b3182017-01-23 08:51:59 -08001// Copyright 2014 Canonical Ltd.
2// Licensed under the LGPLv3, see LICENCE file for details.
3
4package loggo
5
6import (
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
17func 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.
26var TimeFormat = initTimeFormat()
27
28func initTimeFormat() string {
29 format := os.Getenv("LOGGO_TIME_FORMAT")
30 if format != "" {
31 return format
32 }
33 return "15:04:05"
34}
35
36func formatTime(ts time.Time) string {
37 return ts.Format(TimeFormat)
38}