Class: UnifiedLogger::Logger
- Inherits:
-
Logger
- Object
- Logger
- UnifiedLogger::Logger
- Defined in:
- lib/unified_logger/logger.rb
Constant Summary collapse
- LOGS =
Concurrent::ThreadLocalVar.new([])
- EXTRA_LOG_FIELDS =
Concurrent::ThreadLocalVar.new({})
- SILENCED =
Concurrent::ThreadLocalVar.new(false)
- SEVERITY_LEVELS =
{ debug: ::Logger::DEBUG, info: ::Logger::INFO, note: ::Logger::Severity::NOTE, warn: ::Logger::WARN, error: ::Logger::ERROR, fatal: ::Logger::FATAL, unknown: ::Logger::UNKNOWN }.freeze
- SEVERITY_MAP =
SEVERITY_LEVELS.invert.freeze
Class Method Summary collapse
- .add(hash) ⇒ Object
- .extra_log_fields ⇒ Object
- .format(log) ⇒ Object
- .format_exception(exception) ⇒ Object
- .logs ⇒ Object
- .reset_thread_logs ⇒ Object
- .setup_silencing(path) ⇒ Object
- .silenced? ⇒ Boolean
- .trim(data) ⇒ Object
- .write_log(log) ⇒ Object
Instance Method Summary collapse
- #<<(message) ⇒ Object
- #add(severity, message = nil, progname = nil, &block) ⇒ Object
- #debug(message = nil, &block) ⇒ Object
- #error(message = nil, &block) ⇒ Object
- #fatal(message = nil, &block) ⇒ Object
- #info(message = nil, &block) ⇒ Object
-
#initialize(logging_device = $stdout) ⇒ Logger
constructor
A new instance of Logger.
- #note(message = nil, &block) ⇒ Object
- #unknown(message = nil, &block) ⇒ Object
- #warn(message = nil, &block) ⇒ Object
- #write(message) ⇒ Object
Constructor Details
#initialize(logging_device = $stdout) ⇒ Logger
Returns a new instance of Logger.
17 18 19 20 21 |
# File 'lib/unified_logger/logger.rb', line 17 def initialize(logging_device = $stdout, *) super @logging_device = logging_device self.formatter = proc {} end |
Class Method Details
.add(hash) ⇒ Object
82 83 84 85 86 |
# File 'lib/unified_logger/logger.rb', line 82 def add(hash) return if SILENCED.value EXTRA_LOG_FIELDS.value = EXTRA_LOG_FIELDS.value.merge(hash) end |
.extra_log_fields ⇒ Object
88 89 90 |
# File 'lib/unified_logger/logger.rb', line 88 def extra_log_fields EXTRA_LOG_FIELDS.value end |
.format(log) ⇒ Object
128 129 130 131 132 |
# File 'lib/unified_logger/logger.rb', line 128 def format(log) filtered_log = filter(log) formatter = UnifiedLogger.format_log_callable formatter.present? ? formatter.call(filtered_log) : filtered_log.to_json end |
.format_exception(exception) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/unified_logger/logger.rb', line 112 def format_exception(exception) if exception.is_a?(String) { message: exception } elsif exception.is_a?(Exception) btc = ActiveSupport::BacktraceCleaner.new prefix = UnifiedLogger.backtrace_root + File::SEPARATOR btc.add_filter { |line| line.sub(/\A#{Regexp.escape(prefix)}/, "") } btc.add_silencer { |line| /request_logger.rb/.match?(line) } { class_name: exception.class.name, message: exception., backtrace: btc.clean(exception.backtrace || []) } elsif exception.respond_to?(:to_s) exception.to_s else exception.class.name end end |
.logs ⇒ Object
68 69 70 |
# File 'lib/unified_logger/logger.rb', line 68 def logs LOGS.value end |
.reset_thread_logs ⇒ Object
92 93 94 95 96 |
# File 'lib/unified_logger/logger.rb', line 92 def reset_thread_logs LOGS.value = [] EXTRA_LOG_FIELDS.value = {} SILENCED.value = false end |
.setup_silencing(path) ⇒ Object
76 77 78 79 80 |
# File 'lib/unified_logger/logger.rb', line 76 def setup_silencing(path) SILENCED.value = UnifiedLogger.config[:silence_paths].any? do |pattern| pattern.is_a?(Regexp) ? pattern.match?(path) : pattern == path end end |
.silenced? ⇒ Boolean
72 73 74 |
# File 'lib/unified_logger/logger.rb', line 72 def silenced? SILENCED.value end |
.trim(data) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/unified_logger/logger.rb', line 98 def trim(data) data = filter(data) size = data.inspect.length max = UnifiedLogger.config[:max_log_field_size] return data if size < max begin json = JSON.generate(data, quirks_mode: true) rescue JSON::GeneratorError, Encoding::UndefinedConversionError json = data.respond_to?("to_s") ? data.to_s : "unparseable object (instance of #{data.class.name})" end "#{json[0..max]}... (#{size - max} extra characters omitted)" end |
.write_log(log) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/unified_logger/logger.rb', line 134 def write_log(log) logger = UnifiedLogger.current_logger max = UnifiedLogger.config[:max_log_size] if log.inspect.length <= max || !log.key?(:logs) logger.write(format(log)) else entries = log.delete(:logs) logger.write(format(log)) base_fields = log.except(:request, :response) write_overflow_logs(base_fields, entries, max, logger) end end |
Instance Method Details
#<<(message) ⇒ Object
58 59 60 61 |
# File 'lib/unified_logger/logger.rb', line 58 def <<() add(::Logger::UNKNOWN, .to_s.chomp) self end |
#add(severity, message = nil, progname = nil, &block) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/unified_logger/logger.rb', line 185 def add(severity, = nil, progname = nil, &block) if .nil? = block ? block.call : progname end return true if .blank? return true unless severity >= level severity_symbol = SEVERITY_MAP[severity] || :unknown append_log(severity_symbol, ) end |
#debug(message = nil, &block) ⇒ Object
23 24 25 26 |
# File 'lib/unified_logger/logger.rb', line 23 def debug( = nil, &block) = block.call if .nil? && block add(::Logger::DEBUG, ) end |
#error(message = nil, &block) ⇒ Object
43 44 45 46 |
# File 'lib/unified_logger/logger.rb', line 43 def error( = nil, &block) = block.call if .nil? && block add(::Logger::ERROR, ) end |
#fatal(message = nil, &block) ⇒ Object
48 49 50 51 |
# File 'lib/unified_logger/logger.rb', line 48 def fatal( = nil, &block) = block.call if .nil? && block add(::Logger::FATAL, ) end |
#info(message = nil, &block) ⇒ Object
28 29 30 31 |
# File 'lib/unified_logger/logger.rb', line 28 def info( = nil, &block) = block.call if .nil? && block add(::Logger::INFO, ) end |
#note(message = nil, &block) ⇒ Object
33 34 35 36 |
# File 'lib/unified_logger/logger.rb', line 33 def note( = nil, &block) = block.call if .nil? && block add(::Logger::Severity::NOTE, ) end |
#unknown(message = nil, &block) ⇒ Object
53 54 55 56 |
# File 'lib/unified_logger/logger.rb', line 53 def unknown( = nil, &block) = block.call if .nil? && block add(::Logger::UNKNOWN, ) end |
#warn(message = nil, &block) ⇒ Object
38 39 40 41 |
# File 'lib/unified_logger/logger.rb', line 38 def warn( = nil, &block) = block.call if .nil? && block add(::Logger::WARN, ) end |
#write(message) ⇒ Object
63 64 65 |
# File 'lib/unified_logger/logger.rb', line 63 def write() @logging_device.write("#{}\n") if @logging_device.respond_to?(:write) end |