Class: BackgroundQueue::Worker::Logger
- Inherits:
-
Logger
- Object
- Logger
- BackgroundQueue::Worker::Logger
- Defined in:
- lib/background_queue/worker/logger.rb
Class Method Summary collapse
- .build_logger(path, task_key, level) ⇒ Object
- .init_logger(worker_name, owner_key, job_key, task_key, level) ⇒ Object
Instance Method Summary collapse
- #format_message(severity, datetime, progname, msg) ⇒ Object
-
#initialize(logdev, task_key) ⇒ Logger
constructor
A new instance of Logger.
- #revert_to_previous_state ⇒ Object
- #set_previous_state(previous_state) ⇒ Object
Constructor Details
#initialize(logdev, task_key) ⇒ Logger
Returns a new instance of Logger.
4 5 6 7 |
# File 'lib/background_queue/worker/logger.rb', line 4 def initialize(logdev, task_key) super(logdev, 0, 1048576) @default_formatter = BackgroundQueue::Worker::LogFormatter.new(task_key) end |
Class Method Details
.build_logger(path, task_key, level) ⇒ Object
50 51 52 53 54 |
# File 'lib/background_queue/worker/logger.rb', line 50 def self.build_logger(path, task_key, level) logger = BackgroundQueue::Worker::Logger.new(path, task_key) logger.level = level logger end |
.init_logger(worker_name, owner_key, job_key, task_key, level) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/background_queue/worker/logger.rb', line 17 def self.init_logger(worker_name, owner_key, job_key, task_key, level) logger = build_logger("#{BackgroundQueue::Utils.current_root}/log/workers/#{worker_name}-#{owner_key}-#{job_key}.log", task_key, level) prev_state = {} #we also want to have active record use this log file if defined? ActiveRecord && defined? ActiveRecord::Base && ActiveRecord::Base.class.respond_to?(:logger) begin prev_state[:ar_base] = ActiveRecord::Base.logger ActiveRecord::Base.logger = logger if ActiveRecord::Base.respond_to?(:active_connections) #set the logger for connections that are already loaded ActiveRecord::Base.active_connections.each_value { |con| con.set_logger(ActiveRecord::Base.logger)} end rescue Exception=>e logger.debug("Error setting up active record logger: #{e.}") end end #and RAILS_DEFAULT_LOGGER if defined?(RAILS_DEFAULT_LOGGER) prev_state[:rails_default_logger] = RAILS_DEFAULT_LOGGER Object.redefine_const( :RAILS_DEFAULT_LOGGER, logger ) end logger.set_previous_state(prev_state) unless prev_state.empty? logger end |
Instance Method Details
#format_message(severity, datetime, progname, msg) ⇒ Object
13 14 15 |
# File 'lib/background_queue/worker/logger.rb', line 13 def (severity, datetime, progname, msg) (@formatter || @default_formatter).call(severity, datetime, progname, msg) end |
#revert_to_previous_state ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/background_queue/worker/logger.rb', line 56 def revert_to_previous_state unless @previous_state.nil? if @previous_state[:ar_base] ActiveRecord::Base.logger = @previous_state[:ar_base] if ActiveRecord::Base.respond_to?(:active_connections) #set the logger for connections that are already loaded ActiveRecord::Base.active_connections.each_value { |con| con.set_logger(ActiveRecord::Base.logger)} end end if @previous_state[:rails_default_logger] Object.redefine_const( :RAILS_DEFAULT_LOGGER, @previous_state[:rails_default_logger] ) end end end |
#set_previous_state(previous_state) ⇒ Object
9 10 11 |
# File 'lib/background_queue/worker/logger.rb', line 9 def set_previous_state(previous_state) @previous_state = previous_state end |