Skip to content

Commit

Permalink
Merge branch 'log-file'
Browse files Browse the repository at this point in the history
  • Loading branch information
nebhale committed Jan 11, 2016
2 parents 834926b + e2e0c53 commit d6c19f8
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
3 changes: 2 additions & 1 deletion config/logging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@

# Logging configuration
---
default_log_level: INFO
default_log_level: INFO
enable_log_file: false
18 changes: 11 additions & 7 deletions lib/java_buildpack/logging/logger_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@ def initialize
# @return [Void]
def setup(app_dir)
@monitor.synchronize do
@log_file = app_dir + '.java-buildpack.log'
@delegates = [file_logger, console_logger]
configuration = JavaBuildpack::Util::ConfigurationUtils.load('logging', true, false)

@log_file = app_dir + '.java-buildpack.log'

@delegates = [console_logger(configuration)]
@delegates << file_logger if configuration['enable_log_file']

@initialized = true
end
end
Expand Down Expand Up @@ -100,9 +105,9 @@ def get_logger(klass)

private

def console_logger
def console_logger(configuration)
logger = Logger.new($stderr)
logger.level = severity
logger.level = severity(configuration)
logger.formatter = lambda do |severity, _datetime, klass, message|
"#{klass.ljust(32)} #{severity.ljust(5)} #{message}\n"
end
Expand All @@ -126,11 +131,10 @@ def ruby_mode
$VERBOSE || $DEBUG ? 'DEBUG' : nil
end

def severity
def severity(configuration)
severity = ENV['JBP_LOG_LEVEL']
severity = ruby_mode unless severity
severity =
JavaBuildpack::Util::ConfigurationUtils.load('logging', true, false)['default_log_level'] unless severity
severity = configuration['default_log_level'] unless severity
severity = 'INFO' unless severity

"::Logger::Severity::#{severity.upcase}".constantize
Expand Down
2 changes: 1 addition & 1 deletion spec/java_buildpack/logging/logger_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
end

it 'logs all levels to file',
log_level: 'FATAL' do
:enable_log_file, log_level: 'FATAL' do

trigger

Expand Down
8 changes: 6 additions & 2 deletions spec/java_buildpack/util/cache/internet_availability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,18 @@
end
end

it 'records availability' do
it 'records availability',
:enable_log_file do

described_class.instance.available false

expect(described_class.instance.available?).not_to be
expect(log_contents).not_to match(/Internet availability set to false/)
end

it 'records availability with message' do
it 'records availability with message',
:enable_log_file do

described_class.instance.available false, 'test message'

expect(described_class.instance.available?).not_to be
Expand Down
12 changes: 9 additions & 3 deletions spec/logging_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
require 'console_helper'
require 'fileutils'
require 'java_buildpack/logging/logger_factory'
require 'yaml'

shared_context 'logging_helper' do
include_context 'console_helper'
include_context 'application_helper'

previous_log_config = ENV['JBP_CONFIG_LOGGING']
previous_log_level = ENV['JBP_LOG_LEVEL']
previous_debug_level = $DEBUG
previous_verbose_level = $VERBOSE
Expand All @@ -34,6 +36,9 @@
log_level = example.metadata[:log_level]
ENV['JBP_LOG_LEVEL'] = log_level if log_level

enable_log_file = example.metadata[:enable_log_file]
ENV['JBP_CONFIG_LOGGING'] = { 'enable_log_file' => true }.to_yaml if enable_log_file

$DEBUG = example.metadata[:debug]
$VERBOSE = example.metadata[:verbose]

Expand All @@ -43,9 +48,10 @@
after do
JavaBuildpack::Logging::LoggerFactory.instance.reset

ENV['JBP_LOG_LEVEL'] = previous_log_level
$VERBOSE = previous_verbose_level
$DEBUG = previous_debug_level
ENV['JBP_CONFIG_LOGGING'] = previous_log_config
ENV['JBP_LOG_LEVEL'] = previous_log_level
$VERBOSE = previous_verbose_level
$DEBUG = previous_debug_level
end

end

0 comments on commit d6c19f8

Please sign in to comment.