Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'logger-config' of https://github.com/davydovanton/lotus …
…into davydovanton-logger-config
- Loading branch information
Showing
8 changed files
with
183 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
require 'hanami/logger' | ||
|
||
module Hanami | ||
module Config | ||
# Logger configuration | ||
# | ||
# @since x.x.x | ||
class Logger | ||
attr_reader :application_module | ||
|
||
def initialize | ||
@device = STDOUT | ||
end | ||
|
||
# Log device option value. STDOUT by default | ||
# | ||
# @overload stream(value) | ||
# Sets the given value | ||
# @param value [String] for log device option. | ||
# | ||
# @overload stream | ||
# Gets the value | ||
# @return [String] log device option's value | ||
# | ||
# @since x.x.x | ||
# @api private | ||
def stream(value = nil) | ||
if value.nil? | ||
@device | ||
else | ||
@device = value | ||
end | ||
end | ||
|
||
# Custom log instance value. `nil` by default | ||
# | ||
# @overload stream(value) | ||
# Sets the given value | ||
# @param value [String] for custom log instance value. | ||
# | ||
# @overload stream | ||
# Gets the value | ||
# @return [String] custom log instance value | ||
# | ||
# @since x.x.x | ||
# @api private | ||
def custom_logger(value = nil) | ||
if value.nil? | ||
@custom_logger | ||
else | ||
@custom_logger = value | ||
end | ||
end | ||
|
||
# Application name value. | ||
# | ||
# @overload stream(value) | ||
# Sets the given value | ||
# @param value [String] for app name option | ||
# | ||
# @overload stream | ||
# Gets the value | ||
# @return [String] app name option's value | ||
# | ||
# @since x.x.x | ||
# @api private | ||
def app_name(value = nil) | ||
if value.nil? | ||
@app_name | ||
else | ||
@app_name = value | ||
end | ||
end | ||
|
||
# Returns new Hanami::Logger instance with all options | ||
# | ||
# @return [Hanami::Logger] the new logger instance | ||
# | ||
# @since x.x.x | ||
# @api private | ||
def build | ||
@custom_logger || | ||
::Hanami::Logger.new(@app_name, device: @device) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require 'test_helper' | ||
|
||
describe Hanami::Config::Logger do | ||
let(:logger) { Hanami::Config::Logger.new } | ||
let(:path) { 'path/to/log/file' } | ||
|
||
describe '#stream' do | ||
it 'contains steam object' do | ||
logger.stream(path) | ||
logger.stream.must_equal path | ||
end | ||
|
||
it 'contains STDOUT value by default' do | ||
logger.stream.must_equal STDOUT | ||
end | ||
end | ||
|
||
describe '#name' do | ||
it 'contains name' do | ||
logger.app_name('test_app') | ||
logger.app_name.must_equal 'test_app' | ||
end | ||
end | ||
|
||
describe '#custom logger' do | ||
let(:custom_logger) { ::Logger.new(STDOUT) } | ||
|
||
it 'contains name' do | ||
logger.custom_logger(custom_logger) | ||
logger.custom_logger.must_equal custom_logger | ||
end | ||
|
||
it 'contains nil by default' do | ||
logger.custom_logger.must_equal nil | ||
end | ||
end | ||
|
||
describe '#build' do | ||
it 'returns new Utils::Logger instance' do | ||
logger.build.must_be_instance_of Hanami::Logger | ||
end | ||
|
||
describe 'when stream value is set' do | ||
let(:io) { StringIO.new } | ||
|
||
it 'returns new Utils::Logger instance with changed log_device' do | ||
logger.stream(io) | ||
builded_logger = logger.build | ||
|
||
builded_logger.must_be_instance_of Hanami::Logger | ||
builded_logger.instance_variable_get("@device").must_equal io | ||
end | ||
end | ||
|
||
describe 'when user set custom logger instance' do | ||
it 'returns new Utils::Logger instance with changed log_device' do | ||
logger.custom_logger(::Logger.new(STDOUT)) | ||
builded_logger = logger.build | ||
|
||
builded_logger.must_be_instance_of ::Logger | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters