mirror of
https://github.com/taigrr/wails.git
synced 2026-04-02 05:08:54 -07:00
Support SetLogLevel() at runtime
Refactor of loglevel
This commit is contained in:
@@ -43,7 +43,7 @@ func CreateApp(options *options.App) *App {
|
||||
|
||||
// Set up logger
|
||||
myLogger := logger.New(options.Logger)
|
||||
myLogger.SetLogLevel(uint8(options.LogLevel))
|
||||
myLogger.SetLogLevel(options.LogLevel)
|
||||
|
||||
window := ffenestri.NewApplicationWithConfig(options, myLogger)
|
||||
|
||||
|
||||
@@ -7,10 +7,13 @@ import (
|
||||
"github.com/wailsapp/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 uint8
|
||||
logLevel LogLevel
|
||||
showLevelInLog bool
|
||||
}
|
||||
|
||||
@@ -18,7 +21,7 @@ type Logger struct {
|
||||
// are the targets for the logs
|
||||
func New(output logger.Logger) *Logger {
|
||||
result := &Logger{
|
||||
logLevel: INFO,
|
||||
logLevel: logger.INFO,
|
||||
showLevelInLog: true,
|
||||
output: output,
|
||||
}
|
||||
@@ -37,7 +40,7 @@ func (l *Logger) HideLogLevel() {
|
||||
}
|
||||
|
||||
// SetLogLevel sets the minimum level of logs that will be output
|
||||
func (l *Logger) SetLogLevel(level uint8) {
|
||||
func (l *Logger) SetLogLevel(level LogLevel) {
|
||||
l.logLevel = level
|
||||
}
|
||||
|
||||
@@ -60,23 +63,15 @@ func (l *Logger) Print(message string) error {
|
||||
|
||||
// Trace level logging. Works like Sprintf.
|
||||
func (l *Logger) Trace(format string, args ...interface{}) error {
|
||||
if l.logLevel <= TRACE {
|
||||
if l.logLevel <= logger.TRACE {
|
||||
return l.output.Trace(fmt.Sprintf(format, args...))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// // CustomTrace returns a custom Logging function that will insert the given name before the message
|
||||
// func (l *Logger) CustomTrace(name string) func(format string, args ...interface{}) {
|
||||
// return func(format string, args ...interface{}) {
|
||||
// format = name + " | " + format
|
||||
// l.processLogMessage(fmt.Sprintf(format, args...))
|
||||
// }
|
||||
// }
|
||||
|
||||
// Debug level logging. Works like Sprintf.
|
||||
func (l *Logger) Debug(format string, args ...interface{}) error {
|
||||
if l.logLevel <= DEBUG {
|
||||
if l.logLevel <= logger.DEBUG {
|
||||
return l.output.Debug(fmt.Sprintf(format, args...))
|
||||
}
|
||||
return nil
|
||||
@@ -84,7 +79,7 @@ func (l *Logger) Debug(format string, args ...interface{}) error {
|
||||
|
||||
// Info level logging. Works like Sprintf.
|
||||
func (l *Logger) Info(format string, args ...interface{}) error {
|
||||
if l.logLevel <= INFO {
|
||||
if l.logLevel <= logger.INFO {
|
||||
return l.output.Info(fmt.Sprintf(format, args...))
|
||||
}
|
||||
return nil
|
||||
@@ -92,7 +87,7 @@ func (l *Logger) Info(format string, args ...interface{}) error {
|
||||
|
||||
// Warning level logging. Works like Sprintf.
|
||||
func (l *Logger) Warning(format string, args ...interface{}) error {
|
||||
if l.logLevel <= WARNING {
|
||||
if l.logLevel <= logger.WARNING {
|
||||
return l.output.Warning(fmt.Sprintf(format, args...))
|
||||
}
|
||||
return nil
|
||||
@@ -100,7 +95,7 @@ func (l *Logger) Warning(format string, args ...interface{}) error {
|
||||
|
||||
// Error level logging. Works like Sprintf.
|
||||
func (l *Logger) Error(format string, args ...interface{}) error {
|
||||
if l.logLevel <= ERROR {
|
||||
if l.logLevel <= logger.ERROR {
|
||||
return l.output.Error(fmt.Sprintf(format, args...))
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
package logger
|
||||
|
||||
const (
|
||||
// TRACE level
|
||||
TRACE uint8 = 0
|
||||
|
||||
// DEBUG level logging
|
||||
DEBUG uint8 = 1
|
||||
|
||||
// INFO level logging
|
||||
INFO uint8 = 2
|
||||
|
||||
// WARNING level logging
|
||||
WARNING uint8 = 4
|
||||
|
||||
// ERROR level logging
|
||||
ERROR uint8 = 8
|
||||
|
||||
// FATAL level logging
|
||||
FATAL uint8 = 16
|
||||
|
||||
// BYPASS level logging - does not use a log level
|
||||
BYPASS uint8 = 255
|
||||
)
|
||||
|
||||
var mapLogLevel = map[uint8]string{
|
||||
TRACE: "TRACE | ",
|
||||
DEBUG: "DEBUG | ",
|
||||
INFO: "INFO | ",
|
||||
WARNING: "WARN | ",
|
||||
ERROR: "ERROR | ",
|
||||
FATAL: "FATAL | ",
|
||||
BYPASS: "",
|
||||
}
|
||||
@@ -64,7 +64,7 @@ func (d *DispatchClient) DispatchMessage(incomingMessage string) {
|
||||
d.logger.Trace(fmt.Sprintf("Received message: %+v", incomingMessage))
|
||||
parsedMessage, err := message.Parse(incomingMessage)
|
||||
if err != nil {
|
||||
d.logger.Trace("Error: " + err.Error())
|
||||
d.logger.Error(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ func (d *DispatchClient) DispatchMessage(incomingMessage string) {
|
||||
|
||||
// Check error
|
||||
if err != nil {
|
||||
d.logger.Trace("Error: " + err.Error())
|
||||
d.logger.Error(err.Error())
|
||||
// Hrm... what do we do with this?
|
||||
d.bus.PublishForTarget("generic:message", incomingMessage, d.id)
|
||||
return
|
||||
|
||||
@@ -10,6 +10,7 @@ var logMessageMap = map[byte]string{
|
||||
'W': "log:warning",
|
||||
'E': "log:error",
|
||||
'F': "log:fatal",
|
||||
'S': "log:setlevel",
|
||||
}
|
||||
|
||||
// logMessageParser does what it says on the tin!
|
||||
@@ -25,7 +26,7 @@ func logMessageParser(message string) (*parsedMessage, error) {
|
||||
|
||||
// If the type is invalid, raise error
|
||||
if messageTopic == "" {
|
||||
return nil, fmt.Errorf("log message type '%b' invalid", message[1])
|
||||
return nil, fmt.Errorf("log message type '%c' invalid", message[1])
|
||||
}
|
||||
|
||||
// Create a new parsed message struct
|
||||
|
||||
@@ -2,6 +2,7 @@ package goruntime
|
||||
|
||||
import (
|
||||
"github.com/wailsapp/wails/v2/internal/servicebus"
|
||||
"github.com/wailsapp/wails/v2/pkg/logger"
|
||||
)
|
||||
|
||||
// Log defines all Log related operations
|
||||
@@ -13,6 +14,7 @@ type Log interface {
|
||||
Warning(message string)
|
||||
Error(message string)
|
||||
Fatal(message string)
|
||||
SetLogLevel(level logger.LogLevel)
|
||||
}
|
||||
|
||||
type log struct {
|
||||
@@ -60,3 +62,8 @@ func (r *log) Error(message string) {
|
||||
func (r *log) Fatal(message string) {
|
||||
r.bus.Publish("log:fatal", message)
|
||||
}
|
||||
|
||||
// Sets the log level
|
||||
func (r *log) SetLogLevel(level logger.LogLevel) {
|
||||
r.bus.Publish("log:setlevel", level)
|
||||
}
|
||||
|
||||
@@ -94,3 +94,22 @@ export function Error(message) {
|
||||
export function Fatal(message) {
|
||||
sendLogMessage('F', message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Log level to the given log level
|
||||
*
|
||||
* @export
|
||||
* @param {number} loglevel
|
||||
*/
|
||||
export function SetLogLevel(loglevel) {
|
||||
sendLogMessage('S', loglevel);
|
||||
}
|
||||
|
||||
// Log levels
|
||||
export const Level = {
|
||||
TRACE: 0,
|
||||
DEBUG: 1,
|
||||
INFO: 2,
|
||||
WARNING: 3,
|
||||
ERROR: 4,
|
||||
};
|
||||
|
||||
@@ -74,13 +74,32 @@ function Error(message) {
|
||||
/**
|
||||
* Log the given fatal message with the backend
|
||||
*
|
||||
* @export
|
||||
* @param {string} message
|
||||
*/
|
||||
function Fatal(message) {
|
||||
window.wails.Log.Fatal(message);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the Log level to the given log level
|
||||
*
|
||||
* @param {number} loglevel
|
||||
*/
|
||||
function SetLogLevel(loglevel) {
|
||||
window.wails.Log.SetLogLevel(loglevel);
|
||||
}
|
||||
|
||||
// Log levels
|
||||
const Level = {
|
||||
TRACE: 0,
|
||||
DEBUG: 1,
|
||||
INFO: 2,
|
||||
WARNING: 3,
|
||||
ERROR: 4,
|
||||
};
|
||||
|
||||
|
||||
module.exports = {
|
||||
Print: Print,
|
||||
Trace: Trace,
|
||||
@@ -88,5 +107,7 @@ module.exports = {
|
||||
Info: Info,
|
||||
Warning: Warning,
|
||||
Error: Error,
|
||||
Fatal: Fatal
|
||||
Fatal: Fatal,
|
||||
SetLogLevel: SetLogLevel,
|
||||
Level: Level,
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package subsystem
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/wailsapp/wails/v2/internal/logger"
|
||||
@@ -71,6 +72,19 @@ func (l *Log) Start() error {
|
||||
l.logger.Error(logMessage.Data().(string))
|
||||
case "fatal":
|
||||
l.logger.Fatal(logMessage.Data().(string))
|
||||
case "setlevel":
|
||||
switch inLevel := logMessage.Data().(type) {
|
||||
case logger.LogLevel:
|
||||
l.logger.SetLogLevel(inLevel)
|
||||
case string:
|
||||
uint64level, err := strconv.ParseUint(inLevel, 10, 8)
|
||||
if err != nil {
|
||||
l.logger.Error("Error parsing log level: %+v", inLevel)
|
||||
continue
|
||||
}
|
||||
l.logger.SetLogLevel(logger.LogLevel(uint64level))
|
||||
}
|
||||
|
||||
default:
|
||||
l.logger.Error("unknown log message: %+v", logMessage)
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ func (r *Runtime) Start() error {
|
||||
case "browser":
|
||||
err = r.processBrowserMessage(method, runtimeMessage.Data())
|
||||
default:
|
||||
fmt.Errorf("unknown log message: %+v", runtimeMessage)
|
||||
fmt.Errorf("unknown runtime message: %+v", runtimeMessage)
|
||||
}
|
||||
|
||||
// If we had an error, log it
|
||||
|
||||
@@ -14,13 +14,13 @@ const (
|
||||
INFO LogLevel = 2
|
||||
|
||||
// WARNING level logging
|
||||
WARNING LogLevel = 4
|
||||
WARNING LogLevel = 3
|
||||
|
||||
// ERROR level logging
|
||||
ERROR LogLevel = 8
|
||||
ERROR LogLevel = 4
|
||||
|
||||
// FATAL level logging
|
||||
FATAL LogLevel = 16
|
||||
FATAL LogLevel = 5
|
||||
)
|
||||
|
||||
// Logger specifies the methods required to attach
|
||||
|
||||
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
wails "github.com/wailsapp/wails/v2"
|
||||
"github.com/wailsapp/wails/v2/pkg/logger"
|
||||
)
|
||||
|
||||
// Logger struct
|
||||
@@ -50,3 +51,8 @@ func (l *Logger) Error(message string) {
|
||||
func (l *Logger) Fatal(message string) {
|
||||
l.runtime.Log.Fatal(message)
|
||||
}
|
||||
|
||||
// SetLogLevel will set the given loglevel
|
||||
func (l *Logger) SetLogLevel(loglevel logger.LogLevel) {
|
||||
l.runtime.Log.SetLogLevel(loglevel)
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ func main() {
|
||||
WindowBackgroundIsTranslucent: true,
|
||||
TitleBar: mac.TitleBarHiddenInset(),
|
||||
},
|
||||
LogLevel: logger.INFO,
|
||||
LogLevel: logger.TRACE,
|
||||
})
|
||||
|
||||
app.Bind(&Logger{})
|
||||
|
||||
Reference in New Issue
Block a user