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
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
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/civo/logger/middleware.rb', line 11 def initialize(app, taggers = nil) @app = app @taggers = taggers || [] Rails.logger = ActionController::Base.logger = ActiveRecord::Base.logger = self $stdout.sync = true if ENV["RAILS_LOGGER_HOSTNAME"].present? @redis = Redis.new(host: ENV["RAILS_LOGGER_HOSTNAME"], port: Rails.application.config.x.redis_port, password: ENV["RAILS_LOGGER_PASSWORD"], timeout: 1) else @redis = Redis.current end end |
Instance Method Details
#app_name ⇒ Object
29 30 31 |
# File 'lib/civo/logger/middleware.rb', line 29 def app_name ENV["APP_NAME"] || "unknown-app" end |
#call(env) ⇒ Object
23 24 25 26 27 |
# File 'lib/civo/logger/middleware.rb', line 23 def call(env) request = ActionDispatch::Request.new(env) call_app(request, env) end |
#debug(message = "") ⇒ Object
33 34 35 36 37 |
# File 'lib/civo/logger/middleware.rb', line 33 def debug( = "") = yield if block_given? return if .blank? @lines << if debug? && @recording end |
#debug? ⇒ Boolean
39 40 41 |
# File 'lib/civo/logger/middleware.rb', line 39 def debug? i{debug}.include?(level) end |
#end_worker ⇒ Object
105 106 107 108 109 |
# File 'lib/civo/logger/middleware.rb', line 105 def end_worker @redis.set("#{app_name}:log:#{@key}", @lines.join("\n"), expires_in: 1.hour) puts @lines.join("\n") if Rails.env.production? @recording = false end |
#error(message = "") ⇒ Object
69 70 71 72 73 |
# File 'lib/civo/logger/middleware.rb', line 69 def error( = "") = yield if block_given? return if .blank? @lines << if error? && @recording end |
#error? ⇒ Boolean
75 76 77 |
# File 'lib/civo/logger/middleware.rb', line 75 def error? i{debug info warn error}.include?(level) end |
#fatal(message = "") ⇒ Object
79 80 81 82 83 |
# File 'lib/civo/logger/middleware.rb', line 79 def fatal( = "") = yield if block_given? return if .blank? @lines << if fatal? && @recording end |
#fatal? ⇒ Boolean
85 86 87 |
# File 'lib/civo/logger/middleware.rb', line 85 def fatal? i{debug info warn error fatal}.include?(level) end |
#formatter ⇒ Object
93 94 95 |
# File 'lib/civo/logger/middleware.rb', line 93 def formatter ActiveSupport::Logger::SimpleFormatter end |
#info(message = "") ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/civo/logger/middleware.rb', line 43 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
55 56 57 |
# File 'lib/civo/logger/middleware.rb', line 55 def info? i{debug info}.include?(level) end |
#level ⇒ Object
89 90 91 |
# File 'lib/civo/logger/middleware.rb', line 89 def level Rails.configuration.log_level end |
#start_worker(name) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/civo/logger/middleware.rb', line 97 def start_worker(name) @name = name @key = Time.zone.now.strftime("%Y%m%d-%H%M%S")+"-#{name}" @lines = [] @start = Time.now.to_f @recording = true end |
#warn(message = "") ⇒ Object
59 60 61 62 63 |
# File 'lib/civo/logger/middleware.rb', line 59 def warn( = "") = yield if block_given? return if .blank? @lines << if warn? && @recording end |
#warn? ⇒ Boolean
65 66 67 |
# File 'lib/civo/logger/middleware.rb', line 65 def warn? i{debug info warn}.include?(level) end |