Skip to content

Commit

Permalink
Merge branch 'logger-config' of https://github.com/davydovanton/lotus
Browse files Browse the repository at this point in the history
…into davydovanton-logger-config
  • Loading branch information
jodosha committed Feb 24, 2016
2 parents f215341 + c3d8b22 commit 29a5abd
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 40 deletions.
87 changes: 87 additions & 0 deletions lib/hanami/config/logger.rb
@@ -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
27 changes: 11 additions & 16 deletions lib/hanami/configuration.rb
@@ -1,14 +1,15 @@
require 'hanami/utils/kernel'
require 'hanami/utils/deprecation'
require 'hanami/environment'
require 'hanami/config/configure'
require 'hanami/config/cookies'
require 'hanami/config/framework_configuration'
require 'hanami/config/load_paths'
require 'hanami/config/routes'
require 'hanami/config/logger'
require 'hanami/config/mapping'
require 'hanami/config/sessions'
require 'hanami/config/configure'
require 'hanami/config/routes'
require 'hanami/config/security'
require 'hanami/config/cookies'
require 'hanami/config/sessions'

module Hanami
# Configuration for a Hanami application
Expand Down Expand Up @@ -1657,13 +1658,11 @@ def view
@view ||= Config::FrameworkConfiguration.new
end

# Defines a logger instance to the configuration
#
# This logger instance will be used to set the logger available on application module
# Defines a Config::Logger instance to the logger configuration.
#
# If no logger instance is defined, a Hanami::Logger will be set by default
# This instance will be used to generate the hanami logger available on application module.
#
# @return [Logger, NilClass] logger instance
# @return [Config::Logger] Configure logger instance
#
# @since 0.5.0
#
Expand All @@ -1673,7 +1672,7 @@ def view
# module Bookshelf
# class Application < Hanami::Application
# configure do
# logger Logger.new(STDOUT)
# logger.stream 'path/to/log/file'
# end
# load!
# end
Expand All @@ -1689,12 +1688,8 @@ def view
# end
# end
#
def logger(value = nil)
if value.nil?
@logger
else
@logger = value
end
def logger
@logger ||= Config::Logger.new
end

# This options is used as a bridge between container and router application.
Expand Down
6 changes: 6 additions & 0 deletions lib/hanami/generators/app/application.rb.tt
Expand Up @@ -231,6 +231,9 @@ module <%= config[:classified_app_name] %>
configure :development do
# Don't handle exceptions, render the stack trace
handle_exceptions false

# Select path for logger
# logger.stream "#{Hanami.root}logs/development.log"
end

##
Expand All @@ -249,6 +252,9 @@ module <%= config[:classified_app_name] %>
# host 'example.org'
# port 443

# Select path for logger
# logger.stream "#{Hanami.root}logs/production.log"

assets do
# Don't compile static assets in production mode (eg. Sass, ES6)
#
Expand Down
6 changes: 2 additions & 4 deletions lib/hanami/loader.rb
Expand Up @@ -143,10 +143,8 @@ def _configure_model_framework!

def _configure_logger!
unless application_module.const_defined?('Logger', false)
if configuration.logger.nil?
configuration.logger Hanami::Logger.new(application_module.to_s)
end
application_module.const_set('Logger', configuration.logger)
configuration.logger.app_name(application_module.to_s)
application_module.const_set('Logger', configuration.logger.build)
end
end

Expand Down
64 changes: 64 additions & 0 deletions test/config/logger_test.rb
@@ -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
18 changes: 3 additions & 15 deletions test/configuration_test.rb
Expand Up @@ -794,27 +794,15 @@ class FileSystem
end

describe '#logger' do
describe "when not previously set" do
before do
@configuration = Hanami::Configuration.new
end

it 'defaults to nil' do
@configuration.logger.must_be_nil
end
end

describe "when the logger is set" do
describe "when custom logger not previously set" do
before do
@logger = Logger.new(STDOUT)
@configuration = Hanami::Configuration.new
@configuration.logger @logger
@configuration.logger
end

it 'returns logger instance' do
@configuration.logger.must_equal @logger
@configuration.logger.must_be_instance_of Hanami::Config::Logger
end
end
end

end
6 changes: 6 additions & 0 deletions test/fixtures/commands/generate/app/application.rb
Expand Up @@ -231,6 +231,9 @@ class Application < Hanami::Application
configure :development do
# Don't handle exceptions, render the stack trace
handle_exceptions false

# Select path for logger
# logger.stream "#{Hanami.root}logs/development.log"
end

##
Expand All @@ -249,6 +252,9 @@ class Application < Hanami::Application
# host 'example.org'
# port 443

# Select path for logger
# logger.stream "#{Hanami.root}logs/production.log"

assets do
# Don't compile static assets in production mode (eg. Sass, ES6)
#
Expand Down
9 changes: 4 additions & 5 deletions test/loader_test.rb
Expand Up @@ -108,29 +108,28 @@ class Application < Hanami::Application; load!; end
it 'load a default logger' do
BeerShop::Logger.must_be_instance_of Hanami::Logger
end

it 'has app module name along with log output' do
BeerShop::Logger.info 'foo'
@output.must_match(/BeerShop/)
@output.must_match(/foo/)
end
end

describe 'when explicitly configured' do
before do
class MyLogger < Logger; end
module DrinkShop
class Application < Hanami::Application
configure { logger MyLogger.new(STDOUT) }
configure { logger }
load!
end
end
end
after do
Object.__send__(:remove_const, :MyLogger)
Object.__send__(:remove_const, :DrinkShop)
end

it 'load the configured logger' do
DrinkShop::Logger.must_be_instance_of MyLogger
DrinkShop::Logger.must_be_instance_of Hanami::Logger
end
end
end
Expand Down

0 comments on commit 29a5abd

Please sign in to comment.