Module: Lumberjack::Rails::BroadcastLoggerExtension

Defined in:
lib/lumberjack/rails/broadcast_logger_extension.rb

Overview

Extension for ActiveSupport::BroadcastLogger to provide Lumberjack compatibility.

This module extends ActiveSupport::BroadcastLogger to ensure proper handling of Lumberjack loggers when broadcasting to multiple loggers, including support for Lumberjack-specific features like attributes and contexts.

Instance Method Summary collapse

Instance Method Details

#add(severity, message_or_progname_or_attributes = nil, progname_or_attributes = nil) { ... } ⇒ void Also known as: log

This method returns an undefined value.

Add a log entry with the specified severity and optional attributes support.

Parameters:

  • severity (Integer, Symbol, String)

    the severity for the log entry

  • message_or_progname_or_attributes (String, Hash) (defaults to: nil)

    the message, progname, or attributes

  • progname_or_attributes (String, Hash) (defaults to: nil)

    the progname or attributes

Yields:

  • optional block that returns the message



102
103
104
105
106
107
# File 'lib/lumberjack/rails/broadcast_logger_extension.rb', line 102

def add(severity, message_or_progname_or_attributes = nil, progname_or_attributes = nil, &block)
  severity = Logger::Severity.coerce(severity)
  dispatch_to_each(block) do |logger, one_time_block|
    call_add_with_attributes_arg(logger, severity, message_or_progname_or_attributes, progname_or_attributes, &one_time_block)
  end
end

#append_to(attribute_name, *tag) { ... } ⇒ Object

Append values to an existing attribute.

Parameters:

  • attribute_name (String, Symbol)

    the name of the attribute

  • tag (Array)

    the values to append

Yields:

  • the block to execute with the modified attribute

Returns:

  • (Object)

    the result of the block execution



151
152
153
# File 'lib/lumberjack/rails/broadcast_logger_extension.rb', line 151

def append_to(attribute_name, *tag, &block)
  dispatch_block_method(:append_to, attribute_name, *tag, &block)
end

#clear_attributes { ... } ⇒ Object

Clear all current attributes.

Yields:

  • the block to execute with cleared attributes

Returns:

  • (Object)

    the result of the block execution



159
160
161
# File 'lib/lumberjack/rails/broadcast_logger_extension.rb', line 159

def clear_attributes(&block)
  dispatch_block_method(:clear_attributes, &block)
end

#context { ... } ⇒ Object

Execute a block within a logger context.

Yields:

  • the block to execute within the context

Returns:

  • (Object)

    the result of the block execution



133
134
135
# File 'lib/lumberjack/rails/broadcast_logger_extension.rb', line 133

def context(&block)
  dispatch_block_method(:context, &block)
end

#debug(message_or_progname_or_attributes = nil, progname_or_attributes = nil) { ... } ⇒ void

This method returns an undefined value.

Log a debug message with optional attributes support.

Parameters:

  • message_or_progname_or_attributes (String, Hash) (defaults to: nil)

    the message, progname, or attributes

  • progname_or_attributes (String, Hash) (defaults to: nil)

    the progname or attributes

Yields:

  • optional block that returns the message



29
30
31
32
33
# File 'lib/lumberjack/rails/broadcast_logger_extension.rb', line 29

def debug(message_or_progname_or_attributes = nil, progname_or_attributes = nil, &block)
  dispatch_to_each(block) do |logger, one_time_block|
    call_with_attributes_arg(logger, :debug, message_or_progname_or_attributes, progname_or_attributes, &one_time_block)
  end
end

#ensure_context { ... } ⇒ Object

Ensure a Lumberjack context is present for the duration of the block.

Yields:

  • the block to execute within the ensured context

Returns:

  • (Object)

    the result of the block execution



141
142
143
# File 'lib/lumberjack/rails/broadcast_logger_extension.rb', line 141

