class Anschel::Mjolnir

Thor’s hammer! Like Thor with better logging

Constants

COMMON_OPTIONS

Common options for Thor commands

Public Class Methods

include_common_options() click to toggle source

Decorate Thor commands with the options above

# File lib/anschel/mjolnir.rb, line 33
def self.include_common_options
  COMMON_OPTIONS.each do |name, spec|
    option name, spec
  end
end

Public Instance Methods

log() click to toggle source

Construct a Logger given the command-line options

# File lib/anschel/mjolnir.rb, line 43
def log
  return @logger if defined? @logger
  device = options.log || $stdout
  colorize, prettify = false, false
  colorize, prettify = true, true if device.tty? rescue false
  @logger = Slog.new \
    out: device, colorize: colorize, prettify: prettify
  @logger.level = :debug if options.debug?
  @logger.level = :trace if options.trace?
  @logger
end
setup_log4j(config) click to toggle source

Write out a temporary properties file for Log4j

# File lib/anschel/mjolnir.rb, line 56
def setup_log4j config
  config ||= {}
  path     = config.delete(:path)    || '/dev/stdout'
  pattern  = config.delete(:pattern) || '%p [%d] (%c) %m%n'
  Tempfile.open('anschel_log4j') do |f|
    log4j = %Q|
      log4j.rootLogger=INFO, A1
      log4j.appender.A1=org.apache.log4j.RollingFileAppender
      log4j.appender.A1.File=%s
      log4j.appender.A1.layout=org.apache.log4j.PatternLayout
      log4j.appender.A1.layout.ConversionPattern=%s
    | % [ path, pattern ]
    f.write log4j.gsub(/^\s+/,'')
    f.rewind
    org.apache.log4j.PropertyConfigurator.configure(f.path)
  end
end