Class: Eco::API::Common::Session::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/eco/api/common/session/logger.rb,
lib/eco/api/common/session/logger/log.rb,
lib/eco/api/common/session/logger/cache.rb

Defined Under Namespace

Classes: Cache, Log

Constant Summary collapse

DEFAULT_TIMESTAMP_PATTERN =
'%Y-%m-%dT%H:%M:%S'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(console_level: nil, file_level: ::Logger::DEBUG, log_file: nil, timestamp_console: false, enviro: nil) ⇒ Logger

Returns a new instance of Logger.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/eco/api/common/session/logger.rb', line 10

def initialize(console_level: nil, file_level: ::Logger::DEBUG, log_file: nil, timestamp_console: false, enviro: nil)
  raise "Required Environment object (enviro:). Given: #{enviro}" if enviro && !enviro.is_a?(Eco::API::Common::Session::Environment)
  @enviro = enviro
  @cache  = Logger::Cache.new

  timestamp_console = fetch_timestamp_console(timestamp_console)
  @console_logger = ::Logger.new(STDOUT).tap do |_logger|
    _logger.formatter = proc do |severity, datetime, progname, msg|
      str_timestamp = timestamp_console ? "#{datetime.strftime(DEFAULT_TIMESTAMP_PATTERN)} >" : ""
      "#{severity.to_s[0]}: #{str_timestamp} #{msg}\n"
    end
    _logger.level = fetch_console_level(console_level)
  end

  if log_file = fetch_log_file(log_file)
    @file_logger = ::Logger.new(log_file).tap do |_logger|
      _logger.formatter = proc do |severity, datetime, progname, msg|
        "#{severity.to_s[0]}: #{datetime.strftime(DEFAULT_TIMESTAMP_PATTERN)} > #{msg}\n".tap do |formatted|
          cache.add(severity, datetime, msg, formatted)
        end
      end
      _logger.level = fetch_file_level(file_level)
    end
  end
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



8
9
10
# File 'lib/eco/api/common/session/logger.rb', line 8

def cache
  @cache
end

Instance Method Details

#<<(msg) ⇒ Object



64
65
66
67
# File 'lib/eco/api/common/session/logger.rb', line 64

def << (msg)
  @console_logger << msg unless !@console_logger
  @file_logger << msg    unless !@file_logger
end

#add(*args) ⇒ Object



74
75
76
77
# File 'lib/eco/api/common/session/logger.rb', line 74

def add(*args)
  @console_logger.add(*args) unless !@console_logger
  @file_logger.add(*args)    unless !@file_logger
end

#closeObject



69
70
71
72
# File 'lib/eco/api/common/session/logger.rb', line 69

def close()
  @console_logger.close unless !@console_logger
  @file_logger.close    unless !@file_logger
end

#debug(*args, &block) ⇒ Object



40
41
42
# File 'lib/eco/api/common/session/logger.rb', line 40

def debug(*args, &block)
  log(:debug, *args, &block)
end

#error(*args, &block) ⇒ Object



52
53
54
# File 'lib/eco/api/common/session/logger.rb', line 52

def error(*args, &block)
  return log(:error, *args, &block)
end

#fatal(*args, &block) ⇒ Object



56
57
58
# File 'lib/eco/api/common/session/logger.rb', line 56

def fatal(*args, &block)
  return log(:fatal, *args, &block)
end

#info(*args, &block) ⇒ Object



44
45
46
# File 'lib/eco/api/common/session/logger.rb', line 44

def info(*args, &block)
  log(:info, *args, &block)
end

#level=(value) ⇒ Object



36
37
38
# File 'lib/eco/api/common/session/logger.rb', line 36

def level=(value)
  @console_logger.level = value
end

#unkown(msg, &block) ⇒ Object



60
61
62
# File 'lib/eco/api/common/session/logger.rb', line 60

def unkown(msg, &block)
  return log(:unkown, *args, &block)
end

#warn(*args, &block) ⇒ Object



48
49
50
# File 'lib/eco/api/common/session/logger.rb', line 48

def warn(*args, &block)
  return log(:warn, *args, &block)
end