Ruby On Rails in UA/Rails plugins и фичи/Расширенное логированеи с помощью log4r/pretty logs for rails 1.2 and 2.0

10 июня 2008, 08:05
admin
Ruslan Voloshin
Живет: Odessa,UKR
Сообщений: 2160
Рейтинг: 532.0
Рег: 13 марта 2007
Его блог
pretty logs for rails 1.2 and 2.0
  •  
Вызов логера с блоком logger.debug('debug from log'){ "Received connection from" } logger.info('info from log') logger.info("MainApp"){ "Received connection from" } rails 1.2.6 [DEBU|# 5880|2008-03-11 16:07:47] debug from log: Received connection fr [INFO|# 5880|2008-03-11 16:07:47] : info from log [INFO|# 5880|2008-03-11 16:07:47] MainApp: Received connection from rails 2.0.2 [DEBU|# 5880|2008-03-11 16:07:47] debug from log [INFO|# 5880|2008-03-11 16:07:47] : info from log [INFO|# 5880|2008-03-11 16:07:47] MainApp И так после путем долгий патчей получилась такая версия которая работает с обеими версиями rails 1.2x and 2.0 и так подключаем app/config/environment.rb Rails::Initializer.run do |config| .... end require 'logger_core_ext' Содержимое app/lib/logger_core_ext.rb =begin ====app/config/environment.rb require 'logger_core_ext' =end class Logger # Used for rails 1.2.x alias format_message old_format_message class Formatter def call(severity, time, progname, msg) "[%s|#%5d|%s] %s: %s\n" % [severity[0..3], $$, time.to_s(:db), progname, msg2str(msg)] end end end module ActiveSupport # Used for rails 2.0 class BufferedLogger module Severity def level_to_s(level) case level when 0 'DEBUG' when 1 'INFO' when 2 'WARN' when 3 'ERROR' when 4 'FATAL' when 5 'UNKNOWN' end end end def add(severity, message = nil, progname = nil, &block) return if @level > severity # message = (message || (block && block.call) || progname).to_s message = (message || (block && block.call) || progname).to_s # If a newline is necessary then create a new message ending with a newline. # Ensures that the original message is not mutated. message = "#{message}\n" unless message[-1] == ?\n message = "[%s|#%5d|%s] %s: %s" % [level_to_s(severity)[0..3], $$, Time.now.to_s(:db), progname, message] @buffer << message auto_flush message end end end
------------
Операции с WM | SEO консультации
, ,

Гости: 173 Онлайн: 0