2

I am developing a simple web service with Sinatra/Thin on Windows

In my app, I have the following to enable logging to a file:

Dir.mkdir('log') unless File.exists?('log')
use Rack::CommonLogger, File.new('log/access.log', 'w')

When the server starts up, the file is created. However, nothing is written to the file until I shut down the server.

On the other hand, when I use Logger, like this:

logger = Logger.new('log/access.log', 10, 1024000)
use Rack::CommonLogger, logger

it does not even write entries to the log file, except for a line that says:

# Logfile created on 2013-02-13 11:23:45 +0200 by logger.rb/31641

What I need is:

  • Log entries to be written immediately
  • To be able to Logger for the log rotation feature
mydoghasworms
  • 18,233
  • 11
  • 61
  • 95

2 Answers2

1

I've been using this solution in my sinatra apps (subclassing Sinatra::Base) before do
env['rack.logger'] = Logger.new("log/#{App.environment}.log")
logger.datetime_format = "%Y/%m/%d @ %H:%M:%S "
logger.level = Logger::INFO if App.production?
end

Coelhone
  • 310
  • 1
  • 11
0

Using a Sinatra Classic Style Application, setting the attribute sync to true of the file for logging, works for me. In production and development:

Logger.class_eval { alias :write :'<<' }
flogger = File.open("log/app.log","a")
flogger.sync = true # This is what makes to write always
logger = Logger.new(flogger)
use Rack::CommonLogger, logger

Sources:

Community
  • 1
  • 1
David Clavijo
  • 387
  • 1
  • 11