Class: Localtower::Plugins::Capture
- Inherits:
-
Object
- Object
- Localtower::Plugins::Capture
- Defined in:
- lib/localtower/plugins/capture.rb
Constant Summary collapse
- LOG_FILE =
lambda { "#{Rails.root}/log/localtower_capture.log" }
- LOG_PATH =
lambda { "#{Rails.root}/log" }
- EXCLUDE_INSTANCE_VARIABLES =
[ "@_action_has_layout", "@_routes", "@_headers", "@_status", "@_request", "@__react_component_helper", "@_response", "@_env", "@env", "@controller", "@_lookup_context", "@_action_name", "@_response_body", ]
Class Method Summary collapse
Instance Method Summary collapse
- #clear ⇒ Object
-
#initialize(context = nil, context_binding = nil) ⇒ Capture
constructor
A new instance of Capture.
- #klass_name ⇒ Object
- #log(str) ⇒ Object
- #logs ⇒ Object
- #my_logger ⇒ Object
- #save ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(context = nil, context_binding = nil) ⇒ Capture
Returns a new instance of Capture.
44 45 46 47 |
# File 'lib/localtower/plugins/capture.rb', line 44 def initialize(context = nil, context_binding = nil) @context = context # self @context_binding = context_binding # binding end |
Class Method Details
.printable(content) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/localtower/plugins/capture.rb', line 23 def printable(content) if content.respond_to?(:to_json) JSON.pretty_generate(content) else content.to_s end end |
.type_of(value) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/localtower/plugins/capture.rb', line 31 def type_of(value) if value.instance_of?(Float); return Float.to_s; end; if value.instance_of?(Integer); return Integer.to_s; end; if value.instance_of?(String); return String.to_s; end; if value.instance_of?(Array); return Array.to_s; end; if value.instance_of?(Hash); return Hash.to_s; end; if value.instance_of?(ApplicationController); return ApplicationController.to_s; end; if value.instance_of?(NilClass); return NilClass.to_s; end; nil end |
Instance Method Details
#clear ⇒ Object
168 169 170 171 172 173 174 |
# File 'lib/localtower/plugins/capture.rb', line 168 def clear Dir["#{LOG_PATH.call}/localtower_capture_*.json"].each do |file| File.delete(file) end self end |
#klass_name ⇒ Object
158 159 160 161 162 163 164 165 166 |
# File 'lib/localtower/plugins/capture.rb', line 158 def klass_name value = if @context.class == "Class" @context else @context.class end value.to_s end |
#log(str) ⇒ Object
189 190 191 |
# File 'lib/localtower/plugins/capture.rb', line 189 def log(str) my_logger.info(str) end |
#logs ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/localtower/plugins/capture.rb', line 49 def logs list = [] Dir["#{LOG_PATH.call}/localtower_capture_*.json"].each do |file| json = JSON.parse(open(file).read) list << json end list end |
#my_logger ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/localtower/plugins/capture.rb', line 60 def my_logger @@my_logger ||= Logger.new(LOG_FILE.call) @@my_logger.formatter = proc do |severity, datetime, progname, msg| "#{msg}\n" end @@my_logger end |
#save ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/localtower/plugins/capture.rb', line 176 def save # We don't want to save logs in production: return nil if Rails.env.production? self.clear data = self.values json = data.to_json file = "#{LOG_PATH.call}/localtower_capture_#{data['md5']}.json" File.open(file, 'w') { |f| f.write(json) } end |
#values ⇒ Object
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/localtower/plugins/capture.rb', line 69 def values hash = {} callers = @context.send(:caller) a = callers[1] # xx/xx/app/controllers/clients/events_controller.rb:57:in `new' a = a.split(Rails.root.to_s).last # events_controller.rb:57:in `new' a = a.split("\:") file = a[0].strip line_number = a[1].strip method = a[2].strip.gsub("in \`", "").gsub("\'", "") sublime_path = "#{callers[1].split(":")[0]}:#{line_number}" hash["class"] = self.klass_name hash["file"] = "#{file}##{method}:#{line_number}" hash["method"] = method hash["md5"] = Digest::MD5.hexdigest(hash["file"]) hash["type"] = "CAPTURE_METHOD" hash["time"] = Time.now.utc.strftime('%Y-%m-%d %H:%M:%S.%L') variables = [] @context_binding.local_variables.each do |var| next if EXCLUDE_INSTANCE_VARIABLES.include?(var.to_s) value = @context_binding.local_variable_get(var) klass = self.class.type_of(value) data = { type: 'CAPTURE', time: Time.now.utc.strftime('%Y-%m-%d %H:%M:%S.%L'), event_name: var, identifier: nil, returned: value, meta: { from_klass: hash["class"], from_method: hash["method"], klass: klass.to_s, method: method.to_s, # arguments: data[:arguments], callers: callers, # table_name: data[:table_name], # sql: data[:sql], sublime_path: sublime_path, file: hash["file"], line: line_number } } variables << data end @context.instance_variables.each do |var| next if EXCLUDE_INSTANCE_VARIABLES.include?(var.to_s) value = @context.instance_variable_get(var.to_sym) klass = self.class.type_of(value) data = { type: 'CAPTURE', time: Time.now.utc.strftime('%Y-%m-%d %H:%M:%S.%L'), event_name: var, identifier: nil, returned: value, meta: { from_klass: hash["class"], from_method: hash["method"], klass: klass.to_s, method: method.to_s, # arguments: data[:arguments], callers: callers, # table_name: data[:table_name], # sql: data[:sql], sublime_path: sublime_path, file: hash["file"], line: line_number } } variables << data end hash["variables"] = variables hash end |