Class: Makit::Logging::MultiLogger Deprecated

Inherits:
Object
  • Object
show all
Defined in:
lib/makit/logging.rb

Overview

Deprecated.

This class is deprecated and will be removed in version 0.2.0. Use Makit::Logging::Logger with multiple middleware instead.

Multi-target logger that writes to multiple destinations

This logger allows writing log messages to multiple targets simultaneously, such as both stdout and a file, or multiple files with different formats.

Examples:

Create a logger that writes to both console and file

stdout_logger = Logger.new($stdout)
file_logger = Logger.new("app.log")
multi_logger = MultiLogger.new(stdout_logger, file_logger)
multi_logger.info("This goes to both destinations")

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*targets) ⇒ MultiLogger

Initialize a new MultiLogger with multiple logging targets

Parameters:

  • targets (Array<Logger>)

    One or more logger instances to write to



225
226
227
228
# File 'lib/makit/logging.rb', line 225

def initialize(*targets)
  warn "Makit::Logging::MultiLogger is deprecated and will be removed in version 0.2.0. Use Makit::Logging::Logger with middleware instead."
  @targets = targets
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ nil

Forward any unknown methods to all logger targets

This allows calling standard Logger methods like info, debug, error, etc. directly on the MultiLogger instance.

Parameters:

  • method (Symbol)

    The method name to forward

  • args (Array)

    Arguments to pass to the method

Returns:

  • (nil)


270
271
272
# File 'lib/makit/logging.rb', line 270

def method_missing(method, ...)
  @targets.each { |logger| logger.send(method, ...) }
end

Class Method Details

.create_file_logger(log_filename, log_level, options) ⇒ Logger

Create a file logger with appropriate formatter

Creates a logger that writes to a file with the specified formatter and log level. Ensures the directory for the log file exists.

Parameters:

  • log_filename (String)

    Path where log file should be created

  • log_level (Integer)

    Log level to set (from Logger constants)

  • options (Hash)

    Configuration options

Options Hash (options):

  • :structured_logging (Boolean) — default: false

    Whether to use JSON formatted logs

Returns:

  • (Logger)

    Configured file logger



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/makit/logging.rb', line 332

def self.create_file_logger(log_filename, log_level, options)
  FileUtils.mkdir_p(File.dirname(log_filename))

  file_logger = ::Logger.new(log_filename)
  file_logger.level = log_level

  # Use structured logging for file output if requested

  file_logger.formatter = if options[:structured_logging]
      StructuredFormatter.new
    else
      PlainFormatter.new
    end

  file_logger
end

.create_log_filename(options) ⇒ String

Determine the appropriate log filename

Generates a log filename based on options and the current rake task. If a custom log file is specified in options, that is used. Otherwise, a log file is created in the artifacts directory with a name based on the current rake task.

Parameters:

  • options (Hash)

    Configuration options

Options Hash (options):

  • :log_file (String) — default: nil

    Custom log file path

Returns:

  • (String)

    The path to use for the log file



358
359
360
361
362
363
364
365
366
367
# File 'lib/makit/logging.rb', line 358

def self.create_log_filename(options)
  if options[:log_file]
    options[:log_file]
  elsif ARGV.empty?
    "#{Makit::Environment.project_root_directory}/artifacts/rake.log"
  else
    task_name = ARGV.join("_").gsub(":", "_")
    "#{Makit::Environment.project_root_directory}/artifacts/rake_#{task_name}.log"
  end
end

.create_logger(options = {}) ⇒ Logger, MultiLogger

Deprecated.

This method is deprecated and will be removed in version 0.2.0. Use Makit::Logging::Logger.new with middleware instead.

Create a configured logger based on options

This factory method creates a logger configured with appropriate formatters and targets based on the provided options. By default, it creates a logger that writes to both stdout (with colors) and a log file (plain text).

Parameters:

  • options (Hash) (defaults to: {})

    Logger configuration options

Options Hash (options):

  • :level (Symbol) — default: :info

    The log level (:debug, :info, :warn, :error, :fatal)

  • :file_logging (Boolean) — default: true

    Whether to log to a file

  • :log_file (String) — default: nil

    Custom log file path, defaults to rake task-based path

  • :structured_logging (Boolean) — default: false

    Whether to use JSON structured logging for file output

Returns:



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/makit/logging.rb', line 298

def self.create_logger(options = {})
  warn "Makit::Logging::MultiLogger.create_logger is deprecated and will be removed in version 0.2.0. Use Makit::Logging::Logger.new with middleware instead."
  log_level = options[:level] || DEFAULT_LOG_LEVEL
  log_level = LOG_LEVELS[log_level] || LOG_LEVELS[DEFAULT_LOG_LEVEL]

  stdout_logger = ::Logger.new($stdout)
  stdout_logger.level = log_level
  stdout_logger.formatter = ColorFormatter.new

  # if clean or clobber commands are used, then log ONLY to stdout

  return stdout_logger if ARGV.include?("clean") || ARGV.include?("clobber")

  return stdout_logger if Makit::Environment.project_root_directory.nil?

  # Create file logger if logging to file is enabled

  if options[:file_logging] != false
    log_filename = create_log_filename(options)
    file_logger = create_file_logger(log_filename, log_level, options)
    return MultiLogger.new(file_logger, stdout_logger)
  end

  stdout_logger
end

Instance Method Details

#<<(message) ⇒ nil

Append a message directly to all logger targets

Parameters:

  • message (String)

    The message to append to all loggers

Returns:

  • (nil)


248
249
250
251
252
253
# File 'lib/makit/logging.rb', line 248

def <<(message)
  @targets.each do |logger|
    logger << message
    logger.flush if logger.respond_to?(:flush)
  end
end

#add(severity, message = nil, progname = nil, &block) ⇒ nil

Add a message to all logger targets

Parameters:

  • severity (Integer)

    The severity level (::Logger::DEBUG, ::Logger::INFO, etc.)

  • message (String, nil) (defaults to: nil)

    The message to log (or nil if using block)

  • progname (String, nil) (defaults to: nil)

    The program name to include in the log

  • block (Proc, nil)

    Optional block that returns the message to log

Returns:

  • (nil)


237
238
239
240
241
242
# File 'lib/makit/logging.rb', line 237

def add(severity, message = nil, progname = nil, &block)
  @targets.each do |logger|
    logger.add(severity, message, progname, &block)
    logger.flush if logger.respond_to?(:flush)
  end
end

#closenil

Close all logger targets

Returns:

  • (nil)


258
259
260
# File 'lib/makit/logging.rb', line 258

def close
  @targets.each(&:close)
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Check if all logger targets respond to a given method

Parameters:

  • method (Symbol)

    The method name to check

  • include_private (Boolean) (defaults to: false)

    Whether to include private methods

Returns:

  • (Boolean)

    True if all logger targets respond to the method



279
280
281
# File 'lib/makit/logging.rb', line 279

def respond_to_missing?(method, include_private = false)
  @targets.all? { |logger| logger.respond_to?(method, include_private) }
end