Skip to content

Commit 655445a

Browse files
add a way to create a new root logger (#273)
1 parent 0f32d7a commit 655445a

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

Diff for: support/log/logger.go

+32-15
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,20 @@ import (
66
"strings"
77
)
88

9-
type Level int
10-
type Format int
9+
// Constants for log level
10+
const (
11+
TraceLevel Level = iota
12+
DebugLevel
13+
InfoLevel
14+
WarnLevel
15+
ErrorLevel
16+
)
17+
18+
// Constants for log format
19+
const (
20+
FormatConsole Format = iota
21+
FormatJson
22+
)
1123

1224
const (
1325
EnvKeyLogCtx = "FLOGO_LOG_CTX"
@@ -20,17 +32,11 @@ const (
2032

2133
EnvKeyLogSeparator = "FLOGO_LOG_SEPARATOR"
2234
DefaultLogSeparator = "\t"
23-
24-
TraceLevel Level = iota
25-
DebugLevel
26-
InfoLevel
27-
WarnLevel
28-
ErrorLevel
29-
30-
FormatConsole Format = iota
31-
FormatJson
3235
)
3336

37+
type Level int
38+
type Format int
39+
3440
type Logger interface {
3541
DebugEnabled() bool
3642
TraceEnabled() bool
@@ -121,6 +127,20 @@ func CreateLoggerFromRef(logger Logger, contributionType, ref string) Logger {
121127
}
122128
}
123129

130+
// NewLogger will create a new zap logger with same log format as engine logger
131+
func NewLogger(name string) Logger {
132+
logFormat := DefaultLogFormat
133+
envLogFormat := strings.ToUpper(os.Getenv(EnvKeyLogFormat))
134+
if envLogFormat == "JSON" {
135+
logFormat = FormatJson
136+
}
137+
zl, lvl, _ := newZapLogger(logFormat, DefaultLogLevel)
138+
if name == "" {
139+
name = "flogo.custom"
140+
}
141+
return &zapLoggerImpl{loggerLevel: lvl, mainLogger: zl.Named(name).Sugar()}
142+
}
143+
124144
func Sync() {
125145
zapSync(rootLogger)
126146
}
@@ -163,10 +183,8 @@ func configureLogging() {
163183
}
164184

165185
func ToLogLevel(lvlStr string) Level {
166-
167186
lvl := DefaultLogLevel
168-
169-
switch lvlStr {
187+
switch strings.ToUpper(lvlStr) {
170188
case "TRACE":
171189
lvl = DebugLevel
172190
case "DEBUG":
@@ -180,7 +198,6 @@ func ToLogLevel(lvlStr string) Level {
180198
default:
181199
lvl = DefaultLogLevel
182200
}
183-
184201
return lvl
185202
}
186203

0 commit comments

Comments
 (0)