【问题标题】:rails loggers debugging logsrails loggers 调试日志
【发布时间】:2017-10-25 21:51:35
【问题描述】:

我继承了一个中型 Rails 应用程序,其中包含许多不同的日志记录方法,分布在代码库中。有点乱,我不知道为什么有些日志输出了,为什么有些不输出。

记录器有没有办法在创建时“宣布自己”/记录一些状态信息?与设置debug="true"时的logback does类似

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/aleksander.sumowski/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/aleksander.sumowski/.m2/repository/org/slf4j/slf4j-log4j12/1.6.1/slf4j-log4j12-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
11:28:33,076 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
11:28:33,076 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
11:28:33,076 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/Users/aleksander.sumowski/projects/insurance/car-insurance-aggregator/resources/logback.xml]
11:28:33,168 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Will scan for changes in [file:/Users/aleksander.sumowski/projects/insurance/car-insurance-aggregator/resources/logback.xml]
11:28:33,168 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeTask scanning period to 1 seconds
11:28:33,172 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
11:28:33,175 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
11:28:33,179 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
11:28:33,214 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
11:28:33,218 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [FILE]
11:28:33,225 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1006624255 - Will use gz compression
11:28:33,228 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy@1006624255 - Will use the pattern log/archive/car-insurance-aggregator.%d{yyyy-MM-dd}.log for the active file
11:28:33,231 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd' from file name pattern 'log/archive/car-insurance-aggregator.%d{yyyy-MM-dd}.log.gz'.
11:28:33,231 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
11:28:33,234 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Wed Nov 22 11:28:33 UTC 2017
11:28:33,235 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
11:28:33,236 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE] - Active log file name: log/car-insurance-aggregator.log
11:28:33,236 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE] - File property is set to [log/car-insurance-aggregator.log]
11:28:33,238 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [business_events] to INFO
11:28:33,238 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE] to Logger[business_events]
11:28:33,238 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO
11:28:33,238 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
11:28:33,238 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
11:28:33,239 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@6ea1bcdc - Registering current configuration as safe fallback point
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
2017-11-22 11:28:34,266 [main] INFO  org.eclipse.jetty.util.log - Logging initialized @3093ms

【问题讨论】:

  • 也许您正在寻找日志级别? guides.rubyonrails.org/…
  • 我对日志级别的概念很熟悉,这不是我想要的
  • 您能分享一下应用中有哪些记录器吗?
  • 唯一的方法是分别处理每个记录器/日志库。您可能需要检查它们的设置,并且可能需要对其进行猴子补丁以设置一些设置,而不管在初始化时传递了什么
  • 这就是我所害怕的......基本上没有办法做我要求的......

标签: ruby-on-rails logging


【解决方案1】:

是的,这就是红宝石的美丽和痛苦。 我将从覆盖config/application.rb 中的Rails.logger= 方法开始,并带有一些(就像你说的)“宣布”。

只需添加到config/application.rb 类似的内容并查看日志:

Rails.singleton_class.send('alias_method', :old_logger=, :logger=)
module Rails
  def self.logger=(value)
    puts "Hey logger '#{value.inspect}' starting!"
    self.old_logger = value
  end
end

或者只是调查Rails.logger的价值呢?

【讨论】:

  • 这非常接近我的需要!还有两个后续问题:有没有办法知道任务是谁/在哪里完成的?我正在考虑以某种方式打印调用堆栈。第二点:我认为一些日志记录是由 Rack 制作的,而不是 Rails。类似的方法也适用于 Rack 吗?
【解决方案2】:

不知道是不是你要找的,但 Rails 有 5 个不同的日志级别 (http://guides.rubyonrails.org/debugging_rails_applications.html):

2.2 Log Levels

当记录某些内容时,如果消息的日志级别等于或高于配置的日志级别,则将其打印到相应的日志中。如果想知道当前的日志级别,可以调用 Rails.logger.level 方法。

可用的日志级别有::debug、:info、:warn、:error、:fatal 和:unknown,分别对应从 0 到 5 的日志级别编号。要更改默认日志级别,请使用

config.log_level = :warn

【讨论】:

  • 不,这不是我要找的……该应用程序在不同的地方定义了多个记录器。 rack 也可以自己进行一些日志记录。我想了解这些记录器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-04
  • 2016-12-12
  • 2021-04-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多