Skip to content

Implement use slog.Leveler and slog.LevelVar. And adding custom log level#180

Open
kondr1 wants to merge 1 commit intocharmbracelet:mainfrom
kondr1:fix
Open

Implement use slog.Leveler and slog.LevelVar. And adding custom log level#180
kondr1 wants to merge 1 commit intocharmbracelet:mainfrom
kondr1:fix

Conversation

@kondr1
Copy link

@kondr1 kondr1 commented Aug 15, 2025

Copilot AI review requested due to automatic review settings August 15, 2025 15:44
@kondr1 kondr1 requested a review from aymanbagabas as a code owner August 15, 2025 15:44
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements support for slog.Leveler and slog.LevelVar interfaces while adding custom log level functionality. The changes enable more flexible level management by allowing dynamic level variables and custom level types that implement the slog.Leveler interface.

Key changes:

  • Replace direct Level usage with slog.Leveler interface throughout the codebase
  • Change level storage from int64 to *slog.LevelVar for dynamic level management
  • Update styling system to use int keys instead of Level keys for better extensibility
  • Add support for custom levels with proper styling integration

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
logger.go Core changes to use slog.Leveler interface and *slog.LevelVar for level storage
pkg.go Updated package-level functions and NewWithOptions to handle slog.Leveler types
options.go Changed Options.Level field type from Level to slog.Leveler
styles.go Modified Levels map to use int keys instead of Level keys
level.go Added Level() slog.Level method to implement slog.Leveler interface
text.go Updated text formatter to work with slog.Leveler interface
json.go Enhanced JSON formatter to handle custom levels through the Levels map
logger_121.go Updated slog handler implementation for Go 1.21+
logger_no121.go Updated slog handler implementation for Go <1.21
*_test.go Updated all tests to work with new level system and added custom level tests

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.


// GetLevel returns the level for the default logger.
func GetLevel() Level {
func GetLevel() slog.Leveler {
Copy link

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

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

The return type change from Level to slog.Leveler is a breaking API change. Consider adding a deprecation notice or maintaining backward compatibility by providing both functions.

Suggested change
func GetLevel() slog.Leveler {
//
// Deprecated: Use GetLeveler() instead. This will be removed in a future release.
func GetLevel() Level {
return Default().GetLevel().Level()
}
// GetLeveler returns the slog.Leveler for the default logger.
func GetLeveler() slog.Leveler {

Copilot uses AI. Check for mistakes.
l.mu.RLock()
defer l.mu.RUnlock()
return Level(l.level)
return l.level.Level()
Copy link

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

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

The return type change from Level to slog.Leveler is a breaking API change. The method now returns l.level.Level() instead of the *slog.LevelVar itself, which may not be what callers expect when they need to modify the level dynamically.

Copilot uses AI. Check for mistakes.
func (l *Logger) SetLevel(level Level) {
func (l *Logger) SetLevel(level slog.Leveler) {
l.mu.Lock()
defer l.mu.Unlock()
Copy link

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

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

The parameter type change from Level to slog.Leveler is a breaking API change. Consider maintaining backward compatibility or providing clear migration guidance.

Copilot uses AI. Check for mistakes.
} else {
lvl.Set(o.Level.Level())
}
l.level = lvl
Copy link

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

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

Using a type switch on an interface without handling the nil case explicitly could lead to unexpected behavior. Consider adding a nil check before the type switch.

Suggested change
l.level = lvl
if o.Level == nil {
lvl := new(slog.LevelVar)
lvl.Set(slog.LevelInfo)
l.level = lvl
} else {
switch lvlVar := o.Level.(type) {
case *slog.LevelVar:
l.level = lvlVar
default:
lvl := new(slog.LevelVar)
lvl.Set(o.Level.Level())
l.level = lvl
}

Copilot uses AI. Check for mistakes.
@codecov
Copy link

codecov bot commented Aug 15, 2025

Codecov Report

❌ Patch coverage is 94.59459% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.09%. Comparing base (2338a13) to head (056f91e).
⚠️ Report is 51 commits behind head on main.

Files with missing lines Patch % Lines
pkg.go 84.61% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #180      +/-   ##
==========================================
+ Coverage   80.92%   83.09%   +2.17%     
==========================================
  Files          11       11              
  Lines         739      834      +95     
==========================================
+ Hits          598      693      +95     
+ Misses        126      125       -1     
- Partials       15       16       +1     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

2 participants