Files

108 lines
2.7 KiB
Go
Raw Permalink Normal View History

2020-09-15 19:52:54 -05:00
package logger
import (
2020-10-09 15:39:11 +11:00
"fmt"
2020-09-15 19:52:54 -05:00
"os"
2020-10-09 15:13:45 +11:00
2020-10-09 11:50:45 +11:00
"github.com/wailsapp/wails/v2/pkg/logger"
2020-09-15 19:52:54 -05:00
)
2020-10-10 07:31:23 +11:00
// LogLevel is an alias for the public LogLevel
type LogLevel = logger.LogLevel
2020-09-15 19:52:54 -05:00
// Logger is a utlility to log messages to a number of destinations
type Logger struct {
2020-10-09 15:13:45 +11:00
output logger.Logger
2020-10-10 07:31:23 +11:00
logLevel LogLevel
2020-09-15 19:52:54 -05:00
showLevelInLog bool
}
// New creates a new Logger. You may pass in a number of `io.Writer`s that
// are the targets for the logs
2020-10-09 11:50:45 +11:00
func New(output logger.Logger) *Logger {
if output == nil {
output = logger.NewDefaultLogger()
}
2020-09-15 19:52:54 -05:00
result := &Logger{
2020-10-10 07:31:23 +11:00
logLevel: logger.INFO,
2020-09-15 19:52:54 -05:00
showLevelInLog: true,
2020-10-09 15:13:45 +11:00
output: output,
2020-09-15 19:52:54 -05:00
}
2020-09-15 19:52:54 -05:00
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
2020-10-10 07:31:23 +11:00
func (l *Logger) SetLogLevel(level LogLevel) {
2020-09-15 19:52:54 -05:00
l.logLevel = level
}
// Writeln writes directly to the output with no log level
// Appends a carriage return to the message
2020-12-05 14:14:46 +11:00
func (l *Logger) Writeln(message string) {
l.output.Print(message)
2020-09-15 19:52:54 -05:00
}
// Write writes directly to the output with no log level
2020-12-05 14:14:46 +11:00
func (l *Logger) Write(message string) {
l.output.Print(message)
2020-09-15 19:52:54 -05:00
}
2020-10-09 15:13:45 +11:00
// Print writes directly to the output with no log level
// Appends a carriage return to the message
2020-12-05 14:14:46 +11:00
func (l *Logger) Print(message string) {
l.Write(message)
2020-10-09 15:13:45 +11:00
}
2020-09-15 19:52:54 -05:00
// Trace level logging. Works like Sprintf.
2020-12-05 14:14:46 +11:00
func (l *Logger) Trace(format string, args ...interface{}) {
2020-10-10 07:31:23 +11:00
if l.logLevel <= logger.TRACE {
2020-12-05 14:14:46 +11:00
l.output.Trace(fmt.Sprintf(format, args...))
2020-10-09 15:13:45 +11:00
}
2020-09-15 19:52:54 -05:00
}
// Debug level logging. Works like Sprintf.
2020-12-05 14:14:46 +11:00
func (l *Logger) Debug(format string, args ...interface{}) {
2020-10-10 07:31:23 +11:00
if l.logLevel <= logger.DEBUG {
2020-12-05 14:14:46 +11:00
l.output.Debug(fmt.Sprintf(format, args...))
2020-10-09 15:13:45 +11:00
}
2020-09-15 19:52:54 -05:00
}
// Info level logging. Works like Sprintf.
2020-12-05 14:14:46 +11:00
func (l *Logger) Info(format string, args ...interface{}) {
2020-10-10 07:31:23 +11:00
if l.logLevel <= logger.INFO {
2020-12-05 14:14:46 +11:00
l.output.Info(fmt.Sprintf(format, args...))
2020-10-09 15:13:45 +11:00
}
2020-09-15 19:52:54 -05:00
}
// Warning level logging. Works like Sprintf.
2020-12-05 14:14:46 +11:00
func (l *Logger) Warning(format string, args ...interface{}) {
2020-10-10 07:31:23 +11:00
if l.logLevel <= logger.WARNING {
2020-12-05 14:14:46 +11:00
l.output.Warning(fmt.Sprintf(format, args...))
2020-10-09 15:13:45 +11:00
}
2020-09-15 19:52:54 -05:00
}
// Error level logging. Works like Sprintf.
2020-12-05 14:14:46 +11:00
func (l *Logger) Error(format string, args ...interface{}) {
2020-10-10 07:31:23 +11:00
if l.logLevel <= logger.ERROR {
2020-12-05 14:14:46 +11:00
l.output.Error(fmt.Sprintf(format, args...))
2020-10-09 15:13:45 +11:00
}
2020-09-15 19:52:54 -05:00
}
// Fatal level logging. Works like Sprintf.
func (l *Logger) Fatal(format string, args ...interface{}) {
2020-12-05 14:14:46 +11:00
l.output.Fatal(fmt.Sprintf(format, args...))
2020-09-15 19:52:54 -05:00
os.Exit(1)
}