5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 01:13:03 +08:00

Major logging refactor

This commit is contained in:
Lea Anthony 2020-10-09 11:50:45 +11:00
parent 53b54a8e52
commit ffdfbb8ae5
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
8 changed files with 103 additions and 85 deletions

View File

@ -5,6 +5,5 @@ package app
// Init initialises the application for a debug environment
func (a *App) Init() error {
a.debug = true
println("Initialising debug options")
return nil
}

View File

@ -3,8 +3,6 @@
package app
import (
"os"
"github.com/wailsapp/wails/v2/internal/binding"
"github.com/wailsapp/wails/v2/internal/ffenestri"
"github.com/wailsapp/wails/v2/internal/logger"
@ -44,7 +42,7 @@ func CreateApp(options *options.App) *App {
options.MergeDefaults()
// Set up logger
myLogger := logger.New(os.Stdout)
myLogger := logger.New(options.Logger)
myLogger.SetLogLevel(logger.TRACE)
window := ffenestri.NewApplicationWithConfig(options, myLogger)

View File

@ -2,7 +2,6 @@ package logger
import (
"fmt"
"os"
)
// CustomLogger defines what a user can do with a logger
@ -62,37 +61,36 @@ func (l *customLogger) Write(message string) error {
// Trace level logging. Works like Sprintf.
func (l *customLogger) Trace(format string, args ...interface{}) error {
format = fmt.Sprintf("%s | %s", l.name, format)
return l.logger.processLogMessage(TRACE, format, args...)
return l.logger.Trace(format, args...)
}
// Debug level logging. Works like Sprintf.
func (l *customLogger) Debug(format string, args ...interface{}) error {
format = fmt.Sprintf("%s | %s", l.name, format)
return l.logger.processLogMessage(DEBUG, format, args...)
return l.logger.Debug(format, args...)
}
// Info level logging. Works like Sprintf.
func (l *customLogger) Info(format string, args ...interface{}) error {
format = fmt.Sprintf("%s | %s", l.name, format)
return l.logger.processLogMessage(INFO, format, args...)
return l.logger.Info(format, args...)
}
// Warning level logging. Works like Sprintf.
func (l *customLogger) Warning(format string, args ...interface{}) error {
format = fmt.Sprintf("%s | %s", l.name, format)
return l.logger.processLogMessage(WARNING, format, args...)
return l.logger.Warning(format, args...)
}
// Error level logging. Works like Sprintf.
func (l *customLogger) Error(format string, args ...interface{}) error {
format = fmt.Sprintf("%s | %s", l.name, format)
return l.logger.processLogMessage(ERROR, format, args...)
return l.logger.Error(format, args...)
}
// Fatal level logging. Works like Sprintf.
func (l *customLogger) Fatal(format string, args ...interface{}) {
format = fmt.Sprintf("%s | %s", l.name, format)
l.logger.processLogMessage(FATAL, format, args...)
os.Exit(1)
l.logger.Fatal(format, args...)
}

View File

@ -1,38 +1,28 @@
package logger
import (
"fmt"
"io"
"os"
"sync"
"github.com/wailsapp/wails/v2/pkg/logger"
)
// Logger is a utlility to log messages to a number of destinations
type Logger struct {
writers []io.Writer
output logger.Logger
logLevel uint8
showLevelInLog bool
lock sync.RWMutex
}
// New creates a new Logger. You may pass in a number of `io.Writer`s that
// are the targets for the logs
func New(writers ...io.Writer) *Logger {
func New(output logger.Logger) *Logger {
result := &Logger{
logLevel: INFO,
showLevelInLog: true,
}
for _, writer := range writers {
result.AddOutput(writer)
output: output,
}
return result
}
// Writers gets the log writers
func (l *Logger) Writers() []io.Writer {
return l.writers
}
// CustomLogger creates a new custom logger that prints out a name/id
// before the messages
func (l *Logger) CustomLogger(name string) CustomLogger {
@ -49,95 +39,57 @@ func (l *Logger) SetLogLevel(level uint8) {
l.logLevel = level
}
// AddOutput adds the given `io.Writer` to the list of destinations
// that get logged to
func (l *Logger) AddOutput(writer io.Writer) {
l.writers = append(l.writers, writer)
}
func (l *Logger) write(loglevel uint8, message string) error {
// Don't print logs lower than the current log level
if loglevel < l.logLevel {
return nil
}
// Show log level text if enabled
if l.showLevelInLog {
message = mapLogLevel[loglevel] + message
}
// write out the logs
l.lock.Lock()
for _, writer := range l.writers {
_, err := writer.Write([]byte(message))
if err != nil {
l.lock.Unlock() // Because defer is slow
return err
}
}
l.lock.Unlock()
return nil
}
// writeln appends a newline character to the message before writing
func (l *Logger) writeln(loglevel uint8, message string) error {
return l.write(loglevel, message+"\n")
}
// Writeln writes directly to the output with no log level
// Appends a carriage return to the message
func (l *Logger) Writeln(message string) error {
return l.write(BYPASS, message+"\n")
return l.output.Print(message+"\n")
}
// Write writes directly to the output with no log level
func (l *Logger) Write(message string) error {
return l.write(BYPASS, message)
}
// processLogMessage formats the given message before writing it out
func (l *Logger) processLogMessage(loglevel uint8, format string, args ...interface{}) error {
message := fmt.Sprintf(format, args...)
return l.writeln(loglevel, message)
return l.output.Print(message)
}
// Trace level logging. Works like Sprintf.
func (l *Logger) Trace(format string, args ...interface{}) error {
return l.processLogMessage(TRACE, format, args...)
return l.output.Trace(format, args...)
}
// CustomTrace returns a custom Logging function that will insert the given name before the message
func (l *Logger) CustomTrace(name string) func(format string, args ...interface{}) {
return func(format string, args ...interface{}) {
format = name + " | " + format
l.processLogMessage(TRACE, format, args...)
}
}
// // CustomTrace returns a custom Logging function that will insert the given name before the message
// func (l *Logger) CustomTrace(name string) func(format string, args ...interface{}) {
// return func(format string, args ...interface{}) {
// format = name + " | " + format
// l.processLogMessage(format, args...)
// }
// }
// Debug level logging. Works like Sprintf.
func (l *Logger) Debug(format string, args ...interface{}) error {
return l.processLogMessage(DEBUG, format, args...)
return l.output.Debug(format, args...)
}
// Info level logging. Works like Sprintf.
func (l *Logger) Info(format string, args ...interface{}) error {
return l.processLogMessage(INFO, format, args...)
return l.output.Info(format, args...)
}
// Warning level logging. Works like Sprintf.
func (l *Logger) Warning(format string, args ...interface{}) error {
return l.processLogMessage(WARNING, format, args...)
return l.output.Warning(format, args...)
}
// Error level logging. Works like Sprintf.
func (l *Logger) Error(format string, args ...interface{}) error {
return l.processLogMessage(ERROR, format, args...)
return l.output.Error(format, args...)
}
// Fatal level logging. Works like Sprintf.
func (l *Logger) Fatal(format string, args ...interface{}) {
l.processLogMessage(FATAL, format, args...)
err := l.output.Fatal(format, args...)
// Not much we can do but print it out before exiting
if err != nil {
println(err.Error())
}
os.Exit(1)
}

55
v2/pkg/logger/default.go Normal file
View File

@ -0,0 +1,55 @@
package logger
import (
"fmt"
"os"
)
// DefaultLogger is a utlility to log messages to a number of destinations
type DefaultLogger struct {}
// NewDefaultLogger creates a new Logger.
func NewDefaultLogger() Logger {
return &DefaultLogger{}
}
// Print works like Sprintf.
func (l *DefaultLogger) Print(message string, args ...interface{}) error {
fmt.Printf(message + "\n", args...)
return nil
}
// Trace level logging. Works like Sprintf.
func (l *DefaultLogger) Trace(message string, args ...interface{}) error {
fmt.Printf("TRACE | " + message + "\n", args...)
return nil
}
// Debug level logging. Works like Sprintf.
func (l *DefaultLogger) Debug(message string, args ...interface{}) error {
fmt.Printf("DEBUG | " + message + "\n", args...)
return nil
}
// Info level logging. Works like Sprintf.
func (l *DefaultLogger) Info(message string, args ...interface{}) error {
fmt.Printf("INFO | " + message + "\n", args...)
return nil
}
// Warning level logging. Works like Sprintf.
func (l *DefaultLogger) Warning(message string, args ...interface{}) error {
fmt.Printf("WARN | " + message + "\n", args...)
return nil
}
// Error level logging. Works like Sprintf.
func (l *DefaultLogger) Error(message string, args ...interface{}) error {
fmt.Printf("ERROR | " + message + "\n", args...)
return nil
}
// Fatal level logging. Works like Sprintf.
func (l *DefaultLogger) Fatal(message string, args ...interface{}) error {
fmt.Printf("FATAL | " + message + "\n", args...)
os.Exit(1)
return nil
}

11
v2/pkg/logger/logger.go Normal file
View File

@ -0,0 +1,11 @@
package logger
type Logger interface {
Print(message string, args ...interface{}) error
Trace(message string, args ...interface{}) error
Debug(message string, args ...interface{}) error
Info(message string, args ...interface{}) error
Warning(message string, args ...interface{}) error
Error(message string, args ...interface{}) error
Fatal(message string, args ...interface{}) error
}

View File

@ -1,7 +1,9 @@
package options
import "github.com/wailsapp/wails/v2/pkg/options/mac"
import (
"github.com/wailsapp/wails/v2/pkg/options/mac"
"github.com/wailsapp/wails/v2/pkg/logger"
)
// Default options for creating the App
var Default = &App{
Title: "My Wails App",
@ -15,4 +17,5 @@ var Default = &App{
WebviewIsTransparent: false,
WindowBackgroundIsTranslucent: false,
},
Logger: logger.NewDefaultLogger(),
}

View File

@ -4,6 +4,7 @@ import (
"log"
"github.com/imdario/mergo"
"github.com/wailsapp/wails/v2/pkg/logger"
"github.com/wailsapp/wails/v2/pkg/options/mac"
)
@ -22,6 +23,7 @@ type App struct {
DevTools bool
RGBA int
Mac *mac.Options
Logger logger.Logger
}
// MergeDefaults will set the minimum default values for an application