options.go

 1package log
 2
 3import (
 4	"fmt"
 5	"time"
 6)
 7
 8// DefaultTimeFormat is the default time format.
 9const DefaultTimeFormat = "2006/01/02 15:04:05"
10
11// TimeFunction is a function that returns a time.Time.
12type TimeFunction = func(time.Time) time.Time
13
14// NowUTC is a convenient function that returns the
15// current time in UTC timezone.
16//
17// This is to be used as a time function.
18// For example:
19//
20//	log.SetTimeFunction(log.NowUTC)
21func NowUTC(t time.Time) time.Time {
22	return t.UTC()
23}
24
25// CallerFormatter is the caller formatter.
26type CallerFormatter func(string, int, string) string
27
28// ShortCallerFormatter is a caller formatter that returns the last 2 levels of the path
29// and line number.
30func ShortCallerFormatter(file string, line int, _ string) string {
31	return fmt.Sprintf("%s:%d", trimCallerPath(file, 2), line)
32}
33
34// LongCallerFormatter is a caller formatter that returns the full path and line number.
35func LongCallerFormatter(file string, line int, _ string) string {
36	return fmt.Sprintf("%s:%d", file, line)
37}
38
39// Options is the options for the logger.
40type Options struct {
41	// TimeFunction is the time function for the logger. The default is time.Now.
42	TimeFunction TimeFunction
43	// TimeFormat is the time format for the logger. The default is "2006/01/02 15:04:05".
44	TimeFormat string
45	// Level is the level for the logger. The default is InfoLevel.
46	Level Level
47	// Prefix is the prefix for the logger. The default is no prefix.
48	Prefix string
49	// ReportTimestamp is whether the logger should report the timestamp. The default is false.
50	ReportTimestamp bool
51	// ReportCaller is whether the logger should report the caller location. The default is false.
52	ReportCaller bool
53	// CallerFormatter is the caller format for the logger. The default is ShortCallerFormatter.
54	CallerFormatter CallerFormatter
55	// CallerOffset is the caller format for the logger. The default is 0.
56	CallerOffset int
57	// Fields is the fields for the logger. The default is no fields.
58	Fields []interface{}
59	// Formatter is the formatter for the logger. The default is TextFormatter.
60	Formatter Formatter
61}