forked from wailsapp/wails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default_logger.go
107 lines (89 loc) · 2.65 KB
/
default_logger.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
package logger
import (
"fmt"
"os"
"github.com/secoba/wails/v2/pkg/logger"
)
// LogLevel is an alias for the public LogLevel
type LogLevel = logger.LogLevel
// Logger is a utlility to log messages to a number of destinations
type Logger struct {
output logger.Logger
logLevel LogLevel
showLevelInLog bool
}
// New creates a new Logger. You may pass in a number of `io.Writer`s that
// are the targets for the logs
func New(output logger.Logger) *Logger {
if output == nil {
output = logger.NewDefaultLogger()
}
result := &Logger{
logLevel: logger.INFO,
showLevelInLog: true,
output: output,
}
return result
}
// CustomLogger creates a new custom logger that prints out a name/id
// before the messages
func (l *Logger) CustomLogger(name string) CustomLogger {
return newcustomLogger(l, name)
}
// HideLogLevel removes the loglevel text from the start of each logged line
func (l *Logger) HideLogLevel() {
l.showLevelInLog = true
}
// SetLogLevel sets the minimum level of logs that will be output
func (l *Logger) SetLogLevel(level LogLevel) {
l.logLevel = level
}
// Writeln writes directly to the output with no log level
// Appends a carriage return to the message
func (l *Logger) Writeln(message string) {
l.output.Print(message)
}
// Write writes directly to the output with no log level
func (l *Logger) Write(message string) {
l.output.Print(message)
}
// Print writes directly to the output with no log level
// Appends a carriage return to the message
func (l *Logger) Print(message string) {
l.Write(message)
}
// Trace level logging. Works like Sprintf.
func (l *Logger) Trace(format string, args ...interface{}) {
if l.logLevel <= logger.TRACE {
l.output.Trace(fmt.Sprintf(format, args...))
}
}
// Debug level logging. Works like Sprintf.
func (l *Logger) Debug(format string, args ...interface{}) {
if l.logLevel <= logger.DEBUG {
l.output.Debug(fmt.Sprintf(format, args...))
}
}
// Info level logging. Works like Sprintf.
func (l *Logger) Info(format string, args ...interface{}) {
if l.logLevel <= logger.INFO {
l.output.Info(fmt.Sprintf(format, args...))
}
}
// Warning level logging. Works like Sprintf.
func (l *Logger) Warning(format string, args ...interface{}) {
if l.logLevel <= logger.WARNING {
l.output.Warning(fmt.Sprintf(format, args...))
}
}
// Error level logging. Works like Sprintf.
func (l *Logger) Error(format string, args ...interface{}) {
if l.logLevel <= logger.ERROR {
l.output.Error(fmt.Sprintf(format, args...))
}
}
// Fatal level logging. Works like Sprintf.
func (l *Logger) Fatal(format string, args ...interface{}) {
l.output.Fatal(fmt.Sprintf(format, args...))
os.Exit(1)
}