Class: Securial::Logger::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/securial/logger/builder.rb

Overview

Builder for constructing Securial’s logging system.

This class provides factory methods to create properly configured logger instances based on the application’s configuration settings. It supports multiple logging destinations and handles the setup of formatters, log levels, and tagging.

Class Method Summary collapse

Class Method Details

.buildSecurial::Logger::Broadcaster

Builds a complete logger system based on configuration settings.

Creates file and/or stdout loggers as specified in configuration and combines them using a Broadcaster to provide unified logging to multiple destinations with appropriate formatting for each.

Returns:

See Also:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/securial/logger/builder.rb', line 42

def self.build
  loggers = []
  progname = "Securial"

  file_logger_enabled = Securial.configuration.log_to_file
  file_logger_level = Securial.configuration.log_file_level
  stdout_logger_enabled = Securial.configuration.log_to_stdout
  stdout_logger_level = Securial.configuration.log_stdout_level

  create_file_logger(progname, file_logger_level, loggers) if file_logger_enabled
  create_stdout_logger(progname, stdout_logger_level, loggers) if stdout_logger_enabled

  Broadcaster.new(loggers)
end

.create_file_logger(progname, level, loggers) ⇒ ActiveSupport::TaggedLogging

Creates and configures a file logger.

Sets up a logger that writes to a Rails environment-specific log file with plain text formatting and adds it to the provided loggers array.

Parameters:

  • progname (String)

    The program name to include in log entries

  • level (Integer, Symbol)

    The log level (e.g., :info, :debug)

  • loggers (Array<Logger>)

    Array to which the new logger will be added

Returns:

  • (ActiveSupport::TaggedLogging)

    The configured file logger

See Also:



68
69
70
71
72
73
74
75
# File 'lib/securial/logger/builder.rb', line 68

def self.create_file_logger(progname, level, loggers)
  file_logger = ::Logger.new(Rails.root.join("log", "securial-#{Rails.env}.log"))
  file_logger.level = level
  file_logger.progname = progname
  file_logger.formatter = Formatter::PlainFormatter.new
  tagged_file_logger = ActiveSupport::TaggedLogging.new(file_logger)
  loggers << tagged_file_logger
end

.create_stdout_logger(progname, level, loggers) ⇒ ActiveSupport::TaggedLogging

Creates and configures a stdout logger.

Sets up a logger that writes to standard output with colorful formatting and adds it to the provided loggers array.

Parameters:

  • progname (String)

    The program name to include in log entries

  • level (Integer, Symbol)

    The log level (e.g., :info, :debug)

  • loggers (Array<Logger>)

    Array to which the new logger will be added

Returns:

  • (ActiveSupport::TaggedLogging)

    The configured stdout logger

See Also:



88
89
90
91
92
93
94
95
# File 'lib/securial/logger/builder.rb', line 88

def self.create_stdout_logger(progname, level, loggers)
  stdout_logger = ::Logger.new($stdout)
  stdout_logger.level = level
  stdout_logger.progname = progname
  stdout_logger.formatter = Formatter::ColorfulFormatter.new
  tagged_stdout_logger = ActiveSupport::TaggedLogging.new(stdout_logger)
  loggers << tagged_stdout_logger
end