Skip to content

Commit 6fd722f

Browse files
committed
configure log level
1 parent 65f0e38 commit 6fd722f

File tree

2 files changed

+53
-25
lines changed

2 files changed

+53
-25
lines changed

cmd/root.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"os"
3131
"os/exec"
3232
"os/signal"
33+
"strings"
3334
"syscall"
3435

3536
"github.com/perfana/x2i/gatlingparser"
@@ -111,6 +112,15 @@ var rootCmd = &cobra.Command{
111112
// This is called by main.main(). It only needs to happen once to the rootCmd.
112113
func Execute() {
113114
// Initiating logger before any other processes start
115+
level, _ := rootCmd.Flags().GetString("log-level")
116+
switch strings.ToUpper(level) {
117+
case "DEBUG":
118+
l.SetLogLevel(l.DEBUG)
119+
case "INFO":
120+
l.SetLogLevel(l.INFO)
121+
default:
122+
l.SetLogLevel(l.ERROR)
123+
}
114124
logPath, _ := rootCmd.Flags().GetString("log")
115125
err := l.InitLogger(logPath)
116126
if err != nil {
@@ -132,7 +142,8 @@ func init() {
132142
rootCmd.Flags().StringP("password", "p", "", "Password credential for InfluxDB instance")
133143
rootCmd.Flags().StringP("database", "b", "", "Database name in InfluxDB")
134144
rootCmd.Flags().StringP("testtool", "i", "gatling", "Testtool used, can be gatling, jmeter or k6")
135-
rootCmd.Flags().StringP("log", "l", "x2i.log", "File path to x2i log file")
145+
rootCmd.PersistentFlags().StringP("log-level", "l", "INFO", "Set log level (DEBUG, INFO, ERROR)")
146+
rootCmd.Flags().StringP("log", "o", "x2i.log", "Log file path")
136147
rootCmd.Flags().StringP("test-environment", "t", "", "Test environment identifier")
137148
rootCmd.Flags().StringP("system-under-test", "y", "", "System under test identifier")
138149
rootCmd.Flags().UintP("stop-timeout", "s", 120, "Time (seconds) to exit if no new log lines found")
@@ -141,3 +152,5 @@ func init() {
141152
// set up global context
142153
ctx, cancel = context.WithCancel(context.Background())
143154
}
155+
156+

logger/logger.go

+39-24
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,19 @@ import (
3131
"path/filepath"
3232
)
3333

34-
// that will add prefixes to log lines
34+
type LogLevel int
35+
36+
const (
37+
DEBUG LogLevel = iota
38+
INFO
39+
ERROR
40+
)
41+
3542
var (
36-
// logger is a single local logger implementation
37-
logger *log.Logger
38-
sw io.Writer
39-
ew io.Writer
43+
logger *log.Logger
44+
sw io.Writer
45+
ew io.Writer
46+
logLevel LogLevel = INFO // Default to INFO level
4047
)
4148

4249
// InitLogger sets up a new instance of logger that writes to file and STDOUT
@@ -62,44 +69,52 @@ func InitLogger(fileName string) error {
6269
return nil
6370
}
6471

65-
// Errorln writes a line to STDERR and log file prepending message with ERROR
72+
func SetLogLevel(level LogLevel) {
73+
logLevel = level
74+
}
75+
6676
func Errorln(v ...interface{}) {
77+
// ERROR level always shows
6778
logger.SetOutput(ew)
6879
logger.SetPrefix("ERROR ")
6980
logger.Println(v...)
7081
}
7182

72-
// Errorf writes a formatted output to STDERR and log file prepending message with ERROR
7383
func Errorf(format string, v ...interface{}) {
84+
// ERROR level always shows
7485
logger.SetOutput(ew)
7586
logger.SetPrefix("ERROR ")
7687
logger.Printf(format, v...)
7788
}
7889

79-
// Infoln writes a line to STDERR and log file prepending message with INFO
8090
func Infoln(v ...interface{}) {
81-
logger.SetOutput(sw)
82-
logger.SetPrefix("INFO ")
83-
logger.Println(v...)
91+
if logLevel >= INFO {
92+
logger.SetOutput(sw)
93+
logger.SetPrefix("INFO ")
94+
logger.Println(v...)
95+
}
8496
}
8597

86-
// Infof writes a formatted output to STDERR and log file prepending message with INFO
8798
func Infof(format string, v ...interface{}) {
88-
logger.SetOutput(sw)
89-
logger.SetPrefix("INFO ")
90-
logger.Printf(format, v...)
99+
if logLevel >= INFO {
100+
logger.SetOutput(sw)
101+
logger.SetPrefix("INFO ")
102+
logger.Printf(format, v...)
103+
}
91104
}
92105

93-
// Debugln writes a line to STDERR and log file prepending message with DEBUG
94106
func Debugln(v ...interface{}) {
95-
logger.SetOutput(sw)
96-
logger.SetPrefix("DEBUG ")
97-
logger.Println(v...)
107+
if logLevel <= DEBUG {
108+
logger.SetOutput(sw)
109+
logger.SetPrefix("DEBUG ")
110+
logger.Println(v...)
111+
}
98112
}
99113

100-
// Debugf writes a formatted output to STDERR and log file prepending message with DEBUG
101114
func Debugf(format string, v ...interface{}) {
102-
logger.SetOutput(sw)
103-
logger.SetPrefix("DEBUG ")
104-
logger.Printf(format, v...)
105-
}
115+
if logLevel <= DEBUG {
116+
logger.SetOutput(sw)
117+
logger.SetPrefix("DEBUG ")
118+
logger.Printf(format, v...)
119+
}
120+
}

0 commit comments

Comments
 (0)