aserafin / grape_logging

Request logging for Grape!
MIT License
147 stars 76 forks source link

how to append log file to log/development.log #1

Closed TangMonk closed 9 years ago

TangMonk commented 9 years ago
logger Logger.new(GrapeLogging::MultiIO.new(STDOUT, File.open("#{Rails.root}/log/development.log"), 'a'))

But throw me

log writing failed. not opened for writing
aserafin commented 9 years ago

Thanks for creating the issue :) Can you please pass me your grape/rails versions?

TangMonk commented 9 years ago

thanks for your reply, my version:

gem 'rails', '4.1.10'
gem 'grape', '0.11.0'
thimios commented 9 years ago

should it be like the following?

logger = Logger.new( GrapeLogging::MultiIO.new([STDOUT, File.open('log/api.log', 'a')] ) )
logger.formatter = GrapeLogging::Formatters::Default.new
use GrapeLogging::Middleware::RequestLogger, { logger: logger }
aserafin commented 9 years ago

@thimios I think you are on the right path :) The array is not required but I've overlooked the 'a' modifier - it's outside the File.open function - thanks for noticing!

So the original line should look like this:

logger Logger.new(GrapeLogging::MultiIO.new(STDOUT, File.open("#{Rails.root}/log/development.log", 'a')))
aserafin commented 9 years ago

Fixed with commit 43f945badc09a2289c955300d96bf6fcfe79f359

thimios commented 9 years ago

You still have one parenthesis more than needed.

Dont you also need an assignment ( = ) ? Like this:

logger.formatter = GrapeLogging::Formatters::Default.new logger = Logger.new GrapeLogging::MultiIO.new(STDOUT, File.open('path/to/your/logfile.log', 'a')) use GrapeLogging::Middleware::RequestLogger, { logger: logger }

aserafin commented 9 years ago

You are correct about the parenthesis, changed that. But the assignment is not needed, check the grape doc: https://github.com/intridea/grape#logging

thimios commented 9 years ago

great thanks.

I try it like this:

logger Logger.new GrapeLogging::MultiIO.new( STDOUT, File.open('log/api.log', 'a'))

use GrapeLogging::Middleware::RequestLogger, { logger: logger }

the file is created, but it stays empty. Any ideas?

aserafin commented 9 years ago

Yes, that's ruby buffering writes to the file. You need to set sync = true on the log file to see logs immediately http://stackoverflow.com/questions/8948933/what-does-file-sync-true-do

thimios commented 9 years ago

so finally I have this and it works great:

logfile = File.open('log/api.log', 'a')
logfile.sync = true

logger Logger.new GrapeLogging::MultiIO.new(STDOUT, logfile)
logger.formatter = GrapeLogging::Formatters::Default.new
use GrapeLogging::Middleware::RequestLogger, { logger: logger }
aserafin commented 9 years ago

what do you mean?

thimios commented 9 years ago

I am sorry, the last line of my previous comment did not make much sense, so I removed it.