Class: RailsCronLogger::Logger

Inherits:
Logger
  • Object
show all
Defined in:
lib/rails-cron-logger.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Logger

Returns a new instance of Logger.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rails-cron-logger.rb', line 6

def initialize(options={})
  if options[:sync]
    # Flushes output on every write to avoid losing valuable output if the cron job crashes, etc.
    # see http://coderrr.wordpress.com/2008/12/20/automatically-flushing-redirected-or-piped-stdout/
    # Note that this is obviously an interpreter-wide setting we're changing here.
    # Really this ought to be set at a higher level than the code calling this logger (in the crontab
    # or in the crontab script that calls your code perhaps) but adding an option here for convenience.
    $stdout.sync = true
  end

  logdev = if options[:logdev].present?
             options[:logdev]
           elsif Rails.env.test?
             # don't want log messages for tests mixed in with test output
             Rails.application.config.paths['log'].first
           else
             $stdout
           end

  super(logdev)

  @formatter = Rails.logger.formatter
  @level     = Rails.logger.level
end