Skip to content
Snippets Groups Projects
Commit 45c57d9a authored by Jono Wenger's avatar Jono Wenger
Browse files

Fix log listener to not create a new object on every listen

parent 2a5aa09a
No related branches found
No related tags found
1 merge request!6XX-4050 / Send E2E test
This commit is part of merge request !6. Comments created here will be created in the context of that merge request.
...@@ -56,7 +56,7 @@ func LogLevel(_ js.Value, args []js.Value) interface{} { ...@@ -56,7 +56,7 @@ func LogLevel(_ js.Value, args []js.Value) interface{} {
jww.SetLogThreshold(threshold) jww.SetLogThreshold(threshold)
jww.SetFlags(log.LstdFlags | log.Lmicroseconds) jww.SetFlags(log.LstdFlags | log.Lmicroseconds)
ll := &LogListener{threshold, js.Global().Get("console")} ll := NewLogListener(threshold, js.Global().Get("console"))
logListeners = append(logListeners, ll.Listen) logListeners = append(logListeners, ll.Listen)
jww.SetLogListeners(logListeners...) jww.SetLogListeners(logListeners...)
jww.SetStdoutThreshold(jww.LevelFatal + 1) jww.SetStdoutThreshold(jww.LevelFatal + 1)
...@@ -184,6 +184,32 @@ func (c *console) Write(p []byte) (n int, err error) { ...@@ -184,6 +184,32 @@ func (c *console) Write(p []byte) (n int, err error) {
type LogListener struct { type LogListener struct {
jww.Threshold jww.Threshold
js.Value js.Value
trace *console
debug *console
info *console
error *console
warn *console
critical *console
fatal *console
def *console
}
// NewLogListener initialises a new log listener that listener for the specific
// threshold and prints the logs to the Javascript console.
func NewLogListener(threshold jww.Threshold, consoleObj js.Value) *LogListener {
return &LogListener{
Threshold: threshold,
Value: consoleObj,
trace: &console{"debug", consoleObj},
debug: &console{"log", consoleObj},
info: &console{"info", consoleObj},
warn: &console{"warn", consoleObj},
error: &console{"error", consoleObj},
critical: &console{"error", consoleObj},
fatal: &console{"error", consoleObj},
def: &console{"log", consoleObj},
}
} }
// Listen is called for every logging event. This function adheres to the // Listen is called for every logging event. This function adheres to the
...@@ -195,21 +221,21 @@ func (ll *LogListener) Listen(t jww.Threshold) io.Writer { ...@@ -195,21 +221,21 @@ func (ll *LogListener) Listen(t jww.Threshold) io.Writer {
switch t { switch t {
case jww.LevelTrace: case jww.LevelTrace:
return &console{"debug", ll.Value} return ll.trace
case jww.LevelDebug: case jww.LevelDebug:
return &console{"log", ll.Value} return ll.debug
case jww.LevelInfo: case jww.LevelInfo:
return &console{"info", ll.Value} return ll.info
case jww.LevelWarn: case jww.LevelWarn:
return &console{"warn", ll.Value} return ll.warn
case jww.LevelError: case jww.LevelError:
return &console{"error", ll.Value} return ll.error
case jww.LevelCritical: case jww.LevelCritical:
return &console{"error", ll.Value} return ll.critical
case jww.LevelFatal: case jww.LevelFatal:
return &console{"error", ll.Value} return ll.fatal
default: default:
return &console{"log", ll.Value} return ll.def
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment