我有rails 3应用程序,它会生成很多分析请求。不幸的是,这会淹死日志,我失去了我真正关心的主要页面请求。我想分开这些请求到一个单独的日志文件。有没有办法指定某些操作去某个日志文件?或者可能是减少这些操作的日志级别的方法,然后在回读日志文件时仅显示某些级别的日志?每次操作分离rails日志
1
A
回答
6
我找到了this site,谈到了使用中间件来消除日志动作。我使用了同样的想法,最后编写了一个中间件,根据被调用的操作来交换记录器。这里是中间件,它在我把LIB/noisy_logger.rb
class NoisyLogger < Rails::Rack::Logger
def initialize app, opts = {}
@default_log = Rails.logger
# Put the noisy log in the same directory as the default log.
@noisy_log = Logger.new Rails.root.join('log', 'noisy.log')
@app = app
@opts = opts
@opts[:noisy] = Array @opts[:noisy]
super app
end
def call env
if @opts[:noisy].include? env['PATH_INFO']
logfile = @noisy_log
else
logfile = @default_log
end
# What?! Why are these all separate?
ActiveRecord::Base.logger = logfile
ActionController::Base.logger = logfile
Rails.logger = logfile
# The Rails::Rack::Logger class is responsible for logging the
# 'starting GET blah blah' log line. We need to call super here (as opposed
# to @app.call) to make sure that line gets output. However, the
# ActiveSupport::LogSubscriber class (which Rails::Rack::Logger inherits
# from) caches the logger, so we have to override that too
@logger = logfile
super
end
end
然后这正好在配置/初始化/ noisy_log.rb
MyApp::Application.config.middleware.swap(
Rails::Rack::Logger, NoisyLogger, :noisy => "/analytics/track"
)
希望帮助别人!
0
一个选项可能会使用像New Relic这样的服务,它会为您提供所需的范围设定(每个操作)。
相关问题
- 1. Rails日志记录127.0.0.1每5分钟
- 2. Rails 4 - 每个动作禁用日志
- 3. AspectJ操作日志
- 4. WSO2操作日志清空
- 5. MongoDB操作日志流Node.js
- 6. 詹金斯操作日志
- 7. 天青 - 操作日志api
- 8. Heroku - 如何在每次请求后刷新Rails日志
- 9. 分离tomcat日志文件和应用程序日志文件
- 10. 如何分离日志实现和API?
- 11. Log4j日志记录以分离文件
- 12. 从java.util.logging中分离INFO和ERROR日志
- 13. Hybris-分离hybris和控制台日志
- 14. Jboss/Wildfly日志分离文件
- 15. 分别获取函数的日志每次在Python
- 16. Nlog - 每x分钟发送一次电子邮件日志
- 17. 用Python每15分钟检查一次日志文件
- 18. 在Azure操作日志中关闭日志记录pfx密码
- 19. Bigtable是否将操作写入日志以进行每个操作或批量操作?
- 20. phpmyadmin操作日志存储在哪里?
- 21. Spring Boot Admin中的日志操作
- 22. MVC4操作方法日志记录
- 23. 在日志记录和nologging表操作
- 24. Log4j的默认关闭操作日志
- 25. Android php mysql,不要操作日志
- 26. 亚马逊S3日志操作定义
- 27. 日志延迟加载操作
- 28. 日志层次与根日志?
- 29. Log4j2每日日志文件
- 30. 两次提交出现在每次合并的日志中
尝试http://agilewebdevelopment.com/plugins/tagged_logger –