def ensure_context(&block)
  dispatch_block_method(:ensure_context, &block)
end

#error(message_or_progname_or_attributes = nil, progname_or_attributes = nil) { ... } ⇒ void

This method returns an undefined value.

Log an error message with optional attributes support.

Parameters:

  • message_or_progname_or_attributes (String, Hash) (defaults to: nil)

    the message, progname, or attributes

  • progname_or_attributes (String, Hash) (defaults to: nil)

    the progname or attributes

Yields:

  • optional block that returns the message



65
66
67
68
69
# File 'lib/lumberjack/rails/broadcast_logger_extension.rb', line 65

def error(message_or_progname_or_attributes = nil, progname_or_attributes = nil, &block)
  dispatch_to_each(block) do |logger, one_time_block|
    call_with_attributes_arg(logger, :error, message_or_progname_or_attributes, progname_or_attributes, &one_time_block)
  end
end

#fatal(message_or_progname_or_attributes = nil, progname_or_attributes = nil) { ... } ⇒ void

This method returns an undefined value.

Log a fatal message with optional attributes support.

Parameters:

  • message_or_progname_or_attributes (String, Hash) (defaults to: nil)

    the message, progname, or attributes

  • progname_or_attributes (String, Hash) (defaults to: nil)

    the progname or attributes

Yields:

  • optional block that returns the message



77
78
79
80
81
# File 'lib/lumberjack/rails/broadcast_logger_extension.rb', line 77

def fatal(message_or_progname_or_attributes = nil, progname_or_attributes = nil, &block)
  dispatch_to_each(block) do |logger, one_time_block|
    call_with_attributes_arg(logger, :fatal, message_or_progname_or_attributes, progname_or_attributes, &one_time_block)
  end
end

#fork(level: nil, progname: nil, attributes: nil) ⇒ Lumberjack::ForkedLogger

Create a forked logger from this broadcast logger.

Parameters:

  • level (Symbol, Integer) (defaults to: nil)

    optional log level for the forked logger

  • progname (String) (defaults to: nil)

    optional progname for the forked logger

  • attributes (Hash) (defaults to: nil)

    optional attributes for the forked logger

Returns:

  • (Lumberjack::ForkedLogger)

    the forked logger



195
196
197
198
199
200
201
# File 'lib/lumberjack/rails/broadcast_logger_extension.rb', line 195

def fork(level: nil, progname: nil, attributes: nil)
  logger = Lumberjack::ForkedLogger.new(self)
  logger.level = level if level
  logger.progname = progname if progname
  logger.tag!(attributes) if attributes && !attributes.empty?
  logger
end

#info(message_or_progname_or_attributes = nil, progname_or_attributes = nil) { ... } ⇒ void

This method returns an undefined value.

Log an info message with optional attributes support.

Parameters:

  • message_or_progname_or_attributes (String, Hash) (defaults to: nil)

    the message, progname, or attributes

  • progname_or_attributes (String, Hash) (defaults to: nil)

    the progname or attributes

Yields:

  • optional block that returns the message



41
42
43
44
45
# File 'lib/lumberjack/rails/broadcast_logger_extension.rb', line 41

def info(message_or_progname_or_attributes = nil, progname_or_attributes = nil, &block)
  dispatch_to_each(block) do |logger, one_time_block|
    call_with_attributes_arg(logger, :info, message_or_progname_or_attributes, progname_or_attributes, &one_time_block)
  end
end

#initialize(*loggers) ⇒ Object

Initialize the broadcast logger with validation for Lumberjack loggers.

Parameters:

  • loggers (Array<Logger>)

    array of loggers to broadcast to

Raises:

  • (ArgumentError)

    if more than one Lumberjack logger is provided



15
16
17
18
19
20
21
# File 'lib/lumberjack/rails/broadcast_logger_extension.rb', line 15

def initialize(*loggers)
  if loggers.count { |logger| logger.is_a?(Lumberjack::ContextLogger) } > 1
    raise ArgumentError, "Only one Lumberjack logger is allowed"
  end

  super
