Class: NatsWork::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/natswork/logger.rb

Constant Summary collapse

LEVELS =
{
  debug: ::Logger::DEBUG,
  info: ::Logger::INFO,
  warn: ::Logger::WARN,
  error: ::Logger::ERROR,
  fatal: ::Logger::FATAL
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Logger

Returns a new instance of Logger.



19
20
21
22
23
24
25
26
27
# File 'lib/natswork/logger.rb', line 19

def initialize(options = {})
  @namespace = options[:namespace] || 'natswork'
  @structured = options.fetch(:structured, false)
  @output = options[:output] || $stdout
  @level = options[:level] || :info
  @formatter = options[:formatter]

  setup_logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



17
18
19
# File 'lib/natswork/logger.rb', line 17

def logger
  @logger
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



17
18
19
# File 'lib/natswork/logger.rb', line 17

def namespace
  @namespace
end

#structuredObject (readonly)

Returns the value of attribute structured.



17
18
19
# File 'lib/natswork/logger.rb', line 17

def structured
  @structured
end

Class Method Details

.configure(options = {}) ⇒ Object



143
144
145
# File 'lib/natswork/logger.rb', line 143

def configure(options = {})
  @global = new(options)
end

.globalObject



139
140
141
# File 'lib/natswork/logger.rb', line 139

def global
  @global ||= new
end

.method_missing(method, *args, &block) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/natswork/logger.rb', line 147

def method_missing(method, *args, &block)
  if global.respond_to?(method)
    global.send(method, *args, &block)
  else
    super
  end
end

.respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/natswork/logger.rb', line 155

def respond_to_missing?(method, include_private = false)
  global.respond_to?(method, include_private) || super
end

Instance Method Details

#debug(message, context = {}, &block) ⇒ Object



29
30
31
# File 'lib/natswork/logger.rb', line 29

def debug(message, context = {}, &block)
  log(:debug, message, context, &block)
end

#error(message, context = {}, &block) ⇒ Object



41
42
43
# File 'lib/natswork/logger.rb', line 41

def error(message, context = {}, &block)
  log(:error, message, context, &block)
end

#fatal(message, context = {}, &block) ⇒ Object



45
46
47
# File 'lib/natswork/logger.rb', line 45

def fatal(message, context = {}, &block)
  log(:fatal, message, context, &block)
end

#info(message, context = {}, &block) ⇒ Object



33
34
35
# File 'lib/natswork/logger.rb', line 33

def info(message, context = {}, &block)
  log(:info, message, context, &block)
end

#level=(level) ⇒ Object



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

def level=(level)
  @level = level
  @logger.level = LEVELS[level] || ::Logger::INFO
end

#warn(message, context = {}, &block) ⇒ Object



37
38
39
# File 'lib/natswork/logger.rb', line 37

def warn(message, context = {}, &block)
  log(:warn, message, context, &block)
end

#with_namespace(namespace) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/natswork/logger.rb', line 49

def with_namespace(namespace)
  self.class.new(
    namespace: "#{@namespace}.#{namespace}",
    structured: @structured,
    output: @output,
    level: @level,
    formatter: @formatter
  )
end