Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2022 xx foundation //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file. //
////////////////////////////////////////////////////////////////////////////////
//go:build js && wasm
package logging
import (
"fmt"
"github.com/pkg/errors"
jww "github.com/spf13/jwalterweatherman"
"gitlab.com/elixxir/xxdk-wasm/utils"
"log"
"syscall/js"
)
// LogLevel sets level of logging. All logs at the set level and below will be
// displayed (e.g., when log level is ERROR, only ERROR, CRITICAL, and FATAL
// messages will be printed).
//
// The default log level without updates is INFO.
func LogLevel(threshold jww.Threshold) error {
if threshold < jww.LevelTrace || threshold > jww.LevelFatal {
return errors.Errorf("log level is not valid: log level: %d", threshold)
}
jww.SetLogThreshold(threshold)
jww.SetFlags(log.LstdFlags | log.Lmicroseconds)
ll := NewJsConsoleLogListener(threshold)
AddLogListener(ll.Listen)
jww.SetStdoutThreshold(jww.LevelFatal + 1)
msg := fmt.Sprintf("Log level set to: %s", threshold)
switch threshold {
case jww.LevelTrace:
fallthrough
case jww.LevelDebug:
fallthrough
case jww.LevelInfo:
jww.INFO.Print(msg)
case jww.LevelWarn:
jww.WARN.Print(msg)
case jww.LevelError:
jww.ERROR.Print(msg)
case jww.LevelCritical:
jww.CRITICAL.Print(msg)
case jww.LevelFatal:
jww.FATAL.Print(msg)
}
return nil
}
// LogLevelJS sets level of logging. All logs at the set level and below will be
// displayed (e.g., when log level is ERROR, only ERROR, CRITICAL, and FATAL
// messages will be printed).
//
// Log level options:
//
// TRACE - 0
// DEBUG - 1
// INFO - 2
// WARN - 3
// ERROR - 4
// CRITICAL - 5
// FATAL - 6
//
// The default log level without updates is INFO.
//
// Parameters:
// - args[0] - Log level (int).
//
// Returns:
// - Throws TypeError if the log level is invalid.
func LogLevelJS(_ js.Value, args []js.Value) any {
threshold := jww.Threshold(args[0].Int())
err := LogLevel(threshold)
if err != nil {
utils.Throw(utils.TypeError, err)
return nil
}
return nil
}