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

Slog handler #6474

Merged
merged 6 commits into from
Sep 26, 2024
Merged

Slog handler #6474

merged 6 commits into from
Sep 26, 2024

Conversation

pdabelf5
Copy link
Collaborator

@pdabelf5 pdabelf5 commented Sep 23, 2024

Proposed changes

  • add basic slog handler for glog formatting
  • add ability to add logger to a context

To create a new slog based logger using the glog handler you can use code similar to:

import nic_glog "github.com/nginxinc/kubernetes-ingress/internal/logger/glog"

programLevel := new(slog.LevelVar) // Info by default
l := slog.New(nic_glog.New(os.Stdout, &nic_glog.Options{Level: programLevel}))
slog.SetDefault(l)

# To adjust the log level
programLevel.Set(slog.LevelDebug)

To attach the logger to a context you can use:

ctx := nic_logger.ContextWithLogger(context.Background(), slog.New(nic_glog.New(os.Stdout, nil)))

To retrieve the logger from the context use

l := nic_logger.LoggerFromContext(ctx)
l.Warn("my warning log")

slog does not provide Fatal & Trace log levels by default. These have been added to the glog handler but the following syntax is needed to log using these levels

l.Log(context.Background(), LevelTrace, "test")
l.Log(context.Background(), LevelFatal, "test")
os.Exit(1)

Note: The l.Log(context.Background(), LevelFatal, "test") implementation needs to call os.Exit() as the Fatal level does not exit itself.

Checklist

Before creating a PR, run through this checklist and mark each as complete.

  • I have read the CONTRIBUTING doc
  • I have added tests that prove my fix is effective or that my feature works
  • I have checked that all unit tests pass after adding my changes
  • I have updated necessary documentation
  • I have rebased my branch onto main
  • I will ensure my PR is targeting the main branch and pulling from my branch from my own fork

@github-actions github-actions bot added the go Pull requests that update Go code label Sep 23, 2024
Copy link

codecov bot commented Sep 23, 2024

Codecov Report

Attention: Patch coverage is 82.14286% with 10 lines in your changes missing coverage. Please review.

Project coverage is 53.30%. Comparing base (c099abe) to head (146b6a5).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
internal/logger/logger.go 0.00% 6 Missing ⚠️
internal/logger/glog/handler.go 92.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6474      +/-   ##
==========================================
+ Coverage   53.21%   53.30%   +0.09%     
==========================================
  Files          85       87       +2     
  Lines       16016    16074      +58     
==========================================
+ Hits         8523     8569      +46     
- Misses       7085     7097      +12     
  Partials      408      408              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jjngx
Copy link
Contributor

jjngx commented Sep 23, 2024

it looks great! 🚀

@pdabelf5 pdabelf5 marked this pull request as ready for review September 25, 2024 09:25
@pdabelf5 pdabelf5 requested a review from a team as a code owner September 25, 2024 09:25
@pdabelf5 pdabelf5 enabled auto-merge (squash) September 26, 2024 08:09
@shaun-nx shaun-nx mentioned this pull request Sep 26, 2024
13 tasks
@pdabelf5 pdabelf5 merged commit 4e2e789 into main Sep 26, 2024
29 checks passed
@pdabelf5 pdabelf5 deleted the slog-handler branch September 26, 2024 09:16
@pdabelf5 pdabelf5 linked an issue Sep 26, 2024 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
go Pull requests that update Go code
Projects
Status: Done 🚀
Development

Successfully merging this pull request may close these issues.

Add slog handler to write glog formatted logs
3 participants