跳至内容 跳至搜索

Active Support 广播 Logger

广播日志记录器是一个用于将消息写入多个 IO 的记录器。它通常在开发中用于在 STDOUT 上显示消息,并将它们写入文件 (development.log)。使用广播日志记录器,您可以将日志广播到无限数量的接收器。

BroadcastLogger 充当标准记录器,所有您习惯使用的​​方法都可用。但是,此记录器上的所有方法都将传播并委托给广播中包含的其他记录器。

广播您的日志。

stdout_logger = Logger.new(STDOUT)
file_logger   = Logger.new("development.log")
broadcast = BroadcastLogger.new(stdout_logger, file_logger)

broadcast.info("Hello world!") # Writes the log to STDOUT and the development.log file.

向广播添加记录器。

stdout_logger = Logger.new(STDOUT)
broadcast = BroadcastLogger.new(stdout_logger)
file_logger   = Logger.new("development.log")
broadcast.broadcast_to(file_logger)

broadcast.info("Hello world!") # Writes the log to STDOUT and the development.log file.

修改所有广播记录器的日志级别。

stdout_logger = Logger.new(STDOUT)
file_logger   = Logger.new("development.log")
broadcast = BroadcastLogger.new(stdout_logger, file_logger)

broadcast.level = Logger::FATAL # Modify the log level for the whole broadcast.

停止将日志广播到接收器。

stdout_logger = Logger.new(STDOUT)
file_logger   = Logger.new("development.log")
broadcast = BroadcastLogger.new(stdout_logger, file_logger)
broadcast.info("Hello world!") # Writes the log to STDOUT and the development.log file.

broadcast.stop_broadcasting_to(file_logger)
broadcast.info("Hello world!") # Writes the log *only* to STDOUT.

至少有一个接收器必须是广播的一部分。否则,您的日志将不会写入任何地方。例如

broadcast = BroadcastLogger.new
broadcast.info("Hello world") # The log message will appear nowhere.

