Class: WinstonMongodbRails::MongoLogger

Inherits:
ActiveSupport::BufferedLogger
  • Object
show all
Defined in:
lib/winston_mongodb_rails/mongo_logger.rb

Constant Summary collapse

CONFIGURATION_FILES =

Looks for configuration files in this order

["mongo_logger.yml", "mongoid.yml", "database.yml"]
LOG_LEVEL_SYM =
[:debug, :info, :warn, :error, :fatal, :unknown]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MongoLogger

Returns a new instance of MongoLogger.



10
11
12
13
14
15
16
17
18
19
# File 'lib/winston_mongodb_rails/mongo_logger.rb', line 10

def initialize(options={})
  path = options[:path] || File.join(Rails.root, "log/#{Rails.env}-mog.log")
  level = options[:level] || DEBUG
  @db_configuration = resolve_config
  internal_initialize
  super(path, level)
rescue => e
  # should use a config block for this
  Rails.env.production? ? (raise e) : (puts "Using BufferedLogger due to exception: " + e.message)
end

Class Method Details

.create_logger(config, path) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/winston_mongodb_rails/mongo_logger.rb', line 76

def create_logger(config, path)
  level = ActiveSupport::BufferedLogger.const_get(config.log_level.to_s.upcase)
  logger = MongoLogger.new(:path => path, :level => level)
  logger.auto_flushing = false if Rails.env.production?
  logger
rescue StandardError => e
  logger = ActiveSupport::BufferedLogger.new(STDERR)
  logger.level = ActiveSupport::BufferedLogger::WARN
  logger.warn(
  "CentralLogger Initializer Error: Unable to access log file. Please ensure that #{path} exists and is chmod 0666. " +
  "The log level has been raised to WARN and the output directed to STDERR until the problem is fixed." + "\n" +
  e.message + "\n" + e.backtrace.join("\n")
  )
  logger
end

Instance Method Details

#add(severity, message = nil, progname = nil, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/winston_mongodb_rails/mongo_logger.rb', line 21

def add(severity, message = nil, progname = nil, &block)
  
  # this writes to mongo
  mongo_record = {
    :level => LOG_LEVEL_SYM[severity].to_s,
    :timestamp => Time.now,
    :message => message,
    :application_name => @application_name,
    :meta => progname
  }

  insert_log_record(mongo_record, @safe_insert)
  super
end