Class: TransmissionRSS::Log

Inherits:
Object show all
Includes:
Singleton
Defined in:
lib/transmission-rss/log.rb

Overview

Encapsulates Logger as a singleton class.

Instance Method Summary collapse

Constructor Details

#initialize(target = $stderr, level = :debug) ⇒ Log

Returns a new instance of Log.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/transmission-rss/log.rb', line 9

def initialize(target = $stderr, level = :debug)
  @target = target
  @level = level

  @logger = Logger.new(target)
  @logger.level = to_level_const(level)
  @logger.formatter = proc do |sev, time, _, msg|
    time = time.strftime('%Y-%m-%d %H:%M:%S')
    "#{time} (#{sev.downcase}) #{msg}\n"
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object

If this class misses a method, call it on the encapsulated Logger class.



37
38
39
# File 'lib/transmission-rss/log.rb', line 37

def method_missing(sym, *args)
  @logger.send(sym, *args)
end

Instance Method Details

#level=(level) ⇒ Object

Change log level (String or Symbol)



32
33
34
# File 'lib/transmission-rss/log.rb', line 32

def level=(level)
  initialize(@target, level)
end

#target=(target) ⇒ Object

Change log target (IO, path to a file as String, or Symbol for IO constant).



23
24
25
26
27
28
29
# File 'lib/transmission-rss/log.rb', line 23

def target=(target)
  if target.is_a? Symbol
    target = Object.const_get(target.to_s.upcase)
  end

  initialize(target, @level)
end