Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I disable the log header for ruby logger?

I'm currently running into kind of a problem.

As you might know, the ruby logger adds a logging header at the top of every newly created logfile.

"# Logfile created on %s by %s\n" % [Time.now.to_s, Logger::ProgName]

I am logging CSV files to import them in a warehouse later, usually I just skip the first line with the header. It's seems like there is a bug in the logger, because sometimes the logging header appears more than once, right in the middle of a log file.

So I decided to simply leave that header out. To my surprise I didn't find any argument one could pass at the creation of a logger. I thought of something like this:

Logger.new "info.log", :skip_header => true

But it's just not there. I searched in the ruby core sources and surprisingly there really is nothing that could prevent the logger from adding the log header:

def create_logfile(filename)
  logdev = open(filename, (File::WRONLY | File::APPEND | File::CREAT))
  logdev.sync = true
  add_log_header(logdev)
  logdev
end

def add_log_header(file)
  file.write(
    "# Logfile created on %s by %s\n" % [Time.now.to_s, Logger::ProgName]
)
end

Does anyone have an idea what I could do, to prevent the log header? I'm using Ruby 1.8.7 302 with Rails 2.3.5 here. Simply ignoring the comments on the warehouse side is not possible because I have no control over the code there, and it seems to be to risky to simply ignore it, if something goes wrong with a a logging line.

Does someone know a logger that allows this? Do you think it would be a good idea to use and write plain to a file?

Thanks in advance, Tommy

like image 803
Thomas Fankhauser Avatar asked Nov 04 '10 11:11

Thomas Fankhauser


People also ask

Where are Ruby logs stored?

In a Rails app, logs are stored under the /log folder.

What is logger in ruby?

The Logger class provides a simple but sophisticated logging utility that you can use to output messages. The messages have associated levels, such as INFO or ERROR that indicate their importance. You can then give the Logger a level, and only messages at that level or higher will be printed. The levels are: UNKNOWN.

Where does Rails logger info write to?

By default it puts these log files in the log/ directory of your project. So if you open up that folder you'll see a development.

How do I create a log file in Ruby?

Since the logger library comes with Ruby, there's no need to install any gems or other libraries. To begin using the logger library, simply require 'logger' and create a new Logger object. Any messages written to the Logger object will be written to the log file.


1 Answers

Ideally the method add_log_header on the Logger instance should be overwritten, but since add_log_header is called on initialize, you're too late by the time you get your hands on it. Well, you could just overwrite the add_log_header method on the Class.

class Logger::LogDevice
  def add_log_header(file)
  end
end

log1 = Logger.new('info1.log')

But if your app needs more instances of Logger after this, they will behave the same: no header. To prevent this:

# dismantle the header and save it under another name
class Logger::LogDevice
  alias orig_add_log_header add_log_header

  def add_log_header(file)
  end
end

# Quick,create an instance 
log1 = Logger.new('test_log1file.log')

# restore the old method:
class Logger::LogDevice  
  alias add_log_header orig_add_log_header 
end
like image 135
steenslag Avatar answered Oct 05 '22 22:10

steenslag