|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +module ForestAdminAgent |
| 4 | + module Services |
| 5 | + describe LoggerService do |
| 6 | + describe 'default logger level' do |
| 7 | + it 'defaults to Info' do |
| 8 | + service = described_class.new |
| 9 | + |
| 10 | + expect(service.default_logger.level).to eq(Logger::INFO) |
| 11 | + end |
| 12 | + |
| 13 | + it 'applies the configured level to the default logger' do |
| 14 | + service = described_class.new('Warn') |
| 15 | + |
| 16 | + expect(service.default_logger.level).to eq(Logger::WARN) |
| 17 | + end |
| 18 | + |
| 19 | + it 'is case-insensitive, so the config-style lowercase levels work too' do |
| 20 | + service = described_class.new('debug') |
| 21 | + |
| 22 | + expect(service.default_logger.level).to eq(Logger::DEBUG) |
| 23 | + end |
| 24 | + |
| 25 | + it 'falls back to Info when given an unknown level' do |
| 26 | + service = described_class.new('nonsense') |
| 27 | + |
| 28 | + expect(service.default_logger.level).to eq(Logger::INFO) |
| 29 | + end |
| 30 | + |
| 31 | + it 'supports Fatal, the quietest level' do |
| 32 | + service = described_class.new('Fatal') |
| 33 | + |
| 34 | + expect(service.default_logger.level).to eq(Logger::FATAL) |
| 35 | + end |
| 36 | + |
| 37 | + it 'supports Unknown' do |
| 38 | + service = described_class.new('Unknown') |
| 39 | + |
| 40 | + expect(service.default_logger.level).to eq(Logger::UNKNOWN) |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + describe '#log' do |
| 45 | + it 'filters out messages below the configured level on the default logger' do |
| 46 | + # MonoLogger captures the $stdout object at construction time, so the service |
| 47 | + # has to be built inside the block for RSpec's stdout swap to reach its writes. |
| 48 | + expect do |
| 49 | + described_class.new('Warn').log('Info', 'hidden') |
| 50 | + end.not_to output(/hidden/).to_stdout |
| 51 | + end |
| 52 | + |
| 53 | + it 'still emits messages at or above the configured level' do |
| 54 | + expect do |
| 55 | + described_class.new('Warn').log('Warn', 'shown') |
| 56 | + end.to output(/shown/).to_stdout |
| 57 | + end |
| 58 | + |
| 59 | + it 'delegates to a custom logger regardless of logger_level' do |
| 60 | + custom_logger = 'proc { |severity, message| $stdout.puts "custom:#{severity}:#{message}" }' # rubocop:disable Lint/InterpolationCheck |
| 61 | + service = described_class.new('Error', custom_logger) |
| 62 | + |
| 63 | + expect { service.log('Info', 'hello') }.to output(/custom:1:hello/).to_stdout |
| 64 | + end |
| 65 | + end |
| 66 | + end |
| 67 | + end |
| 68 | +end |
0 commit comments