Module: MailyHerald::Logging

Defined in:
lib/maily_herald/logging.rb

Defined Under Namespace

Modules: LoggerExtensions Classes: Formatter

Constant Summary collapse

OPTIONS =
{
  target: STDOUT,
  level: Logger::INFO,
  progname: "app"
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.initialize(opts = {}) ⇒ Object


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/maily_herald/logging.rb', line 47

def self.initialize(opts = {})
  oldlogger = @logger

  @options ||= OPTIONS.dup
  @options.merge!(opts) if opts

  @options[:target] = Rails.root + @options[:target] if @options[:target].is_a?(String) && Pathname.new(@options[:target]).relative? && defined?(Rails)

  @logger = Logger.new(@options[:target])
  @logger.level = @options[:level]
  @logger.formatter = Formatter.new
  @logger.progname = @options[:progname]
  @logger.extend(LoggerExtensions)

  oldlogger.close if oldlogger
  @logger
end

.initialized?Boolean


65
66
67
# File 'lib/maily_herald/logging.rb', line 65

def self.initialized?
  !!@logger
end

.logger(opts = {}) ⇒ Object


69
70
71
# File 'lib/maily_herald/logging.rb', line 69

def self.logger opts = {}
  @logger || initialize(opts)
end

.logger=(log) ⇒ Object


73
74
75
# File 'lib/maily_herald/logging.rb', line 73

def self.logger=(log)
  @logger = (log ? log : Logger.new('/dev/null'))
end

.optionsObject


77
78
79
# File 'lib/maily_herald/logging.rb', line 77

def self.options
  @options || OPTIONS.dup
end

.safe_optionsObject


81
82
83
84
85
# File 'lib/maily_herald/logging.rb', line 81

def self.safe_options
  opts = self.options.dup
  opts[:target] = nil if !opts[:target].is_a?(String)
  opts
end

Instance Method Details

#loggerObject


87
88
89
# File 'lib/maily_herald/logging.rb', line 87

def logger
  MailyHerald::Logging.logger
end