Active Record messes with my Logger

D

Damphyr

Yes, I know about the hack that AR uses to make the Logger format
friendlier.
The thing is it imposes it's idea of friendliness in a global scale (as
I remember it was necessary with the 1.8.2 Logger due to the rigidness
of the format). This seriously gets on my nerves.

How can I reinstate my Logger format and apply it to active record?
It is and it's not really a Rails question, as I only use AR :)
Cheers,
V.-
 
F

Florian Aßmann

# I found no clean sollution, but maybe this can help ya:
#
begin
define = 'def format_message severity, timestamp, program_name,
message'
default = '( @formatter || @default_formatter ).call ' +
'severity, timestamp, program_name, message'

format ||= default

Logger.class_eval [ define, format, 'end' ].join('; ')
end
#
# You can define format beforehand to something like '"#
{ timestamp }: message"'
# Per default it's overwritten with Logger.format_message method
#
# Cheers
# Flo

Am 01.12.2006 um 15:26 schrieb Damphyr:
 
V

Vassilis Rizopoulos

Florian said:
# I found no clean sollution, but maybe this can help ya:
#
begin
define = 'def format_message severity, timestamp, program_name,
message'
default = '( @formatter || @default_formatter ).call ' +
'severity, timestamp, program_name, message'

format ||= default

Logger.class_eval [ define, format, 'end' ].join('; ')
end
#
# You can define format beforehand to something like '"#{ timestamp }:
message"'
# Per default it's overwritten with Logger.format_message method
#
# Cheers
# Flo
Oh, I found a much cleaner solution:
class MyLogFormatter<Logger::Formatter
MyLogFormat = "[%s] %5s -- %s: %s\n"
def call severity,time,progname,msg
MyLogFormat % [format_datetime(time), severity, progname,
msg2str(msg)]
end
end
logger.formatter=MyLogFormatter.new
logger.formatter.datetime_format="%Y%m%d %H:%M:%S"

It's not exactly documented (at least the RDoc in ruby-doc stil contains
the comment about having to hack the Format constant). This way you can
play with the format constant used and not mess with the logger internals.
Cheers,
V.-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,170
Messages
2,570,921
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top