简体   繁体   English

rails loggers调试日志

[英]rails loggers debugging logs

I've inheritted a medium sized Rails app with lots of different logging approaches spread across the codebase. 我已经继承了一个中型Rails应用程序,在代码库中分布了许多不同的日志记录方法。 It's a bit of a mess and I'm lost why some logs are outputed and why some not. 这有点乱,我失去了为什么有些日志被淘汰,为什么有些没有。

Is there a way for the loggers to "announce themselves"/log some status information when being created? 有没有办法让记录器在创建时“宣布自己”/记录一些状态信息? In a similar way to what logback does when you set debug="true" 与设置debug="true"时的logback操作类似

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

Yeah, this is the beauty and the pain of ruby. 是的,这是红宝石的美丽和痛苦。 I would start with overriding Rails.logger= method in config/application.rb with some (like you say) "announce". 我将首先在config/application.rb覆盖Rails.logger=方法(如你所说)“宣布”。

Just add to config/application.rb something like that and watch the logs: 只需添加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

Or what about just investigating of value of Rails.logger ? 或者只是调查Rails.logger的价值呢?

Don't know if it's this you're looking for, but Rails has 5 different log levels ( http://guides.rubyonrails.org/debugging_rails_applications.html ): 不知道你是否正在寻找它,但是Rails有5种不同的日志级别( http://guides.rubyonrails.org/debugging_rails_applications.html ):

2.2 Log Levels

When something is logged, it's printed into the corresponding log if the log level of the message is equal to or higher than the configured log level. 记录某些内容时,如果消息的日志级别等于或高于配置的日志级别,则会将其打印到相应的日志中。 If you want to know the current log level, you can call the Rails.logger.level method. 如果您想知道当前的日志级别,可以调用Rails.logger.level方法。

The available log levels are: :debug, :info, :warn, :error, :fatal, and :unknown, corresponding to the log level numbers from 0 up to 5, respectively. 可用的日志级别为:: debug,:info,:warn,:error,:fatal和:unknown,分别对应于0到5之间的日志级别编号。 To change the default log level, use 要更改默认日志级别,请使用

config.log_level = :warn config.log_level =:警告

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM