Skip to content

Commit 205b977

Browse files
Add logger utility with configurable log levels
Implement a logger utility with various log levels.
1 parent 2ad4e3a commit 205b977

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

pkg/utils/logger.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
Copyright © 2021 The LitmusChaos Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package utils
17+
18+
import (
19+
"os"
20+
21+
"github.com/litmuschaos/litmusctl/pkg/config"
22+
"github.com/sirupsen/logrus"
23+
)
24+
25+
var Log *logrus.Logger
26+
27+
func init() {
28+
Log = logrus.New()
29+
Log.SetOutput(os.Stdout)
30+
Log.SetFormatter(&logrus.TextFormatter{
31+
DisableColors: false,
32+
FullTimestamp: false,
33+
})
34+
// Default to Error level; will be set to Debug when verbose mode is enabled
35+
Log.SetLevel(logrus.ErrorLevel)
36+
}
37+
38+
// ConfigureVerboseLogging configures the logger based on verbose mode
39+
func ConfigureVerboseLogging() {
40+
if config.VerboseMode {
41+
Log.SetLevel(logrus.DebugLevel)
42+
Log.Debug("Verbose mode enabled")
43+
} else {
44+
Log.SetLevel(logrus.ErrorLevel)
45+
}
46+
}
47+
48+
// Debug logs a message at debug level (only shown in verbose mode)
49+
func Debug(args ...interface{}) {
50+
if config.VerboseMode {
51+
Log.Debug(args...)
52+
}
53+
}
54+
55+
// Debugf logs a formatted message at debug level (only shown in verbose mode)
56+
func Debugf(format string, args ...interface{}) {
57+
if config.VerboseMode {
58+
Log.Debugf(format, args...)
59+
}
60+
}
61+
62+
// Info logs a message at info level (only shown in verbose mode)
63+
func Info(args ...interface{}) {
64+
if config.VerboseMode {
65+
Log.Info(args...)
66+
}
67+
}
68+
69+
// Infof logs a formatted message at info level (only shown in verbose mode)
70+
func Infof(format string, args ...interface{}) {
71+
if config.VerboseMode {
72+
Log.Infof(format, args...)
73+
}
74+
}
75+
76+
// Warn logs a warning message (shown regardless of verbose mode)
77+
func Warn(args ...interface{}) {
78+
Log.Warn(args...)
79+
}
80+
81+
// Warnf logs a formatted warning message (shown regardless of verbose mode)
82+
func Warnf(format string, args ...interface{}) {
83+
Log.Warnf(format, args...)
84+
}
85+
86+
// Error logs an error message (shown regardless of verbose mode)
87+
func Error(args ...interface{}) {
88+
Log.Error(args...)
89+
}
90+
91+
// Errorf logs a formatted error message (shown regardless of verbose mode)
92+
func Errorf(format string, args ...interface{}) {
93+
Log.Errorf(format, args...)
94+
}

0 commit comments

Comments
 (0)