Module: HammerCLI::Logger

Defined in:
lib/hammer_cli/logger.rb,
lib/hammer_cli/logger_watch.rb

Defined Under Namespace

Modules: Watch

Constant Summary collapse

COLOR_LAYOUT =
Logging::Layouts::Pattern.new(:pattern => pattern, :color_scheme => 'bright')
NOCOLOR_LAYOUT =
Logging::Layouts::Pattern.new(:pattern => pattern, :color_scheme => nil)
DEFAULT_LOG_DIR =
'/var/log/hammer'

Class Method Summary collapse

Class Method Details

.initialize_logger(logger) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/hammer_cli/logger.rb', line 53

def self.initialize_logger(logger)
  log_dir = File.expand_path(HammerCLI::Settings.get(:log_dir) || DEFAULT_LOG_DIR)
  begin
    FileUtils.mkdir_p(log_dir, :mode => 0750)
  rescue Errno::EACCES
    $stderr.puts _("No permissions to create log dir %s.") % log_dir
  end

  filename = "#{log_dir}/hammer.log"
  begin
    logger.appenders = ::Logging.appenders.rolling_file('configure',
                                                        :filename => filename,
                                                        :layout   => NOCOLOR_LAYOUT,
                                                        :truncate => false,
                                                        :keep     => 5,
                                                        :size     => (HammerCLI::Settings.get(:log_size) || 1)*1024*1024) # 1MB
    # set owner and group (it's ignored if attribute is nil)
    FileUtils.chown HammerCLI::Settings.get(:log_owner), HammerCLI::Settings.get(:log_group), filename
  rescue ArgumentError
    $stderr.puts _("File %s not writeable, won't log anything to the file!") % filename
  end

  logger.level = HammerCLI::Settings.get(:log_level)
  logger
end