我正在使用Rails 5,并且我在 environment / production.rb
上使用此代码段将应用程序日志发送到papertrail<div id="sd-collection"></div>
有时会延迟将日志发送到papertrail,所以我手动config.logger = ActiveSupport::TaggedLogging.new(
RemoteSyslogLogger.new(
'logs6.papertrailapp.com', 41364,
program: "rails-#{Rails.env}"
)
)
但是由于日志被发送到papertrail,它没有显示任何内容。
要查看有问题的日志,我需要将 config.logger 替换为
tail -f production.log
Rails中有没有办法可以在同一环境中使用多个记录器?基本上我想将日志发送到papertrail或使用tailed logs手动查看日志?
答案 0 :(得分:1)
您可以使用自定义记录器扩展Rails.logger:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Basic Bootstrap Template</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<script type = "text/javascript" src="d3/d3.v2.js"></script>
<script type = "text/javascript" src="d3/d3.v3.min.js"></script>
</head>
<body>
<div class="jumbotron text-center">
<h1>Clinical Data Analysis</h1>
<p>Global Performance</p>
</div>
<svg class="chart" width="40" height="40"></svg>
<p>Global Performance</p>
<script>
var svg = d3.select("svg"),
margin = 200,
width = svg.attr("width") - margin,
height = svg.attr("height") - margin;
var xScale = d3.scaleBand().range ([0, width]).padding(0.4),
yScale = d3.scaleLinear().range ([height, 0]);
var g = svg.append("g")
.attr("transform", "translate(" + 100 + "," + 100 + ")");
d3.csv("XYZ.csv", function(error, data) {
if (error) {
throw error;
}
xScale.domain(data.map(function(d) { return d.year; }));
yScale.domain([0, d3.max(data, function(d) { return d.value; })]);
g.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(xScale));
g.append("g")
.call(d3.axisLeft(yScale).tickFormat(function(d){
return "$" + d;
}).ticks(10))
.append("text")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("text-anchor", "end")
.text("value");
});
</script>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
</svg>
</body>
</html>
您可以在初始化文件中执行此操作,也可以直接在环境配置文件中执行此操作,但您更喜欢这样做。
答案 1 :(得分:0)
Kinda的旧问题,但我只是满足相同的需求,下面是我如何解决的问题:
class LoggerProxy def initialize @loggers = Set.new end def add(logger) @loggers.add(logger) end def remove(logger) @loggers.delete(logger) end def method_missing(name, *args, &block) @loggers.each do |logger| logger.public_send(name, *args, &block) end end end
config.logger = LoggerProxy.new config.logger.add(Logger.new(Rails.root.join('log', "#{Rails.env}.log"), 10, 50.megabytes)) config.logger.add(ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)))