Class: Pakyow::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/pakyow/logger.rb,
lib/pakyow/logger/colorizer.rb,
lib/pakyow/logger/formatter.rb,
lib/pakyow/logger/timekeeper.rb,
lib/pakyow/logger/destination.rb,
lib/pakyow/logger/multiplexed.rb,
lib/pakyow/logger/thread_local.rb,
lib/pakyow/logger/formatters/json.rb,
lib/pakyow/logger/formatters/human.rb,
lib/pakyow/logger/formatters/logfmt.rb

Overview

Logs messages throughout the lifetime of an environment, connection, etc.

In addition to logging standard messages, this class provides a way to log a ‘prologue` and `epilogue` for a connection, as well as a `houston` method for logging errors.

Defined Under Namespace

Modules: Colorizer, Formatters, Timekeeper Classes: Destination, Formatter, Multiplexed, ThreadLocal

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, started_at: Time.now, id: SecureRandom.hex(4), output:, level:) ⇒ Logger

Returns a new instance of Logger.

Parameters:

  • type (Symbol)

    the type of logging being done (e.g. :http, :sock)

  • started_at (Time) (defaults to: Time.now)

    when the logging began

  • output (Object)

    the object that will perform the logging

  • id (String) (defaults to: SecureRandom.hex(4))

    a unique id used to identify the request



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pakyow/logger.rb', line 33

def initialize(type, started_at: Time.now, id: SecureRandom.hex(4), output:, level:)
  @type, @started_at, @id = type, started_at, id

  level = case level
  when :all
    0
  when :off
    7
  when Symbol
    self.class.const_get(:LEVELS)[level]
  else
    level
  end

  super(output, level: level)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



19
20
21
# File 'lib/pakyow/logger.rb', line 19

def id
  @id
end

#started_atObject (readonly)

Returns the value of attribute started_at.



23
24
25
# File 'lib/pakyow/logger.rb', line 23

def started_at
  @started_at
end

#typeObject (readonly)

Returns the value of attribute type.



27
28
29
# File 'lib/pakyow/logger.rb', line 27

def type
  @type
end

Instance Method Details

#<<(message) ⇒ Object



68
69
70
# File 'lib/pakyow/logger.rb', line 68

def <<(message)
  add(:unknown, message)
end

#add(level, message = nil, &block) ⇒ Object Also known as: log



72
73
74
# File 'lib/pakyow/logger.rb', line 72

def add(level, message = nil, &block)
  public_send(level, message, &block)
end

#elapsedObject



102
103
104
# File 'lib/pakyow/logger.rb', line 102

def elapsed
  (Time.now - @started_at)
end

#epilogue(connection) ⇒ Object

Logs the conclusion of a request, including the response status.

Parameters:

  • res (Array)

    the rack response array



90
91
92
# File 'lib/pakyow/logger.rb', line 90

def epilogue(connection)
  info { formatted_epilogue(connection) }
end

#houston(error) ⇒ Object

Logs an error raised when processing the request.

Parameters:

  • error (Object)

    the error object



98
99
100
# File 'lib/pakyow/logger.rb', line 98

def houston(error)
  error { formatted_error(error) }
end

#prologue(connection) ⇒ Object

Logs the beginning of a request, including the time, request method, request uri, and originating ip address.

Parameters:

  • env (Hash)

    the rack env for the request



82
83
84
# File 'lib/pakyow/logger.rb', line 82

def prologue(connection)
  info { formatted_prologue(connection) }
end

#silence(temporary_level = :error) ⇒ Object

Temporarily silences logs, up to temporary_level.



52
53
54
55
56
57
58
# File 'lib/pakyow/logger.rb', line 52

def silence(temporary_level = :error)
  original_level = @level
  self.level = self.class.const_get(:LEVELS)[temporary_level]
  yield
ensure
  self.level = original_level
end