Tips – parse log file tips

Posted by AllenWei | Posted in Tips | Posted on 15-02-2010-05-2008

View Comments




<brseparate variables by “|” # Analyze
field_names = [:level, :time, :pid, :location, :token, :query, :proxy, :action, :cost, :msg]
log_data = Hash[*field_names.zip(parts).flatten]

Then you can easily get all your variables

puts log_data[:level]
puts log_data[:time]
  1. Combined log files analyze
#code in analyzer.rb
ARGF.each_line do |line|
  puts line
end

Then

ruby analyzer.rb log1.log log2.log


Rotate your log file

Posted by AllenWei | Posted in RubyOnRails | Posted on 15-02-2010-05-2008

View Comments

Configuring

config file locate at /etc/logrotate.conf

Sample:

   # Rotate Rails application logs
   /path/to/your/rails/applicaton/log/*.log {
     daily
     missingok
     rotate 7
     compress
     delaycompress
     notifempty
     copytruncate
   }
  • /path/to/your/rails/applicaton/log/*.log - Use the full path to the log directory of your Rails application (symbolic links are acceptable too). “*.log” will rotate any file in the log directory with .log at the end. If you only want to rotate certain log files you can be more specific.
  • daily - Rotates the log files every day. You could specify weekly or monthly instead.
  • missingok - Don’t issue an error message if log files are missing.
  • rotate 7 - The maximum number of log files to keep. Once you have more than this number, the oldest file will be deleted. I set it to keep seven days worth but feel free to change this number.
  • compress - Compress old versions of log files to save space (uses gzip by default).
  • delaycompress - Delays the compression until the next log rotation. It’s a minor point and probably not strictly necessary, but it makes sure that the log file is truly no longer active before compressing it.
  • notifempty - If the log file is empty, there’s no need to rotate it. You can remove this option if you want to rotate even blank log files; just keep in mind that you may erase a log file that has lots of information to make room for your blank log file.
  • copytruncate - Makes a backup copy of the current log and then clears the log file for continued writing. The alternative is to use create which will perform the rotation by renaming the current file and then creating a new log file with the same name as the old file. I strongly recommend that you use copytruncate unless you know that you need create. The reason why is that Rails, FastCGI, Mongrel, etc. may still keep pointing to the old log file even though its name has changed and they may require restarting to locate the new log file. copytruncate avoids this by keeping the same file as the active file.

If you have more than one Rails application, you can repeat this code to rotate them all one-after-another. There other options you can specify, man logrotate will show you them all. I haven’t used them but the options to mail log files on creation or deletion look interesting. It is also possible to have rotate the logs once they get to a certain size instead of at a certain time.

Using logrotate

To use the log rotation you just configured, you have two choices.

  1. Wait for the next day (or whatever time period you specified). If you configured it correctly, rotation should occur automatically and without further commands.
  2. Run it immediately by typing /usr/sbin/logrotate -f /etc/logrotate.conf on the command line. (The -f is for “force” it to run now.)

That’s all there is to it! Now your log files won’t fill up to an unmanagable size yet you’ll still be able to go back and track any recent errors.

By default, logrotate will add cron task to /etc/cron.daily/logrotate. So you don’t need to worry how to add cron task.

analyze rails log using request-log-analyzer

Posted by AllenWei | Posted in RubyOnRails | Posted on 15-02-2010-05-2008

View Comments

Install request-log-analyzer

gem install request-log-analyzer

How to use

request-log-analyzer log/production.log #print result to console
request-log-analyzer log/production.log -o HTML -email someone@company.com #generate html report and send email

More:

http://github.com/wvanbergen/request-log-analyzer/

Ads Plugin created by Jake Ruston's Wordpress Plugins - Powered by and football database.