2

In my Rails project, I noticed that sometimes actions are not written into the log file development.log immediately after they are done, but rather after some other actions are done (or maybe just after some amount of time, I'm not sure.) Exactly when is the development.log file written?

Paul S.
  • 4,362
  • 10
  • 35
  • 52

2 Answers2

4

Updated for Rails 3.2

The default logging for Rails is buffered, so log message are not written immediately, but happen in batches. You can force the logger to flush the buffer with:

That was the old way, as of 3.2 the filesystem handles buffering before writing the log:

ActiveSupport::BufferedLogger#auto_flushing is deprecated. Either set the sync level on the underlying file handle like this. Or tune your filesystem. The FS cache is now what controls flushing.

You can change your config/environments/development.rb to auto flush with every log message:

config.autoflush_log = true

This is used by bootstrap.rb to force the sync of the log (e.g. f.sync = true). This will noticeable impact performance.

mguymon
  • 8,946
  • 2
  • 39
  • 61
  • Hmm I see.. so what is the default auto-flush period? How many messages must pile up before the logger flushes? – Paul S. Oct 22 '12 at 20:24
  • This has changed as of 3.2, the file system controls when the log is flushed to the file. – mguymon Oct 22 '12 at 21:32
2

why don't you use log4r?

  1. it is very fast
  2. it supports the timestamp
  3. you can customize the output format as you wish.

for usage, please refer to: https://stackoverflow.com/a/5959013/445908

Community
  • 1
  • 1
Siwei
  • 19,858
  • 7
  • 75
  • 95