Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration support for zap logger #52

Merged
merged 9 commits into from
Jun 24, 2024

Conversation

sudiptob2
Copy link
Collaborator

@sudiptob2 sudiptob2 commented Jun 13, 2024

closes #48

Changes:

  • Reading logging configuration for ENV/config file
  • Support for Log level strings DEBUG, INFO, WARN, ERROR, DPANIC, PANIC, FATAL
  • Some housekeeping

@sudiptob2 sudiptob2 force-pushed the feat/48/refine-zap-logger branch 2 times, most recently from a692caf to 9bb1d77 Compare June 13, 2024 21:26
@sudiptob2 sudiptob2 marked this pull request as ready for review June 13, 2024 21:27
@sudiptob2 sudiptob2 requested a review from richscott June 13, 2024 21:29
richscott
richscott previously approved these changes Jun 14, 2024
@dejanzele
Copy link
Collaborator

dejanzele commented Jun 20, 2024

Pods in Kubernetes are ephemeral, and we don't use any persistent volumes to backup log files. In case of a Pod restart, the log files will be lost.

Best practice is to log directly to STDOUT and STDERR and leave log managament, rotation... to Kubernetes.
Kubernetes in any case saves the logs from STD in a file.

No reason to keep a log file or manually do rotation.

@dejanzele
Copy link
Collaborator

It would be nice to add a config option to switch between human-readable logs and JSON logs.
In Kubernetes, JSON logs should be default.

log/logger.go Outdated
MaxBackups: maxBackups,
MaxAge: maxAge,
Compress: compress,
Filename: config.LogFilePath,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check my comment, this is redundant

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

log/logger.go Outdated
logLevel = zap.InfoLevel
)
type LogConfig struct {
LogFilePath string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for all file-related log config

@sudiptob2 sudiptob2 requested a review from dejanzele June 21, 2024 04:08
log/logger.go Outdated
consoleEncoder := zapcore.NewConsoleEncoder(developmentCfg)
fileEncoder := zapcore.NewJSONEncoder(productionCfg)
var encoder zapcore.Encoder
if config.IsProduction {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be simplified:

encoder = zapcore.NewConsoleEncoder(developmentCfg)
if config.IsProduction {
  encoder = zapcore.NewJSONEncoder(productionCfg)
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rename IsProduction to UseJSONLogFormat

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, can we entirely remove the development config? And just keep only one configuration. And then according to the log format flag, we can switch the Encoder

config := zap.NewProductionEncoderConfig()
var encoder zapcore.Encoder
encoder = zapcore.NewConsoleEncoder(config)
if config.IsProduction {
	encoder = zapcore.NewJSONEncoder(config)
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that makes sense, I always try to avoid naming something in an application based on environment name and keep things generic

@sudiptob2 sudiptob2 force-pushed the feat/48/refine-zap-logger branch from ea16d1e to d04494b Compare June 23, 2024 14:59
log:
# -- Output type of the log, if true, log will be output in json format
useJSONFormat: true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we could even shorten it to jsonFormat

@sudiptob2 sudiptob2 requested a review from dejanzele June 24, 2024 15:10
log/logger.go Outdated
@@ -10,8 +10,8 @@ import (
)

type LogConfig struct {
UseJSONFormat bool
LogLevel string
JsonFormat bool
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
JsonFormat bool
JSONFormat bool

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

@sudiptob2 sudiptob2 requested a review from dejanzele June 24, 2024 19:55
@dejanzele dejanzele merged commit 06ee73e into G-Research:main Jun 24, 2024
7 checks passed
@sudiptob2 sudiptob2 deleted the feat/48/refine-zap-logger branch July 5, 2024 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for logging settings to config file/env vars
3 participants