-
Notifications
You must be signed in to change notification settings - Fork 127
Draft: CSPL-4347 #1662
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
base: feature/slog-as-default-logger
Are you sure you want to change the base?
Draft: CSPL-4347 #1662
Conversation
Pull Request Test Coverage Report for Build 21068664710Details
💛 - Coveralls |
pkg/logging/slog_test.go
Outdated
| @@ -0,0 +1,947 @@ | |||
| // Copyright (c) 2018-2022 Splunk Inc. All rights reserved. | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2026 instead of 2022?
| flag.StringVar(&slogLevel, "slog-level", "", "slog log level: debug, info, warn, error (overrides LOG_LEVEL env var)") | ||
| flag.StringVar(&slogFormat, "slog-format", "", "slog output format: json, text (overrides LOG_FORMAT env var)") | ||
| flag.BoolVar(&slogAddSource, "slog-add-source", false, "add source file:line to slog output (overrides LOG_ADD_SOURCE env var)") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe those flags should be implementation agnostic? So just log-level, log-format and log-add-source
|
|
||
| // LoadConfig loads logging configuration from environment variables | ||
| // Environment variables take precedence over defaults | ||
| func LoadConfig() Config { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we use it outside of logging package? Maybe it shouldn't be exported?
| AddSource: false, | ||
| } | ||
|
|
||
| if level := os.Getenv(EnvLogLevel); level != "" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[not in this PR] We should have one place where we define configuration. Having those system calls all over codebase is messy.
| // NewProductionHandler creates a handler optimized for production | ||
| // JSON format, INFO level, no source location, sensitive data redaction | ||
| func NewProductionHandler() slog.Handler { | ||
| opts := &slog.HandlerOptions{ | ||
| Level: slog.LevelInfo, | ||
| AddSource: false, | ||
| ReplaceAttr: redactSensitiveData, | ||
| } | ||
|
|
||
| return slog.NewJSONHandler(os.Stdout, opts) | ||
| } | ||
|
|
||
| // NewDevelopmentHandler creates a handler optimized for development | ||
| // Text format, DEBUG level, source location enabled | ||
| func NewDevelopmentHandler() slog.Handler { | ||
| opts := &slog.HandlerOptions{ | ||
| Level: slog.LevelDebug, | ||
| AddSource: true, | ||
| ReplaceAttr: redactSensitiveData, | ||
| } | ||
|
|
||
| return slog.NewTextHandler(os.Stdout, opts) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we set up those opts via config? What peace of code is expected to call those functions?
| func SetupLogger(cfg Config) *slog.Logger { | ||
| handler := NewHandler(cfg) | ||
| logger := slog.New(handler) | ||
| slog.SetDefault(logger) | ||
| return logger | ||
| } | ||
|
|
||
| // SetupLoggerWithAttrs initializes the global slog logger with common attributes | ||
| func SetupLoggerWithAttrs(cfg Config, component, version, build string) *slog.Logger { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should use such function here:
SetupLogger(cfg Config, attrs ...slog.Attr)
| func LevelFromString(s string) slog.Level { | ||
| return parseLevel(s) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit] Maybe we should just use LevelFromString and move parseLevel code here
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package logging |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not black box testing?
Aren't we using build tags?
Description
What does this PR have in it?
Key Changes
Highlight the updates in specific files
Testing and Verification
How did you test these changes? What automated tests are added?
Related Issues
Jira tickets, GitHub issues, Support tickets...
PR Checklist