forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.go
91 lines (75 loc) · 1.91 KB
/
console.go
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
90
91
package js
import (
"encoding/json"
"os"
"strings"
"github.com/dop251/goja"
"github.com/sirupsen/logrus"
)
// console represents a JS console implemented as a logrus.FieldLogger.
type console struct {
logger logrus.FieldLogger
}
// Creates a console with the standard logrus logger.
func newConsole(logger logrus.FieldLogger) *console {
return &console{logger.WithField("source", "console")}
}
// Creates a console logger with its output set to the file at the provided `filepath`.
func newFileConsole(filepath string, formatter logrus.Formatter, level logrus.Level) (*console, error) {
//nolint:gosec,forbidigo // see https://github.com/grafana/k6/issues/2565
f, err := os.OpenFile(filepath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0o644)
if err != nil {
return nil, err
}
l := logrus.New()
l.SetLevel(level)
l.SetOutput(f)
l.SetFormatter(formatter)
return &console{l}, nil
}
func (c console) log(level logrus.Level, args ...goja.Value) {
var strs strings.Builder
for i := 0; i < len(args); i++ {
if i > 0 {
strs.WriteString(" ")
}
strs.WriteString(c.valueString(args[i]))
}
msg := strs.String()
switch level { //nolint:exhaustive
case logrus.DebugLevel:
c.logger.Debug(msg)
case logrus.InfoLevel:
c.logger.Info(msg)
case logrus.WarnLevel:
c.logger.Warn(msg)
case logrus.ErrorLevel:
c.logger.Error(msg)
}
}
func (c console) Log(args ...goja.Value) {
c.Info(args...)
}
func (c console) Debug(args ...goja.Value) {
c.log(logrus.DebugLevel, args...)
}
func (c console) Info(args ...goja.Value) {
c.log(logrus.InfoLevel, args...)
}
func (c console) Warn(args ...goja.Value) {
c.log(logrus.WarnLevel, args...)
}
func (c console) Error(args ...goja.Value) {
c.log(logrus.ErrorLevel, args...)
}
func (c console) valueString(v goja.Value) string {
mv, ok := v.(json.Marshaler)
if !ok {
return v.String()
}
b, err := json.Marshal(mv)
if err != nil {
return v.String()
}
return string(b)
}