Skip to content

Conversation

@SgtPooki
Copy link
Member

@SgtPooki SgtPooki commented Jul 24, 2025

Add Log Level Retrieval Functionality

Summary

Added functionality to track and retrieve current log levels (global and per-subsystem) to improve debugging capabilities.

Related ipfs/kubo#10882
Fixes #159

Changes

New Functions

  • GetLogLevel(names string) (LogLevel, error)

    • Returns current default level when called with "" or "*"
    • Returns specific subsystem level when called with subsystem name
    • Returns ErrNoSuchLogger for non-existent subsystems
  • GetAllLogLevels() map[string]LogLevel

    • Returns map of all current log levels
    • Includes "*" key for default/global level
    • Shows all registered subsystem levels

Key Features

  • Thread-safe - Uses existing loggerMutex for concurrent access
  • Real-time - Reflects dynamic changes made via SetLogLevel()
  • Consistent API - Follows existing naming conventions and patterns

Usage Examples

// Get default level
level, err := log.GetLogLevel("*")

// Get specific subsystem level
httpLevel, err := log.GetLogLevel("http")

// Get all current levels
allLevels := log.GetAllLogLevels()
fmt.Printf("Default: %v\n", allLevels["*"])

Testing

Added comprehensive tests covering:

  • Default level retrieval ("", "*")
  • Subsystem level retrieval (existing/non-existing)
  • Dynamic level changes
  • Modifying the returned map does not affect internal state

@welcome

This comment was marked as resolved.

@SgtPooki SgtPooki requested review from gammazero and lidel July 24, 2025 17:17
Copy link
Member Author

@SgtPooki SgtPooki left a comment

Choose a reason for hiding this comment

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

self review

@SgtPooki
Copy link
Member Author

I posted some questions on the Kubo PR about how we want to handle global going forward. I didn't want to change it in this PR by default, but if we have updated the global setting, it should make sure new loggers are using that setting rather than the “default” that, currently in this PR, is only the basic starting level for logs..

But, I guess it could be viewed as better either way.

@SgtPooki SgtPooki requested a review from gammazero July 28, 2025 15:16
Copy link
Member Author

@SgtPooki SgtPooki left a comment

Choose a reason for hiding this comment

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

self review

Co-authored-by: Russell Dempsey <[email protected]>
@SgtPooki SgtPooki requested a review from gammazero July 28, 2025 18:14
levels.go Outdated
return zapcore.Level(defLvl).String(), nil
}
if lvl, ok := levels[name]; ok {
return zapcore.Level(LogLevel(lvl.Level())).String(), nil
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return zapcore.Level(LogLevel(lvl.Level())).String(), nil
return lvl.Level().String(), nil

levels.go Outdated

// Add all subsystem levels
for name, level := range levels {
result[name] = zapcore.Level(LogLevel(level.Level())).String()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
result[name] = zapcore.Level(LogLevel(level.Level())).String()
result[name] = level.Level().String()

@gammazero gammazero merged commit e5a8432 into master Jul 29, 2025
9 checks passed
@gammazero gammazero deleted the fix/allow-querying-log-levels branch July 29, 2025 06:59
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.

Allow getting current log levels

4 participants