Skip to content

Commit

Permalink
Added log level option configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Omer Aplatony <omerap12@gmail.com>
  • Loading branch information
omerap12 committed Jul 12, 2024
1 parent 3575ba6 commit 2dbbddd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
4 changes: 2 additions & 2 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
const (
// EnvVarKubeConfig is the path to the Kubernetes configuration
EnvVarKubeConfig = "KUBECONFIG"
// EnvVarDebugLog is the env var to turn on the debug mode for logging
EnvVarDebugLog = "DEBUG_LOG"
// EnvVarLogLog is the env var to select the log level - options are debug, info, error
EnvVarLogLevel = "LOG_LEVEL"
// ENVVarPodName should be set to the name of the pod
EnvVarPodName = "POD_NAME"
// ENVVarLeaderElection sets the leader election mode
Expand Down
28 changes: 21 additions & 7 deletions common/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,15 @@ const (
LabelHTTPMethod = "http-method"
LabelTime = "time"
TimestampFormat = "2006-01-02 15:04:05"
InfoLevel = "info"
DebugLevel = "debug"
ErrorLevel = "error"
)

// NewArgoEventsLogger returns a new ArgoEventsLogger
func NewArgoEventsLogger() *zap.SugaredLogger {
var config zap.Config
debugMode, ok := os.LookupEnv(common.EnvVarDebugLog)
if ok && debugMode == "true" {
config = zap.NewDevelopmentConfig()
} else {
config = zap.NewProductionConfig()
}
logLevel, _ := os.LookupEnv(common.EnvVarLogLevel)
config := ConfigureLogLevelLogger(logLevel)
// Config customization goes here if any
config.OutputPaths = []string{"stdout"}
logger, err := config.Build()
Expand Down Expand Up @@ -81,3 +79,19 @@ func FromContext(ctx context.Context) *zap.SugaredLogger {
}
return NewArgoEventsLogger()
}

// Returns logger conifg depending on the log level
func ConfigureLogLevelLogger(logLevel string) zap.Config {
logConfig := zap.NewProductionConfig()
switch logLevel {
case InfoLevel:
logConfig.Level = zap.NewAtomicLevelAt(zap.InfoLevel)
case ErrorLevel:
logConfig.Level = zap.NewAtomicLevelAt(zap.ErrorLevel)
case DebugLevel:
logConfig.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
default:
logConfig.Level = zap.NewAtomicLevelAt(zap.InfoLevel)
}
return logConfig
}
2 changes: 1 addition & 1 deletion docs/FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ with the desired namespace and service account. Make sure to grant the service a
* Make sure you have configured the event source correctly.
* Check the event-source pod's containers logs.

Note: You can set the environment variable `DEBUG_LOG:true` in any of the containers to output debug logs. See [here](https://github.com/argoproj/argo-events/blob/master/examples/sensors/log-debug.yaml) for a debug example.
Note: You can set the environment variable `LOG_LEVEL:info/debug/error` in any of the containers to output debug logs. See [here](https://github.com/argoproj/argo-events/blob/master/examples/sensors/log-debug.yaml) for a debug example.

**Q. The event-source pod is receiving events but nothing happens.**

Expand Down
4 changes: 2 additions & 2 deletions examples/sensors/log-debug.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ spec:
template:
container:
env:
- name: DEBUG_LOG
value: "true"
- name: LOG_LEVEL
value: error
dependencies:
- name: test-dep
eventSourceName: calendar
Expand Down

0 comments on commit 2dbbddd

Please sign in to comment.