Skip to content
This repository has been archived by the owner on Dec 4, 2021. It is now read-only.

Commit

Permalink
support for non rails applications
Browse files Browse the repository at this point in the history
  • Loading branch information
anikolskiy authored and anikolskiy committed Jan 29, 2014
1 parent 9cccd18 commit 448afbf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,6 +2,7 @@
*.rbc
.bundle
.config
.idea
.yardoc
.project
*.log
Expand Down
48 changes: 32 additions & 16 deletions lib/mongodb_logger/logger.rb
Expand Up @@ -24,18 +24,22 @@ class Logger < RailsLogger
attr_reader :db_configuration, :mongo_adapter

def initialize(path = nil, level = DEBUG)
path ||= File.join(Rails.root, "log/#{Rails.env}.log")
@level = level
internal_initialize
rescue => e
# should use a config block for this
Rails.env.production? ? (raise e) : (puts "MongodbLogger WARNING: Using Rails Logger due to exception: #{e.message}")
ensure
if disable_file_logging?
@log = ::Logger.new(STDOUT)
@log.level = @level
else
super(path, @level)
set_root_and_env

begin
path ||= File.join(@app_root, "log/#{@app_env}.log")
@level = level
internal_initialize
rescue => e
# should use a config block for this
Rails.env.production? ? (raise e) : (puts "MongodbLogger WARNING: Using Rails Logger due to exception: #{e.message}")
ensure
if disable_file_logging?
@log = ::Logger.new(STDOUT)
@log.level = @level
else
super(path, @level)
end
end
end

Expand Down Expand Up @@ -114,7 +118,7 @@ def configure
port: 27017,
capsize: default_capsize,
ssl: false}.merge(resolve_config).with_indifferent_access
@db_configuration[:collection] ||= defined?(Rails) ? "#{Rails.env}_log" : "production_log"
@db_configuration[:collection] ||= "#{@app_env}_log"
@db_configuration[:application_name] ||= resolve_application_name
@db_configuration[:write_options] ||= { w: 0, wtimeout: 200 }

Expand All @@ -130,9 +134,9 @@ def resolve_application_name
def resolve_config
config = {}
CONFIGURATION_FILES.each do |filename|
config_file = Rails.root.join("config", filename)
if config_file.file?
config = YAML.load(ERB.new(config_file.read).result)[Rails.env.to_s]
config_file = File.join(@app_root, 'config', filename)
if File.file? config_file
config = YAML.load(ERB.new(File.new(config_file).read).result)[@app_env]
config = config['mongodb_logger'] if config && config.has_key?('mongodb_logger')
break unless config.blank?
end
Expand Down Expand Up @@ -206,5 +210,17 @@ def nice_serialize_object(data)
end
end

def set_root_and_env
if defined? Rails
@app_root = Rails.root.to_s
@app_env = Rails.env.to_s
elsif defined? RACK_ROOT
@app_root = RACK_ROOT
@app_env = ENV['RACK_ENV'] || 'production'
else
raise 'Please define RACK_ROOT in the top of your config.ru like this: RACK_ROOT = File.dirname(__FILE__)'
end
end

end
end
10 changes: 9 additions & 1 deletion lib/mongodb_logger/rack_middleware.rb
Expand Up @@ -22,7 +22,15 @@ def call(env)
ip: request_ip(request)
}

Rails.logger.mongoize(log_attrs) do
@logger ||= if defined?(Rails)
Rails.logger
elsif defined?(LOGGER)
LOGGER
else
MongodbLogger::Logger.new
end

@logger.mongoize(log_attrs) do
return @app.call(env)
end
end
Expand Down

0 comments on commit 448afbf

Please sign in to comment.