Class: Logee::Logger

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

Instance Method Summary collapse

Constructor Details

#initialize(name:, context: {}, parent: nil, target: $stdout) ⇒ Logger

Returns a new instance of Logger.



7
8
9
10
11
12
13
# File 'lib/logee.rb', line 7

def initialize(name:, context: {}, parent: nil, target: $stdout)
  @name = name
  @context = context
  @parent = parent
  @target = target
  @hostname = @parent.nil? ? `hostname`.strip : @parent.hostname
end

Instance Method Details

#child(name, context = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/logee.rb', line 36

def child(name, context = {})
  child_logger = Logee::Logger.
    new("#{@name}::#{name}", @context.merge(context))
  child_logger.set_instance_variable(:@parent, self)
  if block_given?
    yield child_logger
  else
    child_logger
  end
end

#context(ctx = nil) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/logee.rb', line 15

def context(ctx = nil)
  if ctx.nil?
    @context
  else
    @context.merge! ctx
  end
end

#msg(msg, context = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/logee.rb', line 23

def msg(msg, context = {})
  JSON.dump(
    @context.merge(context.merge({
      msg: msg,
      time: Time.now.utc.iso8601(3),
      name: @name,
      hostname: @hostname,
      pid: Process.pid,
    })),
    @target
  )
end