Class: Sinatra::Log

Inherits:
Object
  • Object
show all
Includes:
Log4r
Defined in:
lib/sinatra/log/log.rb,
lib/sinatra/log/version.rb,
lib/sinatra/log/default_formatter.rb

Overview

Logs to specified filename with the format:

[Log Level]: [Timestamp (ISO-8601)]: [File:linenum]: [Log Message]

Defined Under Namespace

Classes: DefaultFormatter

Constant Summary collapse

REQUIRED_CONFIG_SYMBOLS =
[:logger_name, :loglevel, :log_filename,
:enabled].freeze
VERSION =
"0.1.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Log

Returns a new instance of Log.

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sinatra/log/log.rb', line 18

def initialize(config={})
  errors = []
  REQUIRED_CONFIG_SYMBOLS.each do |key|
    if !config.include?(key)
      errors << "#{key} required, but not specified in config hash"
    end
  end
  raise ArgumentError, "#{errors}" if errors.count > 0

  logger_name = config[:logger_name].to_s.gsub(/\s+/, '_')
  @logger = Log4r::Logger.new(logger_name)

  if config[:enabled]
    index = Log4r::LNAMES.index(config[:loglevel])
    # if logger.level is not in LNAMES an exception will be thrown
    @logger.level = index unless index.nil?
  else
    @logger.level = Log4r::OFF
  end

  @outputter = FileOutputter.new("#{logger_name}fileoutput",
                                 :filename => config[:log_filename],
                                 :trunc => false)
  @logger.trace = true
  @outputter.formatter = DefaultFormatter.new(config[:project_dir])
  @logger.outputters = @outputter
end

Instance Attribute Details

#outputterObject (readonly)

Returns the value of attribute outputter.



13
14
15
# File 'lib/sinatra/log/log.rb', line 13

def outputter
  @outputter
end