Level
Level represents the log level using syslog-style numeric values. This numeric representation enables fast-path threshold-based filtering.Type Definition
Level Constants
Log levels follow the syslog convention with numeric values:Extremely detailed, high-volume logging for detailed execution traces
Detailed diagnostic information for troubleshooting
Successful operations (between Debug and Info). Maintained for v2 backward compatibility
General informational messages about application progress
Warning messages for potentially harmful situations
Error events that might still allow the application to continue running
Critical errors that will terminate the application
Disables all logging output
Syslog-Style Filtering
The numeric level system uses syslog-style filtering: a message is logged if its level value is greater than or equal to the threshold level. Example:- Threshold:
WarnLevel(40) - Logged:
ErrorLevel(50),FatalLevel(60) - Filtered out:
InfoLevel(30),DebugLevel(20),TraceLevel(10)
Methods
ShouldLog
Returns true if this level should be logged given the threshold. This is the public version for use in hooks and other external code.The minimum level threshold
true if this level meets or exceeds the threshold, false otherwise.
Example
String
Returns the string representation of the log level. Returns “UNKNOWN” for undefined levels.Example
Functions
ParseLevel
Converts a string to a Level. Case-insensitive. ReturnsInfoLevel as default for unknown strings.
The level string to parse (case-insensitive)
Level. Returns InfoLevel for unknown strings.
Supported Values
"trace"→TraceLevel"debug"→DebugLevel"info"→InfoLevel"warn","warning"→WarnLevel"error"→ErrorLevel"fatal"→FatalLevel"success"→SuccessLevel"silent","none","disable"→SilentLevel- Any other value →
InfoLevel(default)
Example
Performance
Log level filtering is optimized for performance:- Fast-path filtering: ~0.32 ns/op (target: < 5 ns)
- Zero allocations: Level comparisons don’t allocate memory
- Critical optimization: Level checking must be extremely fast as it runs on every log call
Usage Examples
Setting Log Level
Environment-Based Configuration
Conditional Logging in Hooks
Best Practices
- Production: Use
InfoLevelorWarnLevelto reduce log volume - Development: Use
DebugLevelorTraceLevelfor detailed diagnostics - Environment variables: Use
LOG_LEVELenvironment variable for configuration - Performance: Level filtering is optimized, so don’t worry about performance impact
- Silent mode: Use
SilentLevelto completely disable logging when needed