5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-09 21:00:24 +08:00
wails/v3/pkg/services/log/log.go
Atterpac e316cd0719
[V3] Plugins implemenations (#3570)
* plugin handler and lifecycle

* rebase

* remove reflect

s

* remove Config and NewPlugin from plugin template

* Remove plugin manager, generation of plugin interface

* implement http handlers for services

remove log

trim path

prefix wails/services

* update plugine example

* Misc updates

* Ported plugins to services, rewritten example

* Added fileserver

* Update OnStartup and use a context for the application

* Rename PathPrefix to Route. Create docs.

* Use service config copy. Add Name to Service Options. Improve service generation.

* Use service config copy. Add Name to Service Options. Improve service generation. Update README

* Remove rogue db

* Update changelog.md

---------

Co-authored-by: Lea O'Anthony <lea.anthony@gmail.com>
2024-09-01 17:26:22 +10:00

78 lines
1.8 KiB
Go

package log
import (
"context"
_ "embed"
"log/slog"
"github.com/wailsapp/wails/v3/pkg/application"
)
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)
}
type LoggerService struct {
config *Config
app *application.App
level slog.LevelVar
}
func NewLoggerService(config *Config) *LoggerService {
if config.Logger == nil {
config.Logger = application.DefaultLogger(config.LogLevel)
}
result := &LoggerService{
config: config,
}
result.level.Set(config.LogLevel)
return result
}
func New() *LoggerService {
return NewLoggerService(&Config{})
}
// OnShutdown is called when the app is shutting down
// You can use this to clean up any resources you have allocated
func (l *LoggerService) OnShutdown() error { return nil }
// Name returns the name of the plugin.
// You should use the go module format e.g. github.com/myuser/myplugin
func (l *LoggerService) Name() string {
return "github.com/wailsapp/wails/v3/plugins/log"
}
func (l *LoggerService) OnStartup(ctx context.Context, options application.ServiceOptions) error {
// Any initialization code here
return nil
}
func (l *LoggerService) Debug(message string, args ...any) {
l.config.Logger.Debug(message, args...)
}
func (l *LoggerService) Info(message string, args ...any) {
l.config.Logger.Info(message, args...)
}
func (l *LoggerService) Warning(message string, args ...any) {
l.config.Logger.Warn(message, args...)
}
func (l *LoggerService) Error(message string, args ...any) {
l.config.Logger.Error(message, args...)
}
func (l *LoggerService) SetLogLevel(level slog.Level) {
l.level.Set(level)
}