Collect the Rails 4 production.log with td-agent



Hello everybody, It’s me candle.
In this time, let’s get the rails 4 production.log by td-agent.
I have been written same article, but there was lack of explanation and etc, I will rewrite it again.
Also, I will show you how to get Rails 5 production.log at a later date.


You must be able to execute ruby on rails in production mode.
It is ok in any environment as long as it has executable. Such as webrick, nginx, puma and apache.

td-agent or fluentd is instlled.
It is assumed that td-agent or fluentd is installed.

You may change the rails log format.

I think that the format of the production log of rails is the next image by default.

You can change the this log format easily.
The below is a code without any change from the default format.

class Logger::Formatter
  def call(severity, time, progname, msg)
    format = "%s, [%s #%d] %5s -- %s: %sn"
    format % [severity[0], "#{time.strftime('%Y-%m-%dT%H:%M:%S')}.#{'%06d' % time.usec.to_s}", $$, severity, progname, msg2str(msg)]

There is information that is not necessary for rails production log.
It is “Render partial” information.
The below text is sample.

I, [2017-03-24T06:19:15.314646 #5689] INFO — : Rendered users/shared/_links.html.haml (0.5ms)
I, [2017-03-24T06:19:15.315840 #5689] INFO — : Rendered users/registrations/new.html.haml within layouts/application (6.6ms)
I, [2017-03-24T06:19:15.318778 #5689] INFO — : Rendered layouts/_header.html.haml (0.6ms)
I, [2017-03-24T06:19:15.320581 #5689] INFO — : Rendered layouts/_footer.html.haml (0.8ms)

I think that this information increase the log file size
and this information is not necessary in the production mode.
It is ok if you don’t change that or not.

Open the config/environment.rb

Change the format like this.
If you are using erb please change “.haml” part to “.erb”.

class Logger::Formatter
  def call(severity, time, progname, msg)
    if !msg.include?('.haml')
      format = "%s, [%s #%d] %5s -- %s: %sn"
      format % [severity[0], "#{time.strftime('%Y-%m-%dT%H:%M:%S')}.#{'%06d' % time.usec.to_s}", $$, severity, progname, msg2str(msg)]

Setting of rails is over.

Preparing to get rails log with td-agent

Alright, let’s prepare for td-agent side.

My rails and td-agent environment is like this.
Td-agent is running with CentOS 6.5.
Rails application is running on the unicorn + nginx.
Please prepare those servers environment.

Set up td-agent

The biggest problem with rails 4 production log is that Fatal errors are displayed over multiple lines.
Fluentd gets a log each one line.
The multiple line errors are collected as separate logs.

F, [2017-03-24T08:24:51.329266 #6165] FATAL — :
NoMethodError (undefined method `each’ for nil:NilClass):
app/controllers/pages_controller.rb:7:in `about’

So, we use “format multiline”.
This means that the part not matching the regular expression written with “format_firstline” is regarded as the same as the matched part even if it is new line, and it is stored in one log.

Open the /etc/td-agent/td-agent.conf and write it.

  @type tail
  path /usr/share/nginx/alice4/log/production.log
  tag rails.production
  pos_file /var/log/td-agent/rails.production.pos
  format multiline
  format_firstline /^.,/
  format1 /^(?<log_initial>.*), [(?<date>d{1,4}-d{1,2}-d{1,2}Td{1,2}:d{1,2}:d{1,2}.d{1,6}) (?<process_id>[^ ]*)] *(?<log_level>[^ ]*) -- : (?<message>[^']*[^]]*)/

<match rails.production>
  @type stdout

With the “path” reference the production.log in the log folder of the rails project.
Please rewrite the reference path to your environment.

“tag rails.production” tagged a collected log.

With the “pos_file /var/log/td-agent/rails.production.pos” specifies the location of temporary files.

In the “format” declares “multiline”. It is possible to collect logs of multiple lines.

the meaning of “format_firstline / ^., /” regular expression is “1 character +,” at the beginning of the line.
This corresponds to the “I,” part of the rails production log.

“format1” matches the format of rails production.log.

In the “<match rails.production>” gets the tagged log and output it.

This is the setting for td-agent to get rails log.

Getting log and check it

Restart the td-agent.

sudo serice td-agent restart

Lauch the rails application in production.
Let’s access.

The td-agent log is output to the following location.


Check with the tail command.

sudo tail -f /var/log/td-agent/td-agent.log

This is the log.

The most important thing is the part of “FATAL”.

2017-03-24 08:30:11.203171123 +0000 rails.production: {“log_initial”:”F”,”date”:”2017-03-24T08:24:51.329266″,”process_id”:”#6165″,”log_level”:”FATAL”,”message”:”nNoMethodError (undefined method `each’ for nil:NilClass):n app/controllers/pages_controller.rb:7:in `about’nn”}

The messages are gathered to one.


We tried to collect Rails 4 production.log.


If you think this article is good, share it please

-, ,



Your email address will not be published. Required fields are marked *


Build up td-agent server on the EC2 of aws

English 日本語 Abstract Hello everyone, It’s candle. In this time we will build up td-agent environment on the ec2. Precondition You have a EC2.

React + S3 + Route53 + CloudFront HTTPS connection using SSL

Abstract Hello everyone it’s me candle. This time we would like to try SSL connection with the React web application deployed to S3. Programers recently develop web services on the API server and React front end. Then SSL connection is often required on the React side as well. Not only that, but recently https is being standardized. We try to taht. Condition React web is deployed to S3 You have the S3 bucket that name is same as Route53 domain name Create a CloudFront distribution First we will create the distribution of CloudFront. Go to the CloudFront management console, select …

Install only mysql command for RDS in EC2

Abstract Hello everyone it’s me candle. This topic is a little thing but I note to self. If we use RDS, we don’t need Mysql server in the EC2. You may want to install only the mysql command. I will explain it. Precondition You use EC2 and RDS

Monitor the memory state of ec2 with watch and free command

English 日本語 Abstract Hello everyone, It’s candle. In this time, I will show you how to monitor ec2 memory in real time. EC2 server of t2.micro has only 1GB of memory, If you run composer, rails, mysql on it, memory may be barely. It is useful if you can monitor the memory and tune the server. Precondition EC2 server

How to get nginx error.log and access.log with td-agent

English 日本語 Abstract Hello everyone, It’s candle. In this time, we collect nginx log with td-agent. I explain with td-agent, but basic configuration is similer with fluentd. Precondition Td-agent or fluentd is installed You have a nginx server.

I work in the venture company as a CTO. I start to write program in University, first I learned java, C++ and PHP. In the company, I'm developing web services by Rails. I do like to automation.