Class: Musa::Logger::Logger
- Defined in:
- lib/musa-dsl/logger/logger.rb
Overview
The logger inherits all standard Ruby Logger methods (debug, info, warn, error, fatal).
Position values are formatted as floating point for readability, even though the sequencer internally uses Rational numbers.
Custom logger that displays sequencer position with log messages.
This logger extends Ruby's standard Logger class to prepend the current sequencer position to each log message, making it easy to track events in musical time during composition and playback.
Features
- Automatic sequencer position formatting in log output
- Configurable position precision (integer and decimal digits)
- Conditional formatting (position only shown when sequencer is provided)
- Uses InspectNice refinements for better Rational display
- Defaults to STDERR output with WARN level
Log Format
The formatted log output follows this pattern:
[position]: [LEVEL] [progname] message
Where:
positionis the sequencer position (only if sequencer provided)LEVELis the severity level (omitted for DEBUG)prognameis the program/module name (optional)messageis the actual log message
Instance Method Summary collapse
-
#initialize(sequencer: nil, position_format: nil) ⇒ Logger
constructor
Creates a new logger with optional sequencer integration.
-
#level ⇒ Integer
Override level getter to handle encoding compatibility issues.
Constructor Details
#initialize(sequencer: nil, position_format: nil) ⇒ Logger
The logger outputs to STDERR by default with level set to WARN.
Uses InspectNice refinements for better formatting of Rationals and Hashes.
The sequencer's position is read at log time, not at event scheduling time. This means the position reflects when the log message is actually generated.
Creates a new logger with optional sequencer integration.
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/musa-dsl/logger/logger.rb', line 215 def initialize(sequencer: nil, position_format: nil) super STDERR, level: WARN # Store sequencer reference for position queries in formatter @sequencer = sequencer # Store position format specification @position_format = position_format || 3.3 # Custom formatter that integrates sequencer position with log messages. # # This proc is called by Ruby's Logger for each log entry. It captures # @sequencer and @position_format from the enclosing scope to format # messages with musical timing information. # # The formatter constructs messages in the format: # [position]: [LEVEL] [progname] message # # Position calculation: # # - Splits position_format into integer and decimal parts # - Example: 3.3 => 3 integer digits + 3 decimal digits # - Formats sequencer position with calculated precision # - Right-aligns position in the allocated width # # Severity handling: # # - DEBUG level: severity not shown in output # - Other levels: shown as [WARN], [INFO], [ERROR], [FATAL] # # Spacing: # # - Adds separator space only if position, level, or progname are present # - Empty messages output just a newline # # @param severity [String] log level (DEBUG, INFO, WARN, ERROR, FATAL) # @param time [Time] timestamp of the log event (not used in current implementation) # @param progname [String, nil] program/component name # @param msg [String, nil] the actual log message # @return [String] formatted log line with newline self.formatter = proc do |severity, time, progname, msg| # Omit severity label for DEBUG level level = "[#{severity}] " unless severity == 'DEBUG' if msg # Calculate and format sequencer position if available position = if @sequencer # Extract integer and decimal digit counts from position_format # e.g., 3.3 => integer_digits=3, decimal_digits=3 integer_digits = @position_format.to_i decimal_digits = ((@position_format - integer_digits) * 10).round # Format position: total width includes digits + decimal point + ': ' # Right-aligned to keep positions visually aligned in logs "%#{integer_digits + decimal_digits + 1}s: " % ("%.#{decimal_digits}f" % @sequencer.position.to_f) end # Wrap progname in brackets if provided progname = "[#{progname}]" if progname # Construct final message with conditional spacing "#{position}#{level}#{progname}#{' ' if position || level || progname}#{msg}\n" else # Empty message case "\n" end end end |
Instance Method Details
#level ⇒ Integer
Override level getter to handle encoding compatibility issues.
Ruby's Logger (>= 1.7.0) has an encoding bug in level_override that causes Encoding::CompatibilityError when mixing UTF-8 strings from Musa with Logger's BINARY (ASCII-8BIT) internal strings.
This override catches the encoding error and returns the level directly from the instance variable, bypassing the buggy level_override method.
294 295 296 297 298 299 |
# File 'lib/musa-dsl/logger/logger.rb', line 294 def level super rescue Encoding::CompatibilityError # Bypass level_override and return raw level @level end |