Class: Roda::EnhancedLogger::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/roda/enhanced_logger/instance.rb

Overview

Logger instance for this application

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, env, instance_id, root, filter) ⇒ Instance

Returns a new instance of Instance.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/roda/enhanced_logger/instance.rb', line 29

def initialize(logger, env, instance_id, root, filter)
  @logger = logger
  @root = root
  @log_entries = []
  @matches = []
  @timer = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  @filter = filter || proc { false }
  if env["enhanced_logger_id"].nil?
    @primary = true
    env["enhanced_logger_id"] = instance_id
  else
    @primary = false
  end
end

Instance Attribute Details

#filterObject (readonly)

Callable object to filter log entries



27
28
29
# File 'lib/roda/enhanced_logger/instance.rb', line 27

def filter
  @filter
end

#log_entriesObject (readonly)

Log entries generated during request



15
16
17
# File 'lib/roda/enhanced_logger/instance.rb', line 15

def log_entries
  @log_entries
end

#loggerObject (readonly)

Logger instance



18
19
20
# File 'lib/roda/enhanced_logger/instance.rb', line 18

def logger
  @logger
end

#matchesObject (readonly)

Route matches during request



21
22
23
# File 'lib/roda/enhanced_logger/instance.rb', line 21

def matches
  @matches
end

#rootObject (readonly)

Application root



12
13
14
# File 'lib/roda/enhanced_logger/instance.rb', line 12

def root
  @root
end

Instance Method Details

#add(status, request, trace = false) ⇒ Object

Add log entry for request

Parameters:

  • status (Integer)

    status code for the response

  • request (Roda::RodaRequest)

    request object

  • trace (Boolean) (defaults to: false)

    tracing was enabled



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/roda/enhanced_logger/instance.rb', line 56

def add(status, request, trace = false)
  if (last_matched_caller = matches.last)
    handler = format("%s:%d",
      Pathname(last_matched_caller.path).relative_path_from(root),
      last_matched_caller.lineno)
  end

  meth =
    case status
    when 400..499
      :warn
    when 500..599
      :error
    else
      :info
    end

  data = {
    duration: (Process.clock_gettime(Process::CLOCK_MONOTONIC) - timer).round(4),
    status: status,
    verb: request.request_method,
    path: request.path,
    remaining_path: request.remaining_path,
    handler: handler,
    params: request.params
  }

  if (db = Roda::EnhancedLogger::Current.accrued_database_time)
    data[:db] = db.round(6)
  end

  if (query_count = Roda::EnhancedLogger::Current.database_query_count)
    data[:db_queries] = query_count
  end

  if trace
    matches.each do |match|
      add_log_entry([meth, format("  %s (%s:%s)",
        File.readlines(match.path)[match.lineno - 1].strip.sub(" do", ""),
        Pathname(match.path).relative_path_from(root),
        match.lineno)])
    end
  end

  return if filter.call(request.path)

  add_log_entry([meth, "#{request.request_method} #{request.path}", data])
end

#add_match(caller) ⇒ Object

Add a matched route handler



45
46
47
# File 'lib/roda/enhanced_logger/instance.rb', line 45

def add_match(caller)
  @matches << caller
end

#drainBoolean

Drain the log entry queue, writing each to the logger at their respective level

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
121
# File 'lib/roda/enhanced_logger/instance.rb', line 113

def drain
  return unless primary?

  log_entries.each do |args|
    logger.public_send(*args)
  end

  true
end

#primary?Boolean

This instance is the primary logger

Returns:

  • (Boolean)


107
108
109
# File 'lib/roda/enhanced_logger/instance.rb', line 107

def primary?
  @primary
end

#resetBoolean

Reset the counters for this thread

Returns:

  • (Boolean)


125
126
127
# File 'lib/roda/enhanced_logger/instance.rb', line 125

def reset
  Roda::EnhancedLogger::Current.reset
end