Move to slog logger for application and log plugin

This commit is contained in:
Lea Anthony
2023-08-10 22:04:13 +10:00
parent 2553fa1f74
commit 13be4a333f
11 changed files with 102 additions and 191 deletions
+12 -70
View File
@@ -2,11 +2,8 @@ package log
import (
_ "embed"
"fmt"
"io"
"os"
"github.com/wailsapp/wails/v3/pkg/application"
"log/slog"
)
//go:embed plugin.js
@@ -17,29 +14,9 @@ var pluginJS string
// It must implement the application.Plugin interface.
// Both the Init() and Shutdown() methods are called synchronously when the app starts and stops.
type LogLevel = float64
const (
Trace LogLevel = iota + 1
Debug
Info
Warning
Error
Fatal
)
type Config struct {
// Where the logs are written to. Defaults to os.Stderr
// If you want to write to a file, use os.OpenFile()
// e.g. os.OpenFile("mylog.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
// Closes the writer when the app shuts down
Writer io.WriteCloser
// The initial log level. Defaults to Debug
Level LogLevel
// Disables the log level prefixes
DisablePrefix bool
// Logger is the logger to use. If not set, a default logger will be used.
Logger *slog.Logger
// Handles errors that occur when writing to the log
ErrorHandler func(err error)
@@ -48,19 +25,16 @@ type Config struct {
type Plugin struct {
config *Config
app *application.App
level LogLevel
}
func NewPluginWithConfig(config *Config) *Plugin {
if config.Level == 0 {
config.Level = Debug
}
if config.Writer == nil {
config.Writer = os.Stderr
if config.Logger == nil {
config.Logger = application.DefaultLogger()
}
return &Plugin{
config: config,
level: config.Level,
}
}
@@ -70,9 +44,7 @@ func NewPlugin() *Plugin {
// Shutdown is called when the app is shutting down
// You can use this to clean up any resources you have allocated
func (p *Plugin) Shutdown() {
p.config.Writer.Close()
}
func (p *Plugin) Shutdown() {}
// Name returns the name of the plugin.
// You should use the go module format e.g. github.com/myuser/myplugin
@@ -88,13 +60,10 @@ func (p *Plugin) Init(app *application.App) error {
// CallableByJS returns a list of methods that can be called from the frontend
func (p *Plugin) CallableByJS() []string {
return []string{
"Trace",
"Debug",
"Info",
"Warning",
"Error",
"Fatal",
"SetLevel",
}
}
@@ -108,45 +77,18 @@ func (p *Plugin) InjectJS() string {
// You can also return any type that is JSON serializable.
// See https://golang.org/pkg/encoding/json/#Marshal for more information.
func (p *Plugin) write(prefix string, level LogLevel, message string, args ...any) {
if level >= p.level {
if !p.config.DisablePrefix {
message = prefix + " " + message
}
_, err := fmt.Fprintln(p.config.Writer, fmt.Sprintf(message, args...))
if err != nil && p.config.ErrorHandler != nil {
p.config.ErrorHandler(err)
}
}
}
func (p *Plugin) Trace(message string, args ...any) {
p.write("[Trace]", Trace, message, args...)
}
func (p *Plugin) Debug(message string, args ...any) {
p.write("[Debug]", Debug, message, args...)
p.config.Logger.Debug(message, args...)
}
func (p *Plugin) Info(message string, args ...any) {
p.write("[Info]", Info, message, args...)
p.config.Logger.Info(message, args...)
}
func (p *Plugin) Warning(message string, args ...any) {
p.write("[Warning]", Warning, message, args...)
p.config.Logger.Warn(message, args...)
}
func (p *Plugin) Error(message string, args ...any) {
p.write("[Error]", Error, message, args...)
}
func (p *Plugin) Fatal(message string, args ...any) {
p.write("[FATAL]", Fatal, message, args...)
}
func (p *Plugin) SetLevel(newLevel LogLevel) {
if newLevel == 0 {
newLevel = Debug
}
p.level = newLevel
p.config.Logger.Error(message, args...)
}
-54
View File
@@ -2,16 +2,6 @@
// This file should contain helper functions for the that can be used by the frontend.
// Below are examples of how to use JSDoc to define the Hashes struct and the exported functions.
/**
* Log at the Trace level.
* @param input {string} - The message in printf format.
* @param args {...any} - The arguments for the log message.
* @returns {Promise<void|Error>}
*/
function Trace(input, ...args) {
return wails.Plugin("log", "Trace", input, ...args);
}
/**
* Log at the Debug level.
* @param input {string} - The message in printf format.
@@ -52,47 +42,3 @@ function Warning(input, ...args) {
function Error(input, ...args) {
return wails.Plugin("log", "Error", input, ...args);
}
/**
* Log at the Fatal level.
* @param input {string} - The message in printf format.
* @param args {...any} - The arguments for the log message.
* @returns {Promise<void|Error>}
*/
function Fatal(input, ...args) {
return wails.Plugin("log", "Fatal", input, ...args);
}
/**
* SetLevel sets the logging level
* @param level {Level} The log level to set
* @returns {Promise<void>}
*/
function SetLevel(level) {
return wails.Plugin("log", "SetLevel", level);
}
/**
* Log Level.
* @readonly
* @enum {number}
*/
let Level = {
Trace: 1,
Debug: 2,
Info: 3,
Warning: 4,
Error: 5,
Fatal: 6,
};
window.Logger = {
Trace,
Debug,
Info,
Warning,
Error,
Fatal,
SetLevel,
Level,
}