Sha256: ec8b62e13bec37555ade4401ff7f95bc301e9d9d10f7d6cd22294cc6ee9ae44b

Contents?: true

Size: 1.98 KB

Versions: 12

Compression:

Stored size: 1.98 KB

Contents

require 'fileutils'
require 'logging'

module HammerCLI
  module Logger

    Logging.color_scheme('bright',
                         :levels => {
                             :info  => :green,
                             :warn  => :yellow,
                             :error => :red,
                             :fatal => [:white, :on_red]},
                         :date   => :blue,
                         :logger => :cyan,
                         :line   => :yellow,
                         :file   => :yellow,
                         :method => :yellow)

    pattern         = "[%5l %d %c] %m\n"
    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/foreman'

    log_dir = File.expand_path(HammerCLI::Settings.get(:log_dir) || DEFAULT_LOG_DIR)
    begin
      FileUtils.mkdir_p(log_dir, :mode => 0750)
    rescue Errno::EACCES => e
      puts "No permissions to create log dir #{log_dir}"
    end

    logger   = Logging.logger.root
    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) || 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 => e
      puts "File #{filename} not writeable, won't log anything to file!"
    end

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

  end

end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
hammer_cli-0.0.18 lib/hammer_cli/logger.rb
hammer_cli-0.0.16 lib/hammer_cli/logger.rb
hammer_cli-0.0.15 lib/hammer_cli/logger.rb
hammer_cli-0.0.14 lib/hammer_cli/logger.rb
hammer_cli-0.0.13 lib/hammer_cli/logger.rb
hammer_cli-0.0.12 lib/hammer_cli/logger.rb
hammer_cli-0.0.11 lib/hammer_cli/logger.rb
hammer_cli-0.0.10 lib/hammer_cli/logger.rb
hammer_cli-0.0.9 lib/hammer_cli/logger.rb
hammer_cli-0.0.8 lib/hammer_cli/logger.rb
hammer_cli-0.0.7 lib/hammer_cli/logger.rb
hammer_cli-0.0.6 lib/hammer_cli/logger.rb