Class: Civo::PerRequestLogger

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/civo/logger/middleware.rb

Constant Summary collapse

STATS_RESOLUTION =
5.minutes
INFLUXDB_PRECISION =
'ms'
INFLUXDB_RETENTION =
'7d.cpu'

Instance Method Summary collapse

Constructor Details

#initialize(app, taggers = nil) ⇒ PerRequestLogger

Returns a new instance of PerRequestLogger.



16
17
18
19
20
21
22
23
24
# File 'lib/civo/logger/middleware.rb', line 16

def initialize(app, taggers = nil)
  self.class.expires_in ||= 24.hours.to_i
  @app          = app
  @taggers      = taggers || []
  Rails.logger = ActionController::Base.logger = ActiveRecord::Base.logger = self
  $stdout.sync = true
  options = {host: ENV["INFLUX_LOGGER_HOSTNAME"], port: ENV["INFLUX_LOGGER_PORT"], password: ENV["INFLUX_LOGGER_PASSWORD"], username: ENV["INFLUX_LOGGER_USERNAME"], timeout: 1, time_precision: "ms"}
  @influxdb = InfluxDB::Client.new ENV["INFLUX_LOGGER_DATABASE"], options
end

Instance Method Details

#app_nameObject



32
33
34
# File 'lib/civo/logger/middleware.rb', line 32

def app_name
  ENV["APP_NAME"] || "unknown-app"
end

#call(env) ⇒ Object



26
27
28
29
30
# File 'lib/civo/logger/middleware.rb', line 26

def call(env)
  request = ActionDispatch::Request.new(env)

  call_app(request, env)
end

#debug(message = "") ⇒ Object



36
37
38
39
40
# File 'lib/civo/logger/middleware.rb', line 36

def debug(message = "")
  message = yield if block_given?
  return if message.blank?
  @lines << message if debug? && @recording
end

#debug?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/civo/logger/middleware.rb', line 42

def debug?
  %i{debug}.include?(level)
end

#end_workerObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/civo/logger/middleware.rb', line 107

def end_worker
  puts @lines.join("\n") if Rails.env.production?

  if @influxdb.nil?
    puts @log
    return
  end

  data = {
    values:    { log: @lines.join("\n") },
    tags:      { name: @name, app: app_name },
    timestamp: (Time.now.to_r * 1000).to_i
  }

  @influxdb.write_point("workers", data, INFLUXDB_PRECISION)# , INFLUXDB_RETENTION)
# rescue Redis::TimeoutError, Redis::CannotConnectError => e
#   puts "ERROR! Redis error #{e.message}"
ensure
  @recording = false
end

#error(message = "") ⇒ Object



72
73
74
75
76
# File 'lib/civo/logger/middleware.rb', line 72

def error(message = "")
  message = yield if block_given?
  return if message.blank?
  @lines << message if error? && @recording
end

#error?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/civo/logger/middleware.rb', line 78

def error?
  %i{debug info warn error}.include?(level)
end

#fatal(message = "") ⇒ Object



82
83
84
85
86
# File 'lib/civo/logger/middleware.rb', line 82

def fatal(message = "")
  message = yield if block_given?
  return if message.blank?
  @lines << message if fatal? && @recording
end

#fatal?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/civo/logger/middleware.rb', line 88

def fatal?
  %i{debug info warn error fatal}.include?(level)
end

#formatterObject



96
97
98
# File 'lib/civo/logger/middleware.rb', line 96

def formatter
  ActiveSupport::Logger::SimpleFormatter
end

#info(message = "") ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/civo/logger/middleware.rb', line 46

def info(message = "")
  message = yield if block_given?
  return if message.blank?

  matches = message.match(/Completed (\d+) .*? in (\d+)ms/)
  if matches
    @status = matches[1]
    @time_taken = matches[2]
  end
  @lines << message if info? && @recording
end

#info?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/civo/logger/middleware.rb', line 58

def info?
  %i{debug info}.include?(level)
end

#levelObject



92
93
94
# File 'lib/civo/logger/middleware.rb', line 92

def level
  Rails.configuration.log_level
end

#start_worker(name) ⇒ Object



100
101
102
103
104
105
# File 'lib/civo/logger/middleware.rb', line 100

def start_worker(name)
  @name = name
  @lines = []
  @start = Time.now.to_f
  @recording = true
end

#warn(message = "") ⇒ Object



62
63
64
65
66
# File 'lib/civo/logger/middleware.rb', line 62

def warn(message = "")
  message = yield if block_given?
  return if message.blank?
  @lines << message if warn? && @recording
end

#warn?Boolean

Returns:

  • (Boolean)


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

def warn?
  %i{debug info warn}.include?(level)
end