Skip to content

Commit d7edea4

Browse files
committed
Merge branch 'feature/function-log' of https://github.com/Azer0s/logrus into Azer0s-feature/function-log
2 parents e8fa988 + 7d248fa commit d7edea4

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
logrus
22
vendor
3+
4+
.idea/

example_function_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package logrus_test
2+
3+
import (
4+
"fmt"
5+
log "github.com/sirupsen/logrus"
6+
"testing"
7+
)
8+
9+
func TestLogger_LogFn(t *testing.T) {
10+
log.SetFormatter(&log.JSONFormatter{})
11+
log.SetLevel(log.WarnLevel)
12+
13+
log.InfoFn(func() []interface{} {
14+
fmt.Println("This is never run")
15+
return []interface{} {
16+
"Hello",
17+
}
18+
})
19+
20+
log.ErrorFn(func() []interface{} {
21+
fmt.Println("This runs")
22+
return []interface{} {
23+
"Oopsi",
24+
}
25+
})
26+
}

exported.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,51 @@ func Fatal(args ...interface{}) {
134134
std.Fatal(args...)
135135
}
136136

137+
// TraceFn logs a message from a func at level Trace on the standard logger.
138+
func TraceFn(fn LogFunction) {
139+
std.TraceFn(fn)
140+
}
141+
142+
// DebugFn logs a message from a func at level Debug on the standard logger.
143+
func DebugFn(fn LogFunction) {
144+
std.DebugFn(fn)
145+
}
146+
147+
// PrintFn logs a message from a func at level Info on the standard logger.
148+
func PrintFn(fn LogFunction) {
149+
std.PrintFn(fn)
150+
}
151+
152+
// InfoFn logs a message from a func at level Info on the standard logger.
153+
func InfoFn(fn LogFunction) {
154+
std.InfoFn(fn)
155+
}
156+
157+
// WarnFn logs a message from a func at level Warn on the standard logger.
158+
func WarnFn(fn LogFunction) {
159+
std.WarnFn(fn)
160+
}
161+
162+
// WarningFn logs a message from a func at level Warn on the standard logger.
163+
func WarningFn(fn LogFunction) {
164+
std.WarningFn(fn)
165+
}
166+
167+
// ErrorFn logs a message from a func at level Error on the standard logger.
168+
func ErrorFn(fn LogFunction) {
169+
std.ErrorFn(fn)
170+
}
171+
172+
// PanicFn logs a message from a func at level Panic on the standard logger.
173+
func PanicFn(fn LogFunction) {
174+
std.PanicFn(fn)
175+
}
176+
177+
// FatalFn logs a message from a func at level Fatal on the standard logger then the process will exit with status set to 1.
178+
func FatalFn(fn LogFunction) {
179+
std.FatalFn(fn)
180+
}
181+
137182
// Tracef logs a message at level Trace on the standard logger.
138183
func Tracef(format string, args ...interface{}) {
139184
std.Tracef(format, args...)

logger.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import (
99
"time"
1010
)
1111

12+
// LogFunction For big messages, it can be more efficient to pass a function
13+
// and only call it if the log level is actually enables rather than
14+
// generating the log message and then checking if the level is enabled
15+
type LogFunction func()[]interface{}
16+
1217
type Logger struct {
1318
// The logs are `io.Copy`'d to this in a mutex. It's common to set this to a
1419
// file, or leave it default which is `os.Stderr`. You can also set this to
@@ -195,6 +200,14 @@ func (logger *Logger) Log(level Level, args ...interface{}) {
195200
}
196201
}
197202

203+
func (logger *Logger) LogFn(level Level, fn LogFunction) {
204+
if logger.IsLevelEnabled(level) {
205+
entry := logger.newEntry()
206+
entry.Log(level, fn()...)
207+
logger.releaseEntry(entry)
208+
}
209+
}
210+
198211
func (logger *Logger) Trace(args ...interface{}) {
199212
logger.Log(TraceLevel, args...)
200213
}
@@ -234,6 +247,45 @@ func (logger *Logger) Panic(args ...interface{}) {
234247
logger.Log(PanicLevel, args...)
235248
}
236249

250+
func (logger *Logger) TraceFn(fn LogFunction) {
251+
logger.LogFn(TraceLevel, fn)
252+
}
253+
254+
func (logger *Logger) DebugFn(fn LogFunction) {
255+
logger.LogFn(DebugLevel, fn)
256+
}
257+
258+
func (logger *Logger) InfoFn(fn LogFunction) {
259+
logger.LogFn(InfoLevel, fn)
260+
}
261+
262+
func (logger *Logger) PrintFn(fn LogFunction) {
263+
entry := logger.newEntry()
264+
entry.Print(fn()...)
265+
logger.releaseEntry(entry)
266+
}
267+
268+
func (logger *Logger) WarnFn(fn LogFunction) {
269+
logger.LogFn(WarnLevel, fn)
270+
}
271+
272+
func (logger *Logger) WarningFn(fn LogFunction) {
273+
logger.WarnFn(fn)
274+
}
275+
276+
func (logger *Logger) ErrorFn(fn LogFunction) {
277+
logger.LogFn(ErrorLevel, fn)
278+
}
279+
280+
func (logger *Logger) FatalFn(fn LogFunction) {
281+
logger.LogFn(FatalLevel, fn)
282+
logger.Exit(1)
283+
}
284+
285+
func (logger *Logger) PanicFn(fn LogFunction) {
286+
logger.LogFn(PanicLevel, fn)
287+
}
288+
237289
func (logger *Logger) Logln(level Level, args ...interface{}) {
238290
if logger.IsLevelEnabled(level) {
239291
entry := logger.newEntry()

0 commit comments

Comments
 (0)