From 8c72746edb93b82b67aff1ed76e653182f3c732a Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Wed, 13 Sep 2023 09:45:26 +1000 Subject: [PATCH] Update log plugin to have log levels. --- v3/plugins/log/plugin.go | 15 +++++++++++++-- v3/plugins/log/plugin.js | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/v3/plugins/log/plugin.go b/v3/plugins/log/plugin.go index 62af16a8c..f9e180c34 100644 --- a/v3/plugins/log/plugin.go +++ b/v3/plugins/log/plugin.go @@ -18,6 +18,9 @@ type Config struct { // Logger is the logger to use. If not set, a default logger will be used. Logger *slog.Logger + // LogLevel defines the log level of the logger. + LogLevel slog.Level + // Handles errors that occur when writing to the log ErrorHandler func(err error) } @@ -25,16 +28,19 @@ type Config struct { type Plugin struct { config *Config app *application.App + level slog.LevelVar } func NewPluginWithConfig(config *Config) *Plugin { if config.Logger == nil { - config.Logger = application.DefaultLogger() + config.Logger = application.DefaultLogger(config.LogLevel) } - return &Plugin{ + result := &Plugin{ config: config, } + result.level.Set(config.LogLevel) + return result } func NewPlugin() *Plugin { @@ -62,6 +68,7 @@ func (p *Plugin) CallableByJS() []string { "Info", "Warning", "Error", + "SetLogLevel", } } @@ -90,3 +97,7 @@ func (p *Plugin) Warning(message string, args ...any) { func (p *Plugin) Error(message string, args ...any) { p.config.Logger.Error(message, args...) } + +func (p *Plugin) SetLogLevel(level slog.Level) { + p.level.Set(level) +} diff --git a/v3/plugins/log/plugin.js b/v3/plugins/log/plugin.js index 216a00344..7fea212d6 100644 --- a/v3/plugins/log/plugin.js +++ b/v3/plugins/log/plugin.js @@ -42,3 +42,18 @@ function Warning(input, ...args) { function Error(input, ...args) { return wails.CallByID(878590242, input, ...args); } + +const LevelDebug = -4 +const LevelInfo = 0 +const LevelWarn = 4 +const LevelError = 8 + + +/** + * Set Log level + * @param level {LogLevel} - The log level to set. + * @returns {Promise} + */ +function SetLogLevel(level) { + return wails.CallByID(2758810652, level); +}