-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
123 lines (104 loc) · 3.16 KB
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package captainslog
import (
"os"
"time"
"vincent.click/pkg/captainslog/v2/caller"
"vincent.click/pkg/captainslog/v2/format"
"vincent.click/pkg/captainslog/v2/levels"
"vincent.click/pkg/captainslog/v2/msg"
)
// Defaults
const (
ISO8601 = "01-02-2006 15:04:05 MST"
)
// Logger is an object for logging
type Logger struct {
// name of the logger; leave empty to log the current function
Name string
Level int
HasColor bool
// layout string used to format the time. See https://pkg.go.dev/time?tab=doc#Time.Format
TimeFormat string
// maximum caller name length to display
NameCutoff int
Stdout *os.File
Stderr *os.File
Format msg.Format
}
// NewLogger returns a new logger with the specified minimum logging level
func NewLogger() *Logger {
return &Logger{
HasColor: true,
Level: levels.Debug,
TimeFormat: ISO8601,
NameCutoff: 15,
Stdout: os.Stdout,
Stderr: os.Stderr,
Format: format.Flat,
}
}
// name returns the name of the logger or of its caller
func (log *Logger) name() string {
if len(log.Name) > 0 {
return log.Name
}
// if the logger has no name, return the name of the caller
return caller.Shorten(caller.GetName(4), log.NameCutoff)
}
// message returns a new message
func (log *Logger) message() *msg.Message {
msg := msg.MsgPool.Get().(*msg.Message)
msg.Time = time.Now().Format(log.TimeFormat)
msg.Name = log.name()
msg.Stdout = log.Stdout
msg.Stderr = log.Stderr
msg.HasColor = log.HasColor
msg.Threshold = log.Level
msg.Print = log.Format
msg.Data = []interface{}{}
return msg
}
// I returns a single field that can be added to logs
func (log *Logger) I(name string, value interface{}) msg.Field {
return msg.Field{name, value}
}
// Field starts a message with a data field
func (log *Logger) Field(name string, value interface{}) *msg.Message {
return log.message().Field(name, value)
}
// Fields starts a message with multiple data fields
func (log *Logger) Fields(fields ...msg.Field) *msg.Message {
return log.message().Fields(fields...)
}
// Trace logs a message with level Trace
func (log *Logger) Trace(format string, args ...interface{}) {
log.message().Trace(format, args...)
}
// Debug logs a message with level Debug
func (log *Logger) Debug(format string, args ...interface{}) {
log.message().Debug(format, args...)
}
// Info logs a message with level Info
func (log *Logger) Info(format string, args ...interface{}) {
log.message().Info(format, args...)
}
// Warn logs a message with level Warn
func (log *Logger) Warn(format string, args ...interface{}) {
log.message().Warn(format, args...)
}
// Error logs a message with level Error
func (log *Logger) Error(format string, args ...interface{}) {
log.message().Error(format, args...)
}
// Exit logs an error and exits with the given code
func (log *Logger) Exit(code int, format string, args ...interface{}) {
log.message().Exit(code, format, args...)
}
// Fatal logs an error and exits with code 1
func (log *Logger) Fatal(format string, args ...interface{}) {
log.message().Fatal(format, args...)
}
// Panic logs an error and panics
func (log *Logger) Panic(format string, args ...interface{}) {
log.message().Panic(format, args...)
}