Class: ActiveCypher::Utils::Logger

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/active_cypher/utils/logger.rb

Overview

A singleton logger class for ActiveCypher

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogger

Returns a new instance of Logger.



14
15
16
17
18
# File 'lib/active_cypher/utils/logger.rb', line 14

def initialize
  @logger = ::Logger.new($stdout)
  @logger.level = ::Logger::INFO
  @logger.formatter = self.class.standard_formatter
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



12
13
14
# File 'lib/active_cypher/utils/logger.rb', line 12

def logger
  @logger
end

Class Method Details

.configure(options = {}) ⇒ Object



66
67
68
# File 'lib/active_cypher/utils/logger.rb', line 66

def configure(options = {})
  instance.configure(options)
end

.levelObject



70
71
72
# File 'lib/active_cypher/utils/logger.rb', line 70

def level
  instance.level
end

.level=(level) ⇒ Object



74
75
76
# File 'lib/active_cypher/utils/logger.rb', line 74

def level=(level)
  instance.level = level
end

.setupself

Setup with standard configuration for all examples

Returns:

  • (self)


89
90
91
# File 'lib/active_cypher/utils/logger.rb', line 89

def setup
  configure(formatter: standard_formatter)
end

.standard_formatterProc

Returns a standard formatter with time stamp

Returns:

  • (Proc)

    A standard log formatter



80
81
82
83
84
85
# File 'lib/active_cypher/utils/logger.rb', line 80

def standard_formatter
  proc do |severity, time, _progname, msg|
    time_str = time.strftime('%H:%M:%S.%L')
    "[#{time_str}] #{severity}: #{msg}\n"
  end
end

Instance Method Details

#configure(options = {}) ⇒ Object

Configure the logger

Parameters:

  • options (Hash) (defaults to: {})

    Configuration options

Options Hash (options):

  • :output (IO)

    Where to send logs (default: $stdout)

  • :level (Symbol, Integer)

    Log level (default: :info)

  • :formatter (Proc)

    Custom formatter for log messages



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/active_cypher/utils/logger.rb', line 25

def configure(options = {})
  @logger = ::Logger.new(options[:output]) if options[:output]

  if options[:level]
    level = options[:level]
    level = ::Logger.const_get(level.to_s.upcase) if level.is_a?(Symbol)
    @logger.level = level
  end

  @logger.formatter = options[:formatter] if options[:formatter]

  self
end

#levelInteger

Get the current logger level

Returns:

  • (Integer)

    Current log level



53
54
55
# File 'lib/active_cypher/utils/logger.rb', line 53

def level
  @logger.level
end

#level=(level) ⇒ Object

Set the logger level

Parameters:

  • level (Symbol, Integer)

    The log level



59
60
61
62
# File 'lib/active_cypher/utils/logger.rb', line 59

def level=(level)
  level = ::Logger.const_get(level.to_s.upcase) if level.is_a?(Symbol)
  @logger.level = level
end