Class: Makit::Commands::Middleware::CommandLogger

Inherits:
Base
  • Object
show all
Defined in:
lib/makit/commands/middleware/command_logger.rb

Overview

Command-specific logging middleware with enhanced output handling

This middleware provides specialized logging for command execution with enhanced output handling, performance tracking, and structured data logging. It’s designed to work alongside the base Logger middleware to provide comprehensive command execution logging.

Examples:

Basic usage

logger = CommandLogger.new
runner = Runner.new(middleware: [logger])
result = runner.execute("git --version")

With custom configuration

logger = CommandLogger.new(
  log_stdout: true,
  log_stderr: true,
  log_performance: true,
  max_output_lines: 50
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#name

Constructor Details

#initialize(log_stdout: false, log_stderr: true, log_performance: true, max_output_lines: 100) ⇒ CommandLogger

Initialize command logging middleware.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/makit/commands/middleware/command_logger.rb', line 39

def initialize(
  log_stdout: false,
  log_stderr: true,
  log_performance: true,
  max_output_lines: 100
)
  @log_stdout = log_stdout
  @log_stderr = log_stderr
  @log_performance = log_performance
  @max_output_lines = max_output_lines

  # Use the default logger for all logging
  @logger = Makit::Logging.default_logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



31
32
33
# File 'lib/makit/commands/middleware/command_logger.rb', line 31

def logger
  @logger
end

Instance Method Details

#applicable?(_request) ⇒ Boolean

Check if this middleware applies to the given request.



92
93
94
95
# File 'lib/makit/commands/middleware/command_logger.rb', line 92

def applicable?(_request)
  # Apply to all requests
  true
end

#call(request) {|Request| ... } ⇒ Result

Execute command with enhanced logging.

Yields:

  • (Request)

    yields request to next middleware



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/makit/commands/middleware/command_logger.rb', line 59

def call(request, &block)
  start_time = Time.now
  command_id = generate_command_id

  # Log command start with enhanced context
  log_command_start(request, command_id)

  begin
    # Execute the command
    result = block.call(request)
    duration = Time.now - start_time

    # Log command completion with performance data
    log_command_completion(request, result, duration, command_id)

    # Log command output if configured
    log_command_output(request, result, command_id)

    result
  rescue StandardError => e
    duration = Time.now - start_time

    # Log command error with full context
    log_command_error(request, e, duration, command_id)

    raise
  end
end

#configHash

Get middleware configuration.



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/makit/commands/middleware/command_logger.rb', line 100

def config
  {
    name: "CommandLogger",
    description: "Command execution logging with performance tracking and output handling",
    log_stdout: @log_stdout,
    log_stderr: @log_stderr,
    log_performance: @log_performance,
    max_output_lines: @max_output_lines,
    logger_class: @logger.class.name,
  }
end