end

#set_progname(value) { ... } ⇒ Object

Alias for with_progname for backward compatibility.

Parameters:

  • value (String)

    the progname to set

Yields:

  • the block to execute with the specified progname

Returns:

  • (Object)

    the result of the block execution



185
186
187
# File 'lib/lumberjack/rails/broadcast_logger_extension.rb', line 185

def set_progname(value, &block)
  dispatch_block_method(:with_progname, value, &block)
end

#tag(attributes) { ... } ⇒ Object

Tag log entries with the specified attributes.

Parameters:

  • attributes (Hash)

    the attributes to tag with

Yields:

  • the block to execute with the tagged context

Returns:

  • (Object)

    the result of the block execution



125
126
127
# File 'lib/lumberjack/rails/broadcast_logger_extension.rb', line 125

def tag(attributes, &block)
  dispatch_block_method(:tag, attributes, &block)
end

#unknown(message_or_progname_or_attributes = nil, progname_or_attributes = nil) { ... } ⇒ void

This method returns an undefined value.

Log an unknown severity message with optional attributes support.

Parameters:

  • message_or_progname_or_attributes (String, Hash) (defaults to: nil)

    the message, progname, or attributes

  • progname_or_attributes (String, Hash) (defaults to: nil)

    the progname or attributes

Yields:

  • optional block that returns the message



89
90
91
92
93
# File 'lib/lumberjack/rails/broadcast_logger_extension.rb', line 89

def unknown(message_or_progname_or_attributes = nil, progname_or_attributes = nil, &block)
  dispatch_to_each(block) do |logger, one_time_block|
    call_with_attributes_arg(logger, :unknown, message_or_progname_or_attributes, progname_or_attributes, &one_time_block)
  end
end

#untagged { ... } ⇒ Object

Execute a block without any tags.

Yields:

  • the block to execute without tags

Returns:

  • (Object)

    the result of the block execution



167
168
169
# File 'lib/lumberjack/rails/broadcast_logger_extension.rb', line 167

def untagged(&block)
  dispatch_block_method(:untagged, &block)
end

#warn(message_or_progname_or_attributes = nil, progname_or_attributes = nil) { ... } ⇒ void

This method returns an undefined value.

Log a warning message with optional attributes support.

Parameters:

  • message_or_progname_or_attributes (String, Hash) (defaults to: nil)

    the message, progname, or attributes

  • progname_or_attributes (String, Hash) (defaults to: nil)

    the progname or attributes

Yields:

  • optional block that returns the message



53
54
55
56
57
# File 'lib/lumberjack/rails/broadcast_logger_extension.rb', line 53

def warn(message_or_progname_or_attributes = nil, progname_or_attributes = nil, &block)
  dispatch_to_each(block) do |logger, one_time_block|
    call_with_attributes_arg(logger, :warn, message_or_progname_or_attributes, progname_or_attributes, &one_time_block)
  end
end

#with_level(level) { ... } ⇒ Object

Override the with_level method defined on the logger gem to use Rails’ log_at method instead.

Parameters:

  • level (Symbol, Integer)

    the log level to set temporarily

Yields:

  • the block to execute at the specified log level

Returns:

  • (Object)

    the result of the block execution



116
117
118
# File 'lib/lumberjack/rails/broadcast_logger_extension.rb', line 116

def with_level(level, &block)
  log_at(level, &block)
end

#with_progname(value) { ... } ⇒ Object

Set the progname temporarily for a block.

Parameters:

  • value (String)

    the progname to set

Yields:

  • the block to execute with the specified progname

Returns:

  • (Object)

    the result of the block execution



176
177
178
# File 'lib/lumberjack/rails/broadcast_logger_extension.rb', line 176

def with_progname(value, &block)
  dispatch_block_method(:with_progname, value, &block)
end