5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-10 22:19:46 +08:00

Update log plugin to have log levels.

This commit is contained in:
Lea Anthony 2023-09-13 09:45:26 +10:00
parent ea3509d2e7
commit 8c72746edb
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
2 changed files with 28 additions and 2 deletions

View File

@ -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)
}

View File

@ -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<void|Error>}
*/
function SetLogLevel(level) {
return wails.CallByID(2758810652, level);
}