Skip to content

Commit d320201

Browse files
feature: improve logger initialization and usability - Add explicit logger initialization at startup - Make logger fields parameter optional for all log levels - Enable debug level logging by default - Remove console logging fallback for cleaner behavior (#102)
1 parent a65f9cd commit d320201

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

cli-v2.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,25 @@ import (
44
"codacy/cli-v2/cmd"
55
"codacy/cli-v2/config"
66
config_file "codacy/cli-v2/config-file"
7+
"codacy/cli-v2/utils/logger"
78
"fmt"
89
"os"
10+
11+
"github.com/sirupsen/logrus"
912
)
1013

1114
func main() {
1215
// Initialize config global object
1316
config.Init()
1417

18+
// Initialize logger
19+
if err := logger.Initialize(&config.Config); err != nil {
20+
fmt.Printf("Failed to initialize logger: %v\n", err)
21+
}
22+
23+
// Log startup message
24+
logger.Debug("Starting Codacy CLI.", logrus.Fields{})
25+
1526
// This also setup the config global !
1627
configErr := config_file.ReadConfigFile(config.Config.ProjectConfigFile())
1728

@@ -31,7 +42,17 @@ func main() {
3142

3243
// All other commands require a configuration file
3344
if configErr != nil && len(os.Args) > 1 {
34-
fmt.Println("No configuration file was found, execute init command first.")
45+
if os.IsNotExist(configErr) {
46+
message := "No configuration file was found, execute init command first."
47+
logger.Info(message)
48+
fmt.Println(message)
49+
} else {
50+
logger.Error("Configuration error", logrus.Fields{
51+
"error": configErr.Error(),
52+
})
53+
fmt.Printf("Failed to parse configuration file: %v\n", configErr)
54+
fmt.Println("Please check the file format and try again, or run init command to create a new configuration.")
55+
}
3556
return
3657
}
3758

utils/logger/logger.go

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ func Initialize(conf *config.ConfigType) error {
7575
// Set up log rotation using lumberjack
7676
logFile := filepath.Join(logsDir, "codacy-cli.log")
7777

78+
// Try to create/open the log file to test permissions
79+
f, err := os.OpenFile(logFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, utils.DefaultFilePerms)
80+
if err != nil {
81+
return fmt.Errorf("failed to create/open log file: %w", err)
82+
}
83+
f.Close()
84+
7885
lumberjackLogger := &lumberjack.Logger{
7986
Filename: logFile,
8087
MaxSize: 10, // megabytes
@@ -86,7 +93,7 @@ func Initialize(conf *config.ConfigType) error {
8693
// Configure logrus to use our custom formatter
8794
fileLogger.SetFormatter(&CustomTextFormatter{})
8895
fileLogger.SetOutput(lumberjackLogger)
89-
fileLogger.SetLevel(logrus.InfoLevel)
96+
fileLogger.SetLevel(logrus.DebugLevel)
9097

9198
// Enable caller information for file location
9299
fileLogger.SetReportCaller(true)
@@ -103,21 +110,37 @@ func Log(level logrus.Level, msg string, fields logrus.Fields) {
103110
}
104111

105112
// Info logs an info level message
106-
func Info(msg string, fields logrus.Fields) {
107-
Log(logrus.InfoLevel, msg, fields)
113+
func Info(msg string, fields ...logrus.Fields) {
114+
var f logrus.Fields
115+
if len(fields) > 0 {
116+
f = fields[0]
117+
}
118+
Log(logrus.InfoLevel, msg, f)
108119
}
109120

110121
// Error logs an error level message
111-
func Error(msg string, fields logrus.Fields) {
112-
Log(logrus.ErrorLevel, msg, fields)
122+
func Error(msg string, fields ...logrus.Fields) {
123+
var f logrus.Fields
124+
if len(fields) > 0 {
125+
f = fields[0]
126+
}
127+
Log(logrus.ErrorLevel, msg, f)
113128
}
114129

115130
// Debug logs a debug level message
116-
func Debug(msg string, fields logrus.Fields) {
117-
Log(logrus.DebugLevel, msg, fields)
131+
func Debug(msg string, fields ...logrus.Fields) {
132+
var f logrus.Fields
133+
if len(fields) > 0 {
134+
f = fields[0]
135+
}
136+
Log(logrus.DebugLevel, msg, f)
118137
}
119138

120139
// Warn logs a warning level message
121-
func Warn(msg string, fields logrus.Fields) {
122-
Log(logrus.WarnLevel, msg, fields)
140+
func Warn(msg string, fields ...logrus.Fields) {
141+
var f logrus.Fields
142+
if len(fields) > 0 {
143+
f = fields[0]
144+
}
145+
Log(logrus.WarnLevel, msg, f)
123146
}

0 commit comments

Comments
 (0)