Enabling a custom formatter in Rails -
i've written custom formatter rails:
module logging class generalformatter < logger::formatter def call(severity, time, program_name, message) ... end end end
and according rails guide , this answer, have use set
config.log_formatter = logging::generalformatter.new
unfortunately, doesn't seem work—my custom formatting isn't kicking in. , yet, if instead define well:
module logging class generallogger < activesupport::logger def initialize(*args) super(*args) @formatter = generalformatter.new end end end
then can config.logger = logging::generallogger.new
, logs formatted desired.
what doing wrong setting log_formatter
? i'd rather not define own logger when want custom formatting.
edit (responding comments, , adding more detail little more digging):
i'm setting config.log_formatter
in application.rb
, testing in development, , appears working to extent, in calling rails.logger.formatter
gives me custom class. here's behavior i'm seeing on calling rails.logger.warn("test")
:
test
printed out console (followed newline).- then
call
method of formatter entered. - the return value of
call
method never printed out console.
where thought happen (and want happen is):
- the
call
method of formatter entered. - the return value of
call
method printed out console. - nothing else printed.
am misunderstanding how formatters work?
edit again (providing clearer example):
when have defined:
def call(severity, time, program_name, message) puts "checkpoint 1" "checkpoint 2" end
and call rails.logger.warn("checkpoint 0")
, expect see:
checkpoint 1 checkpiont 2
but instead see:
checkpoint 0 checkpoint 1
okay, let's try again.
i don't have answer -- instead, unable reproduce problem. give steps took , saw, perhaps able compare you've done see how it's different.
- you didn't version of rails have, started latest 4.1.1. built brand new application
rails new logtest
.
added file @ ./lib/custom_formatter.rb
looks this:
class customformatter < logger::formatter def call(severity, time, program_name, message) # think makes no sense have raw `puts` # in log formatter, i'll leave in # copy you. bad idea though. # commented out. puts "checkpoint 1" return "checkpoint 2" end end
added existing config/application.rb
:
require 'custom_formatter' config.log_formatter = customformatter.new
now start application rails server
, , access http://localhost:3000/test
. expect error message, because haven't defined routes or controllers or anything. because of weird custom logger, expect log lines replaced "checkpoint 1" , "checkpoint 2". indeed that's what's happened. console looks this:
[2014-06-03 18:22:49] info webrick 1.3.1 [2014-06-03 18:22:49] info ruby 1.9.3 (2013-02-22) [x86_64-darwin12.2.1] [2014-06-03 18:22:49] info webrick::httpserver#start: pid=57113 port=3000 checkpoint 1 checkpoint 2checkpoint 1 checkpoint 1 checkpoint 2checkpoint 1 checkpoint 1 checkpoint 2checkpoint 1 checkpoint 1 checkpoint 2checkpoint 1 checkpoint 1 checkpoint 2checkpoint 1 checkpoint 1 checkpoint 2checkpoint 1 checkpoint 1 checkpoint 2checkpoint 1
my development.log looks this:
checkpoint 2checkpoint 2checkpoint 2checkpoint 2checkpoint 2checkpoint 2checkpoint 2
so custom 'formatter' isn't doing useful -- , recommend against putting straight 'puts' line in real formatter.
but custom formatter indeed being used, log lines 'formatted' fixed string 'checkpoint 2' (with no newline @ end!), , additional "checkpoint 1" puts stdout.
so problem not reproducible steps you've given, if try you've done, dont' have problem. so, indeed how debugging works, frustrating! suggest follow steps create test app above , see if works too. you've got compare actual app little test app , figure out how it's different, manifest problem in actual app. 1 common way make copy of actual app, , begin stripping out can, barest possible app still manifests problem. odds are, in doing this, you'll figure out cause of problem -- if don't, can make bare bones demonstration app available others trying you, , can maybe figure out in causing problem.
Comments
Post a Comment