Class: Civo::PerRequestLogger
- Inherits:
-
ActiveSupport::LogSubscriber
- Object
- ActiveSupport::LogSubscriber
- Civo::PerRequestLogger
- 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
- #app_name ⇒ Object
- #call(env) ⇒ Object
- #debug(message = "") ⇒ Object
- #debug? ⇒ Boolean
- #end_worker ⇒ Object
- #error(message = "") ⇒ Object
- #error? ⇒ Boolean
- #fatal(message = "") ⇒ Object
- #fatal? ⇒ Boolean
- #formatter ⇒ Object
- #info(message = "") ⇒ Object
- #info? ⇒ Boolean
-
#initialize(app, taggers = nil) ⇒ PerRequestLogger
constructor
A new instance of PerRequestLogger.
- #level ⇒ Object
- #start_worker(name) ⇒ Object
- #warn(message = "") ⇒ Object
- #warn? ⇒ Boolean
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 = {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"], end |
Instance Method Details
#app_name ⇒ Object
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( = "") = yield if block_given? return if .blank? @lines << if debug? && @recording end |
#debug? ⇒ Boolean
42 43 44 |
# File 'lib/civo/logger/middleware.rb', line 42 def debug? %i{debug}.include?(level) end |
#end_worker ⇒ Object
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( = "") = yield if block_given? return if .blank? @lines << if error? && @recording end |
#error? ⇒ 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( = "") = yield if block_given? return if .blank? @lines << if fatal? && @recording end |
#fatal? ⇒ Boolean
88 89 90 |
# File 'lib/civo/logger/middleware.rb', line 88 def fatal? %i{debug info warn error fatal}.include?(level) end |
#formatter ⇒ Object
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( = "") = yield if block_given? return if .blank? matches = .match(/Completed (\d+) .*? in (\d+)ms/) if matches @status = matches[1] @time_taken = matches[2] end @lines << if info? && @recording end |
#info? ⇒ Boolean
58 59 60 |
# File 'lib/civo/logger/middleware.rb', line 58 def info? %i{debug info}.include?(level) end |
#level ⇒ Object
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( = "") = yield if block_given? return if .blank? @lines << if warn? && @recording end |
#warn? ⇒ Boolean
68 69 70 |
# File 'lib/civo/logger/middleware.rb', line 68 def warn? %i{debug info warn}.include?(level) end |