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



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

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.



283
284
285
# File 'lib/makit/logging.rb', line 283

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.

Options Hash (options):

  • :structured_logging (Boolean) — default: false

    Whether to use JSON formatted logs



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/makit/logging.rb', line 345

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.

Options Hash (options):

  • :log_file (String) — default: nil

    Custom log file path



371
372
373
374
375
376
377
378
379
380
# File 'lib/makit/logging.rb', line 371

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).

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



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/makit/logging.rb', line 311

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



261
262
263
264
265
266
# File 'lib/makit/logging.rb', line 261

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



250
251
252
253
254
255
# File 'lib/makit/logging.rb', line 250

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



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

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

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

Check if all logger targets respond to a given method



292
293
294
# File 'lib/makit/logging.rb', line 292

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