Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
186 lines (159 loc) · 3.8 KB
/
Copy pathmain.go
File metadata and controls
186 lines (159 loc) · 3.8 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package flog
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"sync"
"time"
"github.com/aidenfine/flog/src"
)
type Logger struct {
production bool
// onPanic bool
// onFatal bool
name string
config Config
clock src.Clock
output io.Writer
files []*os.File
mu sync.Mutex
fields []src.Field
writer src.ConsoleWriter
entry src.Entity
timestamp time.Time
closed bool
caller bool
}
type Config struct {
LogLevel string // min level at which logs will show.
OutputPath []string
Separator string // lets remove this, we prob dont need it.
// errorOutputPath []string
// stacktrace bool
ShowTime bool
TimeEncoding string // Go time layout; empty defaults to time.RFC3339. Used when ShowTime is true.
}
// New creates a logger and opens its output files in append mode.
func New(production bool, name string, cfg Config) (*Logger, error) {
// set default
if cfg.Separator == "" {
cfg.Separator = "{}"
}
l := &Logger{
production: production,
name: name,
config: cfg,
clock: src.SystemClock,
}
outputs := []io.Writer{os.Stderr}
for _, path := range cfg.OutputPath {
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
return nil, errors.Join(fmt.Errorf("open log file %q: %w", path, err), l.Close())
}
l.files = append(l.files, file)
outputs = append(outputs, file)
}
l.output = io.MultiWriter(outputs...)
return l, nil
}
func (l *Logger) Info(msg string, fields ...any) {
l.log("INFO", msg, fields...)
}
func (l *Logger) Debug(msg string, fields ...any) {
l.log("DEBUG", msg, fields...)
}
func (l *Logger) Error(msg string, fields ...any) {
l.log("ERROR", msg, fields...)
}
func (l *Logger) Warn(msg string, fields ...any) {
l.log("WARN", msg, fields...)
}
func (l *Logger) Fatal(msg string, fields ...any) {
l.log("FATAL", msg, fields...)
}
func (l *Logger) Panic(msg string, fields ...any) {
l.log("PANIC", msg, fields...)
}
func (l *Logger) log(level, msg string, fields ...any) {
l.mu.Lock()
defer l.mu.Unlock()
if l.closed {
return
}
caller := ""
if l.caller {
_, file, line, ok := runtime.Caller(2)
if ok {
caller = fmt.Sprintf(" - %s:%d", filepath.Base(file), line)
}
}
if e := l.check(msg, level, fields...); e != nil {
_, err := l.writer.WriteLog(l.output, e, caller)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to write log: %v\n", err)
}
}
// clear fields
clear(l.fields)
l.fields = l.fields[:0]
l.entry = src.Entity{}
// is this the best way to do this?
if level == "PANIC" {
panic("")
}
// once again could this be done differently?
if level == "FATAL" {
os.Exit(1)
}
}
func (l *Logger) Caller() *Logger {
l.caller = true
return l
}
// Close releases the output files opened by New. It is safe to call repeatedly.
// Log calls after Close are ignored. Standard error is never closed.
func (l *Logger) Close() error {
l.mu.Lock()
defer l.mu.Unlock()
if l.closed {
return nil
}
l.closed = true
var errs []error
for _, file := range l.files {
if err := file.Close(); err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}
func (l *Logger) check(msg string, level string, fields ...any) *src.Entity {
if src.LogLevelToInt[level] < src.LogLevelToInt[l.config.LogLevel] {
return nil
}
var t *time.Time = nil
if l.config.ShowTime {
l.timestamp = l.clock.Now()
t = &l.timestamp
}
l.fields = src.AppendFields(l.fields[:0], fields...)
l.entry = src.Entity{
Msg: msg,
LoggerName: l.name,
Time: t,
TimeFormat: l.config.TimeEncoding,
Fields: l.fields,
LogLevel: level,
}
// not sure how much this will log.
// May need to do an offset capture??
// if l.config.stacktrace {
// stack := debug.Stack()
// fmt.Println(string(stack))
// }
return &l.entry
}