Module: SemanticLogger::Utils
- Defined in:
- lib/semantic_logger/utils.rb
Overview
Internal-use only utility functions for Semantic Logger. Not intended for public use.
Class Method Summary collapse
-
.camelize(term) ⇒ Object
Borrow from Rails, when not running Rails.
- .constantize_symbol(symbol, namespace = "SemanticLogger::Appender") ⇒ Object
-
.extract_backtrace(stack = caller) ⇒ Object
Extract the backtrace stripping off the leading semantic logger entries.
-
.extract_path?(path) ⇒ Boolean
Whether this path should be excluded from any cleansed backtrace.
- .extract_paths ⇒ Object
-
.method_visibility(mod, method_name) ⇒ Object
Returns the visibility for an instance method.
-
.strip_backtrace(stack = caller) ⇒ Object
Try to strip everything off of the supplied backtrace, until the first application stack entry is at the top.
-
.strip_path?(path) ⇒ Boolean
Whether this path should be excluded from any cleansed backtrace.
-
.strip_paths ⇒ Object
Paths to exclude in the stripped backtrace Includes Gems and built-in Ruby code paths.
Class Method Details
.camelize(term) ⇒ Object
Borrow from Rails, when not running Rails
16 17 18 19 20 21 22 |
# File 'lib/semantic_logger/utils.rb', line 16 def self.camelize(term) string = term.to_s string = string.sub(/^[a-z\d]*/, &:capitalize) string.gsub!(%r{(?:_|(/))([a-z\d]*)}i) { "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}" } string.gsub!("/".freeze, "::".freeze) string end |
.constantize_symbol(symbol, namespace = "SemanticLogger::Appender") ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/semantic_logger/utils.rb', line 5 def self.constantize_symbol(symbol, namespace = "SemanticLogger::Appender") klass = "#{namespace}::#{camelize(symbol.to_s)}" begin Object.const_get(klass) rescue NameError raise(ArgumentError, "Could not convert symbol: #{symbol.inspect} to a class in: #{namespace}. Looking for: #{klass}") end end |
.extract_backtrace(stack = caller) ⇒ Object
Extract the backtrace stripping off the leading semantic logger entries. Leaves all other system and gem path entries in place.
38 39 40 41 42 43 |
# File 'lib/semantic_logger/utils.rb', line 38 def self.extract_backtrace(stack = caller) while (first = stack.first) && extract_path?(first) stack.shift end stack end |
.extract_path?(path) ⇒ Boolean
Whether this path should be excluded from any cleansed backtrace
50 51 52 |
# File 'lib/semantic_logger/utils.rb', line 50 def self.extract_path?(path) extract_paths.any? { |exclude| path.include?(exclude) } end |
.extract_paths ⇒ Object
45 46 47 |
# File 'lib/semantic_logger/utils.rb', line 45 def self.extract_paths @extract_paths ||= %w[lib/semantic_logger lib/rails_semantic_logger] end |
.method_visibility(mod, method_name) ⇒ Object
Returns the visibility for an instance method
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/semantic_logger/utils.rb', line 25 def self.method_visibility(mod, method_name) method_name = method_name.to_sym if mod.instance_methods.include?(method_name) :public elsif mod.private_instance_methods.include?(method_name) :private elsif mod.protected_instance_methods.include?(method_name) :protected end end |
.strip_backtrace(stack = caller) ⇒ Object
Try to strip everything off of the supplied backtrace, until the first application stack entry is at the top. For example all leading gem paths and built-in ruby code paths are removed from the top. Once the first application entry is found, the remaining stack is returned.
57 58 59 60 61 62 |
# File 'lib/semantic_logger/utils.rb', line 57 def self.strip_backtrace(stack = caller) while (first = stack.first) && (strip_path?(first) || extract_path?(first)) stack.shift end stack end |
.strip_path?(path) ⇒ Boolean
Whether this path should be excluded from any cleansed backtrace
76 77 78 |
# File 'lib/semantic_logger/utils.rb', line 76 def self.strip_path?(path) strip_paths.any? { |exclude| path.start_with?(exclude) } end |
.strip_paths ⇒ Object
Paths to exclude in the stripped backtrace Includes Gems and built-in Ruby code paths
66 67 68 69 70 71 72 73 |
# File 'lib/semantic_logger/utils.rb', line 66 def self.strip_paths @strip_paths ||= begin paths = Gem.path | [Gem.default_dir] paths << RbConfig::CONFIG["rubylibdir"] paths end end |