mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-04 03:29:03 +08:00
Support Print logging
This commit is contained in:
parent
9f62a08cd2
commit
228285f693
@ -2,12 +2,13 @@ package logger
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/wailsapp/wails/v2/pkg/logger"
|
"github.com/wailsapp/wails/v2/pkg/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Logger is a utlility to log messages to a number of destinations
|
// Logger is a utlility to log messages to a number of destinations
|
||||||
type Logger struct {
|
type Logger struct {
|
||||||
output logger.Logger
|
output logger.Logger
|
||||||
logLevel uint8
|
logLevel uint8
|
||||||
showLevelInLog bool
|
showLevelInLog bool
|
||||||
}
|
}
|
||||||
@ -18,7 +19,7 @@ func New(output logger.Logger) *Logger {
|
|||||||
result := &Logger{
|
result := &Logger{
|
||||||
logLevel: INFO,
|
logLevel: INFO,
|
||||||
showLevelInLog: true,
|
showLevelInLog: true,
|
||||||
output: output,
|
output: output,
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
@ -42,7 +43,7 @@ func (l *Logger) SetLogLevel(level uint8) {
|
|||||||
// Writeln writes directly to the output with no log level
|
// Writeln writes directly to the output with no log level
|
||||||
// Appends a carriage return to the message
|
// Appends a carriage return to the message
|
||||||
func (l *Logger) Writeln(message string) error {
|
func (l *Logger) Writeln(message string) error {
|
||||||
return l.output.Print(message+"\n")
|
return l.output.Print(message + "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write writes directly to the output with no log level
|
// Write writes directly to the output with no log level
|
||||||
@ -50,9 +51,18 @@ func (l *Logger) Write(message string) error {
|
|||||||
return l.output.Print(message)
|
return l.output.Print(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Print writes directly to the output with no log level
|
||||||
|
// Appends a carriage return to the message
|
||||||
|
func (l *Logger) Print(message string) error {
|
||||||
|
return l.Write(message)
|
||||||
|
}
|
||||||
|
|
||||||
// Trace level logging. Works like Sprintf.
|
// Trace level logging. Works like Sprintf.
|
||||||
func (l *Logger) Trace(format string, args ...interface{}) error {
|
func (l *Logger) Trace(format string, args ...interface{}) error {
|
||||||
return l.output.Trace(format, args...)
|
if l.logLevel <= TRACE {
|
||||||
|
return l.output.Trace(format, args...)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// // CustomTrace returns a custom Logging function that will insert the given name before the message
|
// // CustomTrace returns a custom Logging function that will insert the given name before the message
|
||||||
@ -65,23 +75,34 @@ func (l *Logger) Trace(format string, args ...interface{}) error {
|
|||||||
|
|
||||||
// Debug level logging. Works like Sprintf.
|
// Debug level logging. Works like Sprintf.
|
||||||
func (l *Logger) Debug(format string, args ...interface{}) error {
|
func (l *Logger) Debug(format string, args ...interface{}) error {
|
||||||
return l.output.Debug(format, args...)
|
if l.logLevel <= DEBUG {
|
||||||
|
return l.output.Debug(format, args...)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Info level logging. Works like Sprintf.
|
// Info level logging. Works like Sprintf.
|
||||||
func (l *Logger) Info(format string, args ...interface{}) error {
|
func (l *Logger) Info(format string, args ...interface{}) error {
|
||||||
return l.output.Info(format, args...)
|
if l.logLevel <= INFO {
|
||||||
|
return l.output.Info(format, args...)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Warning level logging. Works like Sprintf.
|
// Warning level logging. Works like Sprintf.
|
||||||
func (l *Logger) Warning(format string, args ...interface{}) error {
|
func (l *Logger) Warning(format string, args ...interface{}) error {
|
||||||
return l.output.Warning(format, args...)
|
if l.logLevel <= WARNING {
|
||||||
|
return l.output.Warning(format, args...)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error level logging. Works like Sprintf.
|
// Error level logging. Works like Sprintf.
|
||||||
func (l *Logger) Error(format string, args ...interface{}) error {
|
func (l *Logger) Error(format string, args ...interface{}) error {
|
||||||
return l.output.Error(format, args...)
|
if l.logLevel <= ERROR {
|
||||||
|
return l.output.Error(format, args...)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fatal level logging. Works like Sprintf.
|
// Fatal level logging. Works like Sprintf.
|
||||||
|
@ -3,6 +3,7 @@ package message
|
|||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
var logMessageMap = map[byte]string{
|
var logMessageMap = map[byte]string{
|
||||||
|
'P': "log:print",
|
||||||
'T': "log:trace",
|
'T': "log:trace",
|
||||||
'D': "log:debug",
|
'D': "log:debug",
|
||||||
'I': "log:info",
|
'I': "log:info",
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
// Log defines all Log related operations
|
// Log defines all Log related operations
|
||||||
type Log interface {
|
type Log interface {
|
||||||
|
Print(message string)
|
||||||
Trace(message string)
|
Trace(message string)
|
||||||
Debug(message string)
|
Debug(message string)
|
||||||
Info(message string)
|
Info(message string)
|
||||||
@ -25,6 +26,11 @@ func newLog(bus *servicebus.ServiceBus) Log {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Print prints a Print level message
|
||||||
|
func (r *log) Print(message string) {
|
||||||
|
r.bus.Publish("log:print", message)
|
||||||
|
}
|
||||||
|
|
||||||
// Trace prints a Trace level message
|
// Trace prints a Trace level message
|
||||||
func (r *log) Trace(message string) {
|
func (r *log) Trace(message string) {
|
||||||
r.bus.Publish("log:trace", message)
|
r.bus.Publish("log:trace", message)
|
||||||
|
@ -57,6 +57,8 @@ func (l *Log) Start() error {
|
|||||||
case logMessage := <-l.logChannel:
|
case logMessage := <-l.logChannel:
|
||||||
logType := strings.TrimPrefix(logMessage.Topic(), "log:")
|
logType := strings.TrimPrefix(logMessage.Topic(), "log:")
|
||||||
switch logType {
|
switch logType {
|
||||||
|
case "print":
|
||||||
|
l.logger.Print(logMessage.Data().(string))
|
||||||
case "trace":
|
case "trace":
|
||||||
l.logger.Trace(logMessage.Data().(string))
|
l.logger.Trace(logMessage.Data().(string))
|
||||||
case "debug":
|
case "debug":
|
||||||
|
24
v2/test/kitchensink/frontend/package-lock.json
generated
24
v2/test/kitchensink/frontend/package-lock.json
generated
@ -124,9 +124,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"@wails/runtime": {
|
"@wails/runtime": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/@wails/runtime/-/runtime-1.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/@wails/runtime/-/runtime-1.0.1.tgz",
|
||||||
"integrity": "sha512-YKL9k4ZRvltnN9Io4gM03u7bk7VX9YjDLu8BdkaHws+3Tt0H+NKt/XnMTo9beUZdPDYnQOgAo5CTxR5UP+n/xA==",
|
"integrity": "sha512-zAhPm1eTZ7f7IsE2II0HoDvCtnttj7ah3gzCTpoAd9zltuoao6aU7m1RMsuqLvd/zvdsA4bKywmVjsa2O4zJpQ==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"alphanum-sort": {
|
"alphanum-sort": {
|
||||||
@ -965,9 +965,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"halfmoon": {
|
"halfmoon": {
|
||||||
"version": "1.1.0",
|
"version": "1.1.1",
|
||||||
"resolved": "https://registry.npmjs.org/halfmoon/-/halfmoon-1.1.0.tgz",
|
"resolved": "https://registry.npmjs.org/halfmoon/-/halfmoon-1.1.1.tgz",
|
||||||
"integrity": "sha512-KgZvQLSvSbu+6foGatWcQHtlkCbmDqn43I7F/DCvdSomDrNCz0Vv3qD4JvHjBbx2IhN9B20u2MNE9yyZPOEQfQ==",
|
"integrity": "sha512-w/ydT3CixxxxdJP4a3sqSzUnCJFmhvcqE2vQOIsWCfevpqanKlbGQNUwKJH0nKvo13M89eetM3R0gsTOfNLGHA==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"handlebars": {
|
"handlebars": {
|
||||||
@ -2956,9 +2956,9 @@
|
|||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"rollup": {
|
"rollup": {
|
||||||
"version": "2.28.2",
|
"version": "2.29.0",
|
||||||
"resolved": "https://registry.npmjs.org/rollup/-/rollup-2.28.2.tgz",
|
"resolved": "https://registry.npmjs.org/rollup/-/rollup-2.29.0.tgz",
|
||||||
"integrity": "sha512-8txbsFBFLmm9Xdt4ByTOGa9Muonmc8MfNjnGAR8U8scJlF1ZW7AgNZa7aqBXaKtlvnYP/ab++fQIq9dB9NWUbg==",
|
"integrity": "sha512-gtU0sjxMpsVlpuAf4QXienPmUAhd6Kc7owQ4f5lypoxBW18fw2UNYZ4NssLGsri6WhUZkE/Ts3EMRebN+gNLiQ==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"fsevents": "~2.1.2"
|
"fsevents": "~2.1.2"
|
||||||
@ -3403,9 +3403,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"svelte-preprocess": {
|
"svelte-preprocess": {
|
||||||
"version": "4.3.2",
|
"version": "4.5.1",
|
||||||
"resolved": "https://registry.npmjs.org/svelte-preprocess/-/svelte-preprocess-4.3.2.tgz",
|
"resolved": "https://registry.npmjs.org/svelte-preprocess/-/svelte-preprocess-4.5.1.tgz",
|
||||||
"integrity": "sha512-CmIsCr62y34qGS10/SC1l1VkmX0kZR6wncbGgRJ1qJftLDMEaazC3bWqoqAlrqyQFvpO0+xb44GQm4RKi/9sLQ==",
|
"integrity": "sha512-fZiLMg+mJzp5y4bsvBtl6wE1WCp+s5L87BoKMONGLXk8HSZD5HuRJzxhM0yhM9LHF0jP5kYG22P2Vc/vrv4I0A==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"@types/pug": "^2.0.4",
|
"@types/pug": "^2.0.4",
|
||||||
|
@ -10,12 +10,12 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@rollup/plugin-commonjs": "^11.0.0",
|
"@rollup/plugin-commonjs": "^11.0.0",
|
||||||
"@rollup/plugin-node-resolve": "^7.0.0",
|
"@rollup/plugin-node-resolve": "^7.0.0",
|
||||||
"@wails/runtime": "^1.0.0",
|
"@wails/runtime": "^1.0.1",
|
||||||
"focus-visible": "^5.0.2",
|
"focus-visible": "^5.0.2",
|
||||||
"halfmoon": "^1.1.0",
|
"halfmoon": "^1.1.1",
|
||||||
"postcss": "^8.1.1",
|
"postcss": "^8.1.1",
|
||||||
"postcss-import": "^12.0.1",
|
"postcss-import": "^12.0.1",
|
||||||
"rollup": "^2.0.0",
|
"rollup": "^2.29.0",
|
||||||
"rollup-plugin-livereload": "^1.0.0",
|
"rollup-plugin-livereload": "^1.0.0",
|
||||||
"rollup-plugin-postcss": "^3.1.8",
|
"rollup-plugin-postcss": "^3.1.8",
|
||||||
"rollup-plugin-string": "^3.0.0",
|
"rollup-plugin-string": "^3.0.0",
|
||||||
@ -24,7 +24,7 @@
|
|||||||
"sirv-cli": "^0.4.4",
|
"sirv-cli": "^0.4.4",
|
||||||
"svelte": "^3.0.0",
|
"svelte": "^3.0.0",
|
||||||
"svelte-highlight": "^0.6.2",
|
"svelte-highlight": "^0.6.2",
|
||||||
"svelte-preprocess": "^4.3.2"
|
"svelte-preprocess": "^4.5.1"
|
||||||
},
|
},
|
||||||
"dependencies": {}
|
"dependencies": {}
|
||||||
}
|
}
|
||||||
|
@ -5,11 +5,11 @@
|
|||||||
import jsCode from './code.jsx';
|
import jsCode from './code.jsx';
|
||||||
import goCode from './code.go';
|
import goCode from './code.go';
|
||||||
|
|
||||||
var loglevel = 'Trace';
|
|
||||||
var message = '';
|
var message = '';
|
||||||
var isJs = false;
|
var isJs = false;
|
||||||
|
|
||||||
var options = ["Trace", "Debug", "Info", "Warning", "Error", "Fatal"];
|
var options = ["Print", "Trace", "Debug", "Info", "Warning", "Error", "Fatal"];
|
||||||
|
var loglevel = options[0];
|
||||||
|
|
||||||
$: lang = isJs ? 'Javascript' : 'Go';
|
$: lang = isJs ? 'Javascript' : 'Go';
|
||||||
|
|
||||||
@ -33,7 +33,7 @@
|
|||||||
|
|
||||||
<ul class="list">
|
<ul class="list">
|
||||||
{#each options as option}
|
{#each options as option}
|
||||||
<li>{options}</li>
|
<li>{option}</li>
|
||||||
{/each}
|
{/each}
|
||||||
</ul>
|
</ul>
|
||||||
All methods will log to the console and <code>Fatal</code> will also exit the program.
|
All methods will log to the console and <code>Fatal</code> will also exit the program.
|
||||||
|
@ -8,6 +8,8 @@ type MyStruct struct {
|
|||||||
|
|
||||||
func (l *MyStruct) WailsInit(runtime *wails.Runtime) error {
|
func (l *MyStruct) WailsInit(runtime *wails.Runtime) error {
|
||||||
|
|
||||||
|
runtime.Log.Print(message)
|
||||||
|
runtime.Log.Trace(message)
|
||||||
runtime.Log.Debug(message)
|
runtime.Log.Debug(message)
|
||||||
runtime.Log.Info(message)
|
runtime.Log.Info(message)
|
||||||
runtime.Log.Warning(message)
|
runtime.Log.Warning(message)
|
||||||
|
@ -3,6 +3,7 @@ import { Log } from '@wails/runtime';
|
|||||||
function doSomeOperation() {
|
function doSomeOperation() {
|
||||||
// Do things
|
// Do things
|
||||||
let value = doSomething();
|
let value = doSomething();
|
||||||
|
Log.Print("A raw message");
|
||||||
Log.Trace("I got: " + value);
|
Log.Trace("I got: " + value);
|
||||||
Log.Debug("A debug message");
|
Log.Debug("A debug message");
|
||||||
Log.Info("An Info message");
|
Log.Info("An Info message");
|
||||||
|
@ -16,6 +16,11 @@ func (l *Logger) WailsInit(runtime *wails.Runtime) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Print will log the given message
|
||||||
|
func (l *Logger) Print(message string) {
|
||||||
|
l.runtime.Log.Print(message)
|
||||||
|
}
|
||||||
|
|
||||||
// Trace will log the given message
|
// Trace will log the given message
|
||||||
func (l *Logger) Trace(message string) {
|
func (l *Logger) Trace(message string) {
|
||||||
l.runtime.Log.Trace(message)
|
l.runtime.Log.Trace(message)
|
||||||
|
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
wails "github.com/wailsapp/wails/v2"
|
wails "github.com/wailsapp/wails/v2"
|
||||||
|
"github.com/wailsapp/wails/v2/pkg/logger"
|
||||||
"github.com/wailsapp/wails/v2/pkg/options"
|
"github.com/wailsapp/wails/v2/pkg/options"
|
||||||
"github.com/wailsapp/wails/v2/pkg/options/mac"
|
"github.com/wailsapp/wails/v2/pkg/options/mac"
|
||||||
)
|
)
|
||||||
@ -22,6 +23,7 @@ func main() {
|
|||||||
WindowBackgroundIsTranslucent: true,
|
WindowBackgroundIsTranslucent: true,
|
||||||
TitleBar: mac.TitleBarHiddenInset(),
|
TitleBar: mac.TitleBarHiddenInset(),
|
||||||
},
|
},
|
||||||
|
LogLevel: logger.INFO,
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Bind(&Logger{})
|
app.Bind(&Logger{})
|
||||||
|
Loading…
Reference in New Issue
Block a user