apache-config-generator/lib/apache/logging.rb

69 lines
2.1 KiB
Ruby
Raw Normal View History

2010-05-05 14:44:20 +00:00
module Apache
2010-05-10 19:57:34 +00:00
# Methods to handle logging configuration are defined here.
#
# For each of the four main log types (Custom, Error, Script, and Rewrite), the following two methods are created:
#
# * (type)_log: A non-rotated log file
# * rotate_(type)_log: A rotated log file
#
# Non-rotated logs work as such:
# custom_log "/path/to/log/file.log", :common #=> CustomLog "/path/to/log/file.log" common
#
# Rotated logs work as such:
# rotate_custom_log "/path/to/log/file-%Y%m%d.log", 86400, :common
# #=> CustomLog "|/path/to/rotatelogs /path/to/log/file-%Y%m%d.jpg 86400" common
#
# Both variations check to make sure the log file diretory exists during generation.
# The rotate_ variations need @rotate_logs_path set to work.
2010-05-05 14:44:20 +00:00
module Logging
class << self
def log_paths
@log_paths ||= {}
end
def reset!
@log_paths = {}
end
end
2010-05-07 02:26:12 +00:00
[ :custom, :error, :script, :rewrite ].each do |type|
class_eval <<-EOT
def #{type}_log(*opts)
2010-05-18 21:43:23 +00:00
handle_log :tag => '#{type.to_s.capitalize}Log',
:path => opts.first,
:additional_options => opts[1..-1],
:type => :#{type}
2010-05-07 02:26:12 +00:00
end
2010-05-05 16:25:07 +00:00
2010-05-07 02:26:12 +00:00
def rotate_#{type}_log(*opts)
2010-05-18 21:43:23 +00:00
handle_log :tag => '#{type.to_s.capitalize}Log',
:path => opts.first,
:real_path => rotatelogs(*opts[0..1]),
:additional_options => opts[2..-1],
:type => :#{type}
2010-05-07 02:26:12 +00:00
end
EOT
2010-05-05 14:44:20 +00:00
end
def combined_log_format(name = 'combined')
log_format '%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"', name.to_sym
end
def common_log_format(name = 'common')
log_format '%h %l %u %t \"%r\" %>s %b', name.to_sym
end
2010-05-05 16:25:07 +00:00
private
2010-05-18 21:43:23 +00:00
def handle_log(info)
writable? (path = info[:path])
real_path = (info[:real_path] || path).quoteize
(Apache::Logging.log_paths[info[:type]] ||= []) << path
2010-05-18 21:43:23 +00:00
self << "#{info[:tag]} #{[real_path, info[:additional_options]].flatten * " "}"
2010-05-05 16:25:07 +00:00
end
2010-05-05 14:44:20 +00:00
end
end