如果您正在向广播添加具有自定义方法的自定义记录器,则 ‘BroadcastLogger` 将代理它们并返回原始值或原始值的数组,具体取决于广播中响应该方法的记录器数量

class MyLogger < ::Logger
  def loggable?
    true
  end
end

logger = BroadcastLogger.new
logger.loggable? # => A NoMethodError exception is raised because no loggers in the broadcasts could respond.

logger.broadcast_to(MyLogger.new(STDOUT))
logger.loggable? # => true
logger.broadcast_to(MyLogger.new(STDOUT))
puts logger.broadcasts # => [MyLogger, MyLogger]
logger.loggable? # [true, true]
方法
#
A
B
C
D
E
F
I
L
N
S
U
W
包含的模块

属性

[R] broadcasts

返回此广播中包含的所有记录器。

[R] formatter
[RW] progname

类公共方法

new(*loggers)

# File activesupport/lib/active_support/broadcast_logger.rb, line 82
def initialize(*loggers)
  @broadcasts = []
  @progname = "Broadcast"

  broadcast_to(*loggers)
end

实例公共方法

<<(message)

# File activesupport/lib/active_support/broadcast_logger.rb, line 112
def <<(message)
  dispatch { |logger| logger.<<(message) }
end

add(...)

也称为: log
# File activesupport/lib/active_support/broadcast_logger.rb, line 116
def add(...)
  dispatch { |logger| logger.add(...) }
end

broadcast_to(*loggers)

将记录器(s)添加到广播。

broadcast_logger = ActiveSupport::BroadcastLogger.new
broadcast_logger.broadcast_to(Logger.new(STDOUT), Logger.new(STDERR))
# File activesupport/lib/active_support/broadcast_logger.rb, line 93
def broadcast_to(*loggers)
  @broadcasts.concat(loggers)
end

close()

# File activesupport/lib/active_support/broadcast_logger.rb, line 162
def close
  dispatch { |logger| logger.close }
end

debug(...)

# File activesupport/lib/active_support/broadcast_logger.rb, line 121
def debug(...)
  dispatch { |logger| logger.debug(...) }
end

debug!()

将整个广播的日志级别设置为 Logger::DEBUG。

# File activesupport/lib/active_support/broadcast_logger.rb, line 173
def debug!
  dispatch { |logger| logger.debug! }
end

debug?()

True 如果日志级别允许将严重性为 Logger::DEBUG 的条目写入至少一个广播。False 否则。

# File activesupport/lib/active_support/broadcast_logger.rb, line 168
def debug?
  @broadcasts.any? { |logger| logger.debug? }
end

error(...)

# File activesupport/lib/active_support/broadcast_logger.rb, line 133
def error(...)
  dispatch { |logger| logger.error(...) }
end

error!()

将整个广播的日志级别设置为 Logger::ERROR。

# File activesupport/lib/active_support/broadcast_logger.rb, line 206
def error!
  dispatch { |logger| logger.error! }
end

error?()

True 如果日志级别允许将严重性为 Logger::ERROR 的条目写入至少一个广播。False 否则。

# File activesupport/lib/active_support/broadcast_logger.rb, line 201
def error?
  @broadcasts.any? { |logger| logger.error? }
end

fatal(...)

# File activesupport/lib/active_support/broadcast_logger.rb, line 137
def fatal(...)
  dispatch { |logger| logger.fatal(...) }
end

fatal!()

将整个广播的日志级别设置为 Logger::FATAL。

# File activesupport/lib/active_support/broadcast_logger.rb, line 217
def fatal!
  dispatch { |logger| logger.fatal! }
end

fatal?()

True 如果日志级别允许将严重性为 Logger::FATAL 的条目写入至少一个广播。False 否则。

# File activesupport/lib/active_support/broadcast_logger.rb, line 212
def fatal?
  @broadcasts.any? { |logger| logger.fatal? }
end

formatter=(formatter)

# File activesupport/lib/active_support/broadcast_logger.rb, line 145
def formatter=(formatter)
  dispatch { |logger| logger.formatter = formatter }

  @formatter = formatter
end

info(...)

# File activesupport/lib/active_support/broadcast_logger.rb, line 125
def info(...)
  dispatch { |logger| logger.info(...) }
end

info!()

将整个广播的日志级别设置为 Logger::INFO。

# File activesupport/lib/active_support/broadcast_logger.rb, line 184
def info!
  dispatch { |logger| logger.info! }
end

info?()

True 如果日志级别允许将严重性为 Logger::INFO 的条目写入至少一个广播。False 否则。

# File activesupport/lib/active_support/broadcast_logger.rb, line 179
def info?
  @broadcasts.any? { |logger| logger.info? }
end

initialize_copy(other)

# File activesupport/lib/active_support/broadcast_logger.rb, line 221
def initialize_copy(other)
  @broadcasts = []
  @progname = other.progname.dup
  @formatter = other.formatter.dup

  broadcast_to(*other.broadcasts.map(&:dup))
end

level()

# File activesupport/lib/active_support/broadcast_logger.rb, line 108
def level
  @broadcasts.map(&:level).min
end

level=(level)

也称为: sev_threshold=
# File activesupport/lib/active_support/broadcast_logger.rb, line 151
def level=(level)
  dispatch { |logger| logger.level = level }
end

local_level=(level)

# File activesupport/lib/active_support/broadcast_logger.rb, line 156
def local_level=(level)
  dispatch do |logger|
    logger.local_level = level if logger.respond_to?(:local_level=)
  end
end

log(...)

别名: add

sev_threshold=(level)

别名: level=

stop_broadcasting_to(logger)

从广播中移除一个日志记录器。移除日志记录器后,发送到广播的消息将不再写入其接收器。

sink = Logger.new(STDOUT)
broadcast_logger = ActiveSupport::BroadcastLogger.new

broadcast_logger.stop_broadcasting_to(sink)
# File activesupport/lib/active_support/broadcast_logger.rb, line 104
def stop_broadcasting_to(logger)
  @broadcasts.delete(logger)
end

unknown(...)

# File activesupport/lib/active_support/broadcast_logger.rb, line 141
def unknown(...)
  dispatch { |logger| logger.unknown(...) }
end

warn(...)

# File activesupport/lib/active_support/broadcast_logger.rb, line 129
def warn(...)
  dispatch { |logger| logger.warn(...) }
end

warn!()

将整个广播的日志级别设置为 Logger::WARN。

# File activesupport/lib/active_support/broadcast_logger.rb, line 195
def warn!
  dispatch { |logger| logger.warn! }
end

warn?()

如果日志级别允许将严重性为 Logger::WARN 的条目写入至少一个广播,则为 True。否则为 False

# File activesupport/lib/active_support/broadcast_logger.rb, line 190
def warn?
  @broadcasts.any? { |logger| logger.warn? }
end