Skip to content
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
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@ Add this line to your application's Gemfile:
gem 'turtle', github: 'petlove/turtle'
```

## Logging

By default, turtle uses `Rails.logger` when running inside a Rails app, or `Logger.new($stdout)` otherwise.

To silence output in tests:
```ruby
# spec/spec_helper.rb
RSpec.configure do |config|
config.before(:each) { Turtle.logger = Logger.new(IO::NULL) }
config.after(:each) { Turtle.logger = nil }
end
```

To use a custom logger:
```ruby
Turtle.logger = Logger.new('log/turtle.log')
```

To restore the default:
```ruby
Turtle.logger = nil
```

## Usage

### Queues with priority for shoryuken
Expand Down
22 changes: 7 additions & 15 deletions lib/aws/sns/configurator/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,25 @@ module AWS
module SNS
module Configurator
module Logger
LOGGER_ENABLED_ENV = ENV.fetch('AWS_SNS_CONFIGURATOR_LOGGER', 'true')
LOGGER_ENABLED_ENV = 'AWS_SNS_CONFIGURATOR_LOGGER'

class << self
def info(message)
puts log_info(message) if log?
end
return ::Turtle.logger.info(message) if ::Turtle.logger_set?

def error(message)
puts log_error(message) if log?
puts message if log?
end

def log_info(message)
log('INFO', message)
end
def error(message)
return ::Turtle.logger.error(message) if ::Turtle.logger_set?

def log_error(message)
log('ERROR', message)
puts message if log?
end

private

def log?
LOGGER_ENABLED_ENV != 'false'
end

def log(severity_level, message)
"[#{Time.now.iso8601}] [AWS::SNS::Configurator] #{severity_level} -- : #{message}"
ENV[LOGGER_ENABLED_ENV] != 'false'
end
end
end
Expand Down
18 changes: 5 additions & 13 deletions lib/aws/sqs/configurator/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,22 @@ module Logger

class << self
def info(message)
puts log_info(message) if log?
end
return ::Turtle.logger.info(message) if ::Turtle.logger_set?

def error(message)
puts log_error(message) if log?
puts message if log?
end

def log_info(message)
log('INFO', message)
end
def error(message)
return ::Turtle.logger.error(message) if ::Turtle.logger_set?

def log_error(message)
log('ERROR', message)
puts message if log?
end

private

def log?
ENV[LOGGER_ENABLED_ENV] != 'false'
end

def log(severity_level, message)
"[#{Time.now.iso8601}] [AWS::SQS::Configurator] #{severity_level} -- : #{message}"
end
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions lib/turtle.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'logger'
require 'turtle/version'
require 'turtle/group'
require 'turtle/queue'
Expand All @@ -14,6 +15,16 @@

module Turtle
class << self
attr_writer :logger

def logger
@logger || build_default_logger
end

def logger_set?
!@logger.nil?
end

def shoryuken_queues_priorities(options = nil)
queues_in_groups = Group.to_h.values.flat_map { |attrs| attrs[:queues].map { |name, _| name } }
Queue.shoryuken_priorities(options).reject { |name, _| queues_in_groups.include?(name) }
Expand Down Expand Up @@ -48,6 +59,12 @@ def retry_intervals

private

def build_default_logger
return ::Rails.logger if defined?(::Rails) && ::Rails.logger

::Logger.new($stdout)
end

def name_for_model(type, options)
case type
when :queue
Expand Down
30 changes: 5 additions & 25 deletions lib/turtle/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,12 @@

module Turtle
module Logger
class << self
def info(message)
puts log_info(message) if log?
end

def error(message)
puts log_error(message) if log?
end

def log_info(message)
log('INFO', message)
end

def log_error(message)
log('ERROR', message)
end

private

def log?
true
end
def self.info(message)
Turtle.logger.info(message)
end

def log(severity_level, message)
"[#{Time.now.iso8601}] [Turtle] #{severity_level} -- : #{message}"
end
def self.error(message)
Turtle.logger.error(message)
end
end
end
118 changes: 46 additions & 72 deletions spec/aws/sns/configurator/logger_spec.rb
Original file line number Diff line number Diff line change
@@ -1,96 +1,70 @@
# frozen_string_literal: true

RSpec.describe AWS::SNS::Configurator::Logger, type: :module do
describe '#log_info' do
subject { described_class.log_info('The topic was created successfully') }

it 'should be an info' do
is_expected.to match(/INFO/)
end

it 'should have the project name' do
is_expected.to match(/[AWS::SNS::Configurator]/)
end

it 'should have the message' do
is_expected.to match(/The topic was created successfully/)
end
end

describe '#log_error' do
subject { described_class.log_error('The topic had an error') }

it 'should be an error' do
is_expected.to match(/ERROR/)
end

it 'should have the project name' do
is_expected.to match(/[AWS::SNS:Configurator]/)
end

it 'should have the message' do
is_expected.to match(/The topic had an error/)
end
end

describe '#info' do
subject { described_class.info('The topic was created successfully') }

after { subject }

context 'when log is disabled' do
before do
stub_const('AWS::SNS::Configurator::Logger::LOGGER_ENABLED_ENV', 'false')
describe '.info' do
context 'when Turtle.logger is set' do
it 'delegates to Turtle.logger' do
expect(Turtle.logger).to receive(:info).with('the message')
described_class.info('the message')
end

it 'should not call log_info' do
expect(described_class).to_not receive(:log_info)
it 'ignores the env var' do
allow(ENV).to receive(:[]).with(described_class::LOGGER_ENABLED_ENV).and_return('false')
expect(Turtle.logger).to receive(:info).with('the message')
described_class.info('the message')
end
end

context 'when log is enabled' do
before do
stub_const('AWS::SNS::Configurator::Logger::LOGGER_ENABLED_ENV', 'true')
end
context 'when Turtle.logger is not set' do
before { Turtle.logger = nil }

it 'should call log_info' do
expect(described_class).to receive(:log_info)
.with('The topic was created successfully').once
end
context 'when log is disabled' do
before { allow(ENV).to receive(:[]).with(described_class::LOGGER_ENABLED_ENV).and_return('false') }

it 'should print with puts' do
expect(described_class).to receive(:puts).once
it 'does not print' do
expect(described_class).not_to receive(:puts)
described_class.info('the message')
end
end
end
end

describe '#error' do
subject { described_class.error('The topic had an error') }

after { subject }
context 'when log is enabled' do
before { allow(ENV).to receive(:[]).with(described_class::LOGGER_ENABLED_ENV).and_return(nil) }

context 'when log is disabled' do
before do
stub_const('AWS::SNS::Configurator::Logger::LOGGER_ENABLED_ENV', 'false')
it 'prints to stdout' do
expect(described_class).to receive(:puts).with('the message').once
described_class.info('the message')
end
end
end
end

it 'should not call log_info' do
expect(described_class).to_not receive(:log_error)
describe '.error' do
context 'when Turtle.logger is set' do
it 'delegates to Turtle.logger' do
expect(Turtle.logger).to receive(:error).with('the message')
described_class.error('the message')
end
end

context 'when log is enabled' do
before do
stub_const('AWS::SNS::Configurator::Logger::LOGGER_ENABLED_ENV', 'true')
end
context 'when Turtle.logger is not set' do
before { Turtle.logger = nil }

context 'when log is disabled' do
before { allow(ENV).to receive(:[]).with(described_class::LOGGER_ENABLED_ENV).and_return('false') }

it 'should call log_info' do
expect(described_class).to receive(:log_error)
.with('The topic had an error').once
it 'does not print' do
expect(described_class).not_to receive(:puts)
described_class.error('the message')
end
end

it 'should print with puts' do
expect(described_class).to receive(:puts).once
context 'when log is enabled' do
before { allow(ENV).to receive(:[]).with(described_class::LOGGER_ENABLED_ENV).and_return(nil) }

it 'prints to stdout' do
expect(described_class).to receive(:puts).with('the message').once
described_class.error('the message')
end
end
end
end
Expand Down
Loading
Loading