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:
parent
ea3509d2e7
commit
8c72746edb
@ -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)
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user