跳至内容 跳至搜索

Active Support Broadcast Logger

Broadcast logger 用于将消息写入多个 IO。它通常在开发环境中使用,用于将消息显示在 STDOUT 上,同时也将它们写入文件 (development.log)。使用 Broadcast logger,您可以将日志广播到无限制数量的接收器。

BroadcastLogger 表现得像一个标准的 logger,您熟悉的所有方法都可用。但是,此 logger 上的所有方法都将传播并委托给 broadcast 中的其他 logger。

广播您的日志。

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 添加一个 logger。

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.

修改所有广播 logger 的日志级别。

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 中至少必须有一个接收器。否则,您的日志将不会被写入任何地方。例如:

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

如果您向 broadcast 添加一个具有自定义方法的自定义 logger,`BroadcastLogger` 将代理它们并返回原始值,或者返回原始值数组,具体取决于 broadcast 中有多少个 logger 响应了该方法。

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]
方法
B
D
E
F
I
L
N
S
W
包含的模块

Attributes

[R] broadcasts

返回属于此 broadcast 的所有 logger。

[RW] progname

类公共方法

new(*loggers)

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

  broadcast_to(*loggers)
end

实例公共方法

broadcast_to(*loggers)

将 logger 添加到 broadcast。

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

debug!()

将整个 broadcast 的日志级别设置为 Logger::DEBUG

# File activesupport/lib/active_support/broadcast_logger.rb, line 146
def debug!
  dispatch(:debug!)
end

debug?()

如果日志级别允许将具有 Logger::DEBUG 严重级别的条目写入至少一个 broadcast,则返回 true。否则返回 false。

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

error!()

将整个 broadcast 的日志级别设置为 Logger::ERROR

# File activesupport/lib/active_support/broadcast_logger.rb, line 179
def error!
  dispatch(:error!)
end

error?()

如果日志级别允许将具有 Logger::ERROR 严重级别的条目写入至少一个 broadcast,则返回 true。否则返回 false。

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

fatal!()

将整个 broadcast 的日志级别设置为 Logger::FATAL

# File activesupport/lib/active_support/broadcast_logger.rb, line 190
def fatal!
  dispatch(:fatal!)
end

fatal?()

如果日志级别允许将具有 Logger::FATAL 严重级别的条目写入至少一个 broadcast,则返回 true。否则返回 false。

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

info!()

将整个 broadcast 的日志级别设置为 Logger::INFO

# File activesupport/lib/active_support/broadcast_logger.rb, line 157
def info!
  dispatch(:info!)
end

info?()

如果日志级别允许将具有 Logger::INFO 严重级别的条目写入至少一个 broadcast,则返回 true。否则返回 false。

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

initialize_copy(other)

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

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

level()

返回 broadcast 中所有 logger 的最低级别。

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

local_level()

# File activesupport/lib/active_support/broadcast_logger.rb, line 113
def local_level
  loggers = @broadcasts.select { |logger| logger.respond_to?(:local_level) }

  loggers.map do |logger|
    logger.local_level
  end.first
end

local_level=(level)

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

stop_broadcasting_to(logger)

从 broadcast 中移除一个 logger。当一个 logger 被移除时,发送到 broadcast 的消息将不再写入其接收器。

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

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

warn!()

将整个 broadcast 的日志级别设置为 Logger::WARN

# File activesupport/lib/active_support/broadcast_logger.rb, line 168
def warn!
  dispatch(:warn!)
end

warn?()

如果日志级别允许将具有 Logger::WARN 严重级别的条目写入至少一个 broadcast,则返回 true。否则返回 false。

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