Class: Lumberjack::Rails::Railtie

Inherits:
Rails::Railtie
  • Object
show all
Defined in:
lib/lumberjack/rails/railtie.rb

Overview

Railtie for integrating Lumberjack::Logger with Rails applications.

This railtie replaces the standard Rails logger with a Lumberjack::Logger while maintaining compatibility with Rails’ logging configuration options.

Configuration options:

config.lumberjack.enabled (default: true)
  Whether to replace Rails.logger with Lumberjack::Logger

config.lumberjack.device (default: Rails log file)
  The device to write logs to (file path, IO object, Lumberjack Device)

config.lumberjack.level (default: config.log_level)
  The log level for the Lumberjack logger

config.lumberjack.attributes (default: nil)
  Attributes to apply to log messages

config.lumberjack.shift_age (default: 0)
  The age (in seconds) of log files before they are rotated or
  a shift name (daily, weekly, monthly)

config.lumberjack.shift_size (default: 1048576)
  The size (in bytes) of log files before they are rotated if shift_age
  is set to 0.

config.lumberjack.log_rake_tasks (default: false)
  Whether to redirect $stdout and $stderr to Rails.logger for rake tasks
  that depend on the :environment task when using a Lumberjack::Logger

config.lumberjack.middleware (default: true)
  Whether to install Rack middleware that adds a Lumberjack context to each request.

config.lumberjack.request_attributes_proc (default: nil)
  A proc to add tags to log entries for each request. The proc, will be
  called with the request object and must return a hash of attributes to
  include in each log entry for the request.

config.lumberjack.silence_rack_request_started (default: false)
  Whether to silence the "Started ..." log lines in Rack::Logger. You may want to silence
  this entry if it is just creating noise in your production logs.

config.lumberjack.*
  All other options are sent as options to the Lumberjack logger
  constructor.

Example usage in config/application.rb:

config.log_level = :info
config.lumberjack.attributes = {app: "my_app", host: Lumberjack::Utils.hostname}
config.lumberjack.device = STDOUT  # optional override

Class Method Summary collapse

Class Method Details

.lumberjack_logger(config, log_file_path = nil) ⇒ Lumberjack::Logger?

Create a Lumberjack logger based on Rails configuration.

Parameters:

  • config (Rails::Application::Configuration)

    the Rails application configuration

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

    optional path to the log file

Returns:

  • (Lumberjack::Logger, nil)

    the configured logger or nil if not enabled



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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/lumberjack/rails/railtie.rb', line 60

def lumberjack_logger(config, log_file_path = nil)
  return nil if config.logger
  return nil if config.lumberjack.nil? || config.lumberjack == false
  return nil unless config.lumberjack.enabled

  # Determine the log device
  device = config.lumberjack.device
  if device.nil?
    if log_file_path
      FileUtils.mkdir_p(File.dirname(log_file_path)) unless File.exist?(File.dirname(log_file_path))
      device = log_file_path
    else
      device = $stdout
    end
  end

  # Determine the log level
  level = config.lumberjack.level || config.log_level || :debug

  # Set default attributes
  attributes = config.lumberjack.attributes
  if config.log_tags
    attributes ||= {}
    attributes["tags"] = config.log_tags
  end

  shift_age = config.lumberjack.shift_age || 0
  shift_size = config.lumberjack.shift_size || 1048576

  # Create logger options
  logger_options = config.lumberjack.to_h.except(
    :enabled,
    :raise_logger_errors,
    :device, :level,
    :progname,
    :attributes,
    :shift_age,
    :shift_size,
    :log_rake_tasks,
    :middleware,
    :request_attribute,
    :silence_rack_request_started
  )

  logger_options.merge!(
    level: level,
    formatter: config.lumberjack.formatter,
    progname: config.lumberjack.progname
  )

  # Create the Lumberjack logger
  logger = Lumberjack::Logger.new(device, shift_age, shift_size, **logger_options)
  logger.tag!(attributes) if attributes
  logger.formatter.prepend(Lumberjack::Rails.active_record_entry_formatter)

  logger
end

.set_standard_streams_to_loggers!(config, logger) ⇒ void

This method returns an undefined value.

Redirect standard streams ($stdout, $stderr) to logger instances.

Parameters:

  • config (Rails::Application::Configuration)

    the Rails application configuration

  • logger (Lumberjack::Logger)

    the logger to redirect streams to



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/lumberjack/rails/railtie.rb', line 123

def set_standard_streams_to_loggers!(config, logger)
  return unless config.lumberjack&.log_rake_tasks
  return unless logger.respond_to?(:fork) && logger.respond_to?(:puts)

  if !$stdout.tty? && !$stdout.is_a?(::Logger)
    stdout_logger = logger.fork
    stdout_logger.default_severity = :info
    $stdout = stdout_logger
  end

  if !$stderr.tty? && !$stderr.is_a?(::Logger)
    stderr_logger = logger.fork
    stderr_logger.default_severity = :warn
    $stderr = stderr_logger
  end
end