Skip to content

Commit c8c6773

Browse files
Merge pull request #507 from petrkotas/log-refactor
Moves environment variables to the beggining of the investigation and interceptor
2 parents 512c8c4 + eaf521e commit c8c6773

File tree

4 files changed

+27
-38
lines changed

4 files changed

+27
-38
lines changed

cadctl/cmd/investigate/investigate.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,28 @@ var InvestigateCmd = &cobra.Command{
4444
}
4545

4646
var (
47-
logLevelFlag = ""
48-
payloadPath = "./payload.json"
47+
logLevelFlag = ""
48+
payloadPath = "./payload.json"
49+
pipelineNameEnv = ""
4950
)
5051

5152
const pagerdutyTitlePrefix = "[CAD Investigated]"
5253

5354
func init() {
54-
InvestigateCmd.Flags().StringVarP(&payloadPath, "payload-path", "p", payloadPath, "the path to the payload")
55-
InvestigateCmd.Flags().StringVarP(&logging.LogLevelString, "log-level", "l", "", "the log level [debug,info,warn,error,fatal], default = info")
55+
InvestigateCmd.Flags().StringVarP(&payloadPath, "payload-path", "p", payloadPath, "the path to the payload, defaults to './payload.json'")
56+
InvestigateCmd.Flags().StringVarP(&logLevelFlag, "log-level", "l", "", "the log level [debug,info,warn,error,fatal], default = info")
5657

57-
err := InvestigateCmd.MarkFlagRequired("payload-path")
58-
if err != nil {
59-
logging.Warn("Could not mark flag 'payload-path' as required")
58+
if envLogLevel, exists := os.LookupEnv("LOG_LEVEL"); exists {
59+
logLevelFlag = envLogLevel
6060
}
61+
62+
pipelineNameEnv = os.Getenv("PIPELINE_NAME")
6163
}
6264

6365
func run(cmd *cobra.Command, _ []string) error {
6466
// early init of logger for logs before clusterID is known
65-
if cmd.Flags().Changed("log-level") {
66-
flagValue, _ := cmd.Flags().GetString("log-level")
67-
logging.RawLogger = logging.InitLogger(flagValue, "")
68-
}
67+
logging.RawLogger = logging.InitLogger(logLevelFlag, pipelineNameEnv, "")
68+
6969
payload, err := os.ReadFile(payloadPath)
7070
if err != nil {
7171
return fmt.Errorf("failed to read webhook payload: %w", err)
@@ -129,12 +129,7 @@ func run(cmd *cobra.Command, _ []string) error {
129129
internalClusterID := cluster.ID()
130130

131131
// re-initialize logger for the internal-cluster-id context
132-
// if log-level flag is set, take priority over env + default
133-
if cmd.Flags().Changed("log-level") {
134-
logging.RawLogger = logging.InitLogger(logLevelFlag, internalClusterID)
135-
} else {
136-
logging.RawLogger = logging.InitLogger(logging.LogLevelString, internalClusterID)
137-
}
132+
logging.RawLogger = logging.InitLogger(logLevelFlag, pipelineNameEnv, internalClusterID)
138133

139134
requiresInvestigation, err := clusterRequiresInvestigation(cluster, pdClient, ocmClient)
140135
if err != nil || !requiresInvestigation {

interceptor/main.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,19 @@ const (
2424
idleTimeout = 60 * time.Second
2525
)
2626

27-
var logger = logging.InitLogger(logging.LogLevelString, "")
27+
var (
28+
pipelineNameEnv = ""
29+
logLevelEnv = "info"
30+
)
31+
32+
func init() {
33+
pipelineNameEnv = os.Getenv("PIPELINE_NAME")
34+
logLevelEnv = os.Getenv("LOG_LEVEL")
35+
}
2836

2937
func main() {
38+
logger := logging.InitLogger(logLevelEnv, pipelineNameEnv, "")
39+
3040
// set up signals so we handle the first shutdown signal gracefully
3141
ctx := signals.NewContext()
3242

pkg/investigations/chgm/chgm_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var _ = Describe("chgm", func() {
3737
event cloudtrailv2types.Event
3838
)
3939
BeforeEach(func() {
40-
logging.InitLogger("fatal", "") // Mute logger for the tests
40+
logging.InitLogger("fatal", "", "") // Mute logger for the tests
4141
mockCtrl = gomock.NewController(GinkgoT())
4242

4343
var err error

pkg/logging/logging.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,23 @@
22
package logging
33

44
import (
5-
"fmt"
65
"log"
7-
"os"
86

97
"go.uber.org/zap"
108
"go.uber.org/zap/zapcore"
119
)
1210

13-
var LogLevelString = getLogLevel()
14-
1511
// RawLogger is the raw global logger object used for calls wrapped by the logging package
16-
var RawLogger = InitLogger(LogLevelString, "")
12+
// empty string defaults to "info" log level
13+
var RawLogger = InitLogger("", "", "")
1714

1815
// InitLogger initializes a cluster-id specific child logger
19-
func InitLogger(logLevelString string, clusterID string) *zap.SugaredLogger {
16+
func InitLogger(logLevelString string, pipelineName string, clusterID string) *zap.SugaredLogger {
2017
logLevel, err := zap.ParseAtomicLevel(logLevelString)
2118
if err != nil {
2219
log.Fatalln("Invalid log level:", logLevelString)
2320
}
2421

25-
pipelineName := os.Getenv("PIPELINE_NAME")
26-
if pipelineName == "" {
27-
fmt.Println("Warning: Unable to retrieve the pipeline ID on logger creation. Continuing with empty value.")
28-
}
29-
3022
config := zap.NewProductionConfig()
3123
config.EncoderConfig.TimeKey = "timestamp"
3224
config.Level = logLevel
@@ -94,11 +86,3 @@ func Errorf(template string, args ...interface{}) {
9486
func Fatalf(template string, args ...interface{}) {
9587
RawLogger.Fatalf(template, args...)
9688
}
97-
98-
// getLogLevel returns the log level from the environment variable LOG_LEVEL
99-
func getLogLevel() string {
100-
if envLogLevel, exists := os.LookupEnv("LOG_LEVEL"); exists {
101-
return envLogLevel
102-
}
103-
return "info"
104-
}

0 commit comments

Comments
 (0)