Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AROR-1545 make the log level used by the manager's log method configu… #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/clockwork/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Clockwork
class Manager
class NoHandlerDefined < RuntimeError; end

LOG_LEVELS = %i[debug info warn error fatal unknown]

attr_reader :config

def initialize
Expand All @@ -26,7 +28,7 @@ def configure
end

def default_configuration
{ :sleep_timeout => 1, :logger => Logger.new(STDOUT), :thread => false, :max_threads => 10 }
{ :sleep_timeout => 1, :logger => Logger.new(STDOUT), :log_level => :info, :thread => false, :max_threads => 10 }
end

def handler(&block)
Expand Down Expand Up @@ -155,7 +157,7 @@ def handle_error(e)
end

def log(msg)
config[:logger].info(msg)
config[:logger].send(log_level, msg)
end

private
Expand All @@ -176,5 +178,10 @@ def every_with_multiple_times(period, job, options={}, &block)
register(period, job, block, each_options)
end
end

def log_level
return config[:log_level] if LOG_LEVELS.include?(config[:log_level])
:info
end
end
end
43 changes: 43 additions & 0 deletions test/manager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def assert_wont_run(t)
@manager.configure do |config|
config[:sleep_timeout] = 200
config[:logger] = "A Logger"
config[:log_level] = :debug
config[:max_threads] = 10
config[:thread] = true
end
Expand All @@ -144,13 +145,15 @@ def assert_wont_run(t)
assert_equal "A Logger", @manager.config[:logger]
assert_equal 10, @manager.config[:max_threads]
assert_equal true, @manager.config[:thread]
assert_equal :debug, @manager.config[:log_level]
end

it "configuration should have reasonable defaults" do
assert_equal 1, @manager.config[:sleep_timeout]
assert @manager.config[:logger].is_a?(Logger)
assert_equal 10, @manager.config[:max_threads]
assert_equal false, @manager.config[:thread]
assert_equal :info, @manager.config[:log_level]
end

it "should accept unnamed job" do
Expand Down Expand Up @@ -401,4 +404,44 @@ def assert_wont_run(t)
end
end
end

describe 'log' do
before do
@lmanager = Clockwork::Manager.new
@lmanager.handler { }
end

it 'uses info as default level' do
mocked_logger = MiniTest::Mock.new
mocked_logger.expect :info, true, ['something fancy']
@lmanager.configure { |c| c[:logger] = mocked_logger }
@lmanager.log('something fancy')
@lmanager.tick(Time.now)
mocked_logger.verify
end

it 'uses the configured log level' do
mocked_logger = MiniTest::Mock.new
mocked_logger.expect :debug, true, ['something really fancy']
@lmanager.configure do |c|
c[:logger] = mocked_logger
c[:log_level] = :debug
end
@lmanager.log('something really fancy')
@lmanager.tick(Time.now)
mocked_logger.verify
end

it 'falls back to info if an invalid level is given' do
mocked_logger = MiniTest::Mock.new
mocked_logger.expect :info, true, ['something really fancy']
@lmanager.configure do |c|
c[:logger] = mocked_logger
c[:log_level] = :bad_and_dangerous_method
end
@lmanager.log('something really fancy')
@lmanager.tick(Time.now)
mocked_logger.verify
end
end
end