-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f719e96
commit 6ad1098
Showing
3 changed files
with
66 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package log | ||
|
||
// DeferredLogger are needed to save logs and print them after calling `PrintLogs()` command. | ||
// for example, this may be necessary when the logger is not yet initialized, but messages need to be transmitted | ||
// in this case, the messages are saved and printed after the logger is initialized | ||
var DeferredLogger deferredLogger | ||
|
||
type deferredLogger struct { | ||
deferredLogs []deferredLog | ||
} | ||
|
||
type deferredLog struct { | ||
logFunc func(format string, args ...any) | ||
message string | ||
args []any | ||
} | ||
|
||
func (d *deferredLogger) Debug(message string, args ...any) { | ||
d.deferredLogs = append(d.deferredLogs, deferredLog{ | ||
logFunc: Debug, | ||
message: message, | ||
args: args, | ||
}) | ||
} | ||
|
||
func (d *deferredLogger) Info(message string, args ...any) { | ||
d.deferredLogs = append(d.deferredLogs, deferredLog{ | ||
logFunc: Info, | ||
message: message, | ||
args: args, | ||
}) | ||
} | ||
|
||
func (d *deferredLogger) Warn(message string, args ...any) { | ||
d.deferredLogs = append(d.deferredLogs, deferredLog{ | ||
logFunc: Warn, | ||
message: message, | ||
args: args, | ||
}) | ||
} | ||
|
||
func (d *deferredLogger) Error(message string, args ...any) { | ||
d.deferredLogs = append(d.deferredLogs, deferredLog{ | ||
logFunc: Error, | ||
message: message, | ||
args: args, | ||
}) | ||
} | ||
|
||
func (d *deferredLogger) PrintLogs() { | ||
for _, l := range d.deferredLogs { | ||
l.logFunc(l.message, l.args...) | ||
} | ||
// Clear deferredLogs | ||
d.deferredLogs = nil | ||
} |