Skip to content

Commit

Permalink
first draft for web-app logging configuration (see #82)
Browse files Browse the repository at this point in the history
- mainly to not require hacks for disabling rotation ...
  • Loading branch information
kares committed Oct 17, 2012
1 parent 63dc9fe commit c4910b6
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 13 deletions.
1 change: 1 addition & 0 deletions lib/trinidad/configuration.rb
Expand Up @@ -110,6 +110,7 @@ def self.symbolize_options(options, deep = true)

# a Hash like deep_merge helper
def self.merge_options(target, current, deep = true)
return target unless current
target_dup = target.dup
current.keys.each do |key|
target_dup[key] =
Expand Down
30 changes: 19 additions & 11 deletions lib/trinidad/logging.rb
Expand Up @@ -69,16 +69,24 @@ def self.configure_web_app(web_app, context)
logger = JUL::Logger.getLogger(logger_name) # exclusive for web app
# avoid duplicate calls - do not configure our FileHandler twice :
return false if logger.handlers.find { |h| h.is_a?(FileHandler) }
level = parse_log_level(web_app.log, nil)
logger.level = level # inherits level from parent if nil
logging = web_app.logging
logger.level = parse_log_level(logging[:level], nil)
# delegate to root (console) output only in development mode :
logger.use_parent_handlers = ( web_app.environment == 'development' )

prefix, suffix = web_app.environment, '.log' # {prefix}{date}{suffix}
file_handler = FileHandler.new(web_app.log_dir, prefix, suffix)
file_handler.rotatable = true # {prefix}{date}{suffix}
file_handler.formatter = web_app_formatter
logger.add_handler(file_handler)
logger.use_parent_handlers = logging[:use_parent_handlers]
# logging:
# file:
# dir: log # [RAILS_ROOT]/log
# prefix: production
# suffix: .log
if file = logging[:file]
prefix, suffix = file[:prefix], file[:suffix] # {prefix}{date}{suffix}
file_handler = FileHandler.new(file[:dir] || file[:directory], prefix, suffix)
file_handler.rotatable = file.key?(:rotatable) ? file[:rotatable] : file[:rotate]
file_handler.buffer_size = file[:buffer_size] if file[:buffer_size]
format = file.key?(:format) ? file[:format] : logging[:format]
file_handler.formatter = web_app_formatter(format) # nil uses default
logger.add_handler(file_handler)
end
logger
end

Expand All @@ -88,9 +96,9 @@ def self.console_formatter
MessageFormatter.new
end

def self.web_app_formatter
def self.web_app_formatter(format = nil)
# format used by Rails "2012-06-13 16:42:21 +0200"
DefaultFormatter.new("yyyy-MM-dd HH:mm:ss Z")
DefaultFormatter.new(format.nil? ? 'yyyy-MM-dd HH:mm:ss Z' : format)
end

private
Expand Down
16 changes: 16 additions & 0 deletions lib/trinidad/web_app.rb
Expand Up @@ -141,6 +141,22 @@ def add_context_param(param_name, param_value)
end
end

def logging
@logging ||= begin
defaults = {
:level => log, # backwards compatibility
:use_parent_handlers => environment == 'development',
:file => {
:dir => log_dir,
:prefix => environment,
:suffix => '.log',
:rotate => true
}
}
Trinidad::Configuration.merge_options(defaults, self[:logging])
end
end

def deployment_descriptor
return nil if @deployment_descriptor == false
@deployment_descriptor ||= expand_path(web_xml) || false
Expand Down
41 changes: 39 additions & 2 deletions spec/trinidad/logging_spec.rb
Expand Up @@ -67,6 +67,35 @@
tomcat.stop if tomcat.server.state_name =~ /START/i
end

it "configures from provided logging configuration" do
make_file log_file = "#{MOCK_WEB_APP_DIR}/log/production.txt", "previous-content\n"
yesterday = Time.now - 60 * 60 * 24
java.io.File.new(log_file).setLastModified(yesterday.to_f * 1000)

web_app = create_mock_web_app :context_path => '/app0',
:logging => {
:level => 'debug', :format => 'dd.MM -',
:file => {
:rotate => false,
:suffix => '.txt',
:directory => 'log',
:buffer_size => 4096
}
}
context = create_mock_web_app_context web_app

logger = Trinidad::Logging.configure_web_app(web_app, context)
logger.fine "hello-there"

File.exist?(log_file).should be true; now = Time.now

File.read(log_file).should == "previous-content\n"

logger.handlers.first.flush
File.read(log_file).should == "previous-content\n" +
"#{now.day}.#{now.month} - FINE: hello-there\n"
end

it "creates the log file according with the environment if it doesn't exist" do
web_app = create_mock_web_app :context_path => '/app1'
context = create_mock_web_app_context web_app
Expand Down Expand Up @@ -118,7 +147,7 @@
end

it "rotates and merges an old log file" do
File.open("#{MOCK_WEB_APP_DIR}/log/staging.log", 'w') { |f| f << "old entry\n" }
make_file "#{MOCK_WEB_APP_DIR}/log/staging.log", "old entry\n"
yesterday = Time.now - 60 * 60 * 24; yesterday_ms = yesterday.to_f * 1000
y_date = yesterday.strftime '%Y-%m-%d'
java.io.File.new("#{MOCK_WEB_APP_DIR}/log/staging.log").setLastModified(yesterday_ms)
Expand All @@ -136,7 +165,7 @@
end

it "rotates when logging date changes" do
File.open("#{MOCK_WEB_APP_DIR}/log/production.log", 'w') { |f| f << "old entry\n" }
make_file "#{MOCK_WEB_APP_DIR}/log/production.log", "old entry\n"
yesterday = Time.now - 60 * 60 * 24; yesterday_ms = yesterday.to_f * 1000
java.io.File.new("#{MOCK_WEB_APP_DIR}/log/production.log").setLastModified(yesterday_ms)

Expand Down Expand Up @@ -346,6 +375,14 @@ def create_web_app_context(context_dir, web_app)
context
end

def make_file(path, content = nil)
if dir = File.dirname(path)
FileUtils.mkdir(dir) unless File.exist?(dir)
end
FileUtils.touch path
File.open(path, 'w') { |f| f << content } if content
end

end

describe 'FileHandler' do
Expand Down
1 change: 1 addition & 0 deletions spec/web_app_rails/.gitignore
@@ -1,4 +1,5 @@
log/*.log
log/*.txt
tmp
work

Empty file added spec/web_app_rails/log/.gitkeep
Empty file.

0 comments on commit c4910b6

Please sign in to comment.