Class: Liquid::Logger
- Inherits:
-
Object
- Object
- Liquid::Logger
- Defined in:
- lib/liquid/logger.rb
Instance Attribute Summary collapse
-
#appender ⇒ Object
Returns the value of attribute appender.
-
#progname ⇒ Object
Returns the value of attribute progname.
Instance Method Summary collapse
- #_log_error_exception(exc, message, attribs) ⇒ Object
- #add_exception_handler(&block) ⇒ Object
- #debug(*args, &block) ⇒ Object
- #debug? ⇒ Boolean
- #error(*args, &block) ⇒ Object
- #error? ⇒ Boolean
- #exception(exc, message = nil, attribs = {}) ⇒ Object
- #info(*args, &block) ⇒ Object
- #info? ⇒ Boolean
-
#initialize(name, progname = nil) ⇒ Logger
constructor
A new instance of Logger.
- #mute! ⇒ Object
- #reload! ⇒ Object
- #trace(*args, &block) ⇒ Object
- #trace? ⇒ Boolean
- #unmute! ⇒ Object
- #warn(*args, &block) ⇒ Object
- #warn? ⇒ Boolean
Constructor Details
#initialize(name, progname = nil) ⇒ Logger
Returns a new instance of Logger.
13 14 15 16 17 18 19 20 |
# File 'lib/liquid/logger.rb', line 13 def initialize(name, progname = nil) @java = RUBY_PLATFORM == "java" @progname = progname || File.basename($0) @logger = @java ? LoggerFactory.getLogger(name) : ::Logger.new(STDOUT) @exceptions = {} @exception_handlers = [method(:_log_error_exception)] unmute! end |
Instance Attribute Details
#appender ⇒ Object
Returns the value of attribute appender.
11 12 13 |
# File 'lib/liquid/logger.rb', line 11 def appender @appender end |
#progname ⇒ Object
Returns the value of attribute progname.
10 11 12 |
# File 'lib/liquid/logger.rb', line 10 def progname @progname end |
Instance Method Details
#_log_error_exception(exc, message, attribs) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/liquid/logger.rb', line 112 def _log_error_exception(exc, , attribs) ::Metrics.meter("exception:#{exc.class.to_s.tableize}").mark @exceptions[exc.class] ||= {} @exceptions[exc.class][exc.backtrace.first] ||= [System.nano_time, 1, 1] five_minutes_ago = System.nano_time - 300_000_000_000 last, count, backoff = *@exceptions[exc.class][exc.backtrace.first] count = backoff = 1 if last < five_minutes_ago backoff = count > backoff ? backoff * 2 : backoff if count % backoff == 0 error("exception", { class: exc.class, count: count, reason: exc., message: , backtrace: exc.backtrace }.merge(attribs).merge(called_from)) end @exceptions[exc.class][exc.backtrace.first] = [ System.nano_time, count + 1, backoff ] end |
#add_exception_handler(&block) ⇒ Object
102 103 104 |
# File 'lib/liquid/logger.rb', line 102 def add_exception_handler(&block) @exception_handlers << block end |
#debug(*args, &block) ⇒ Object
66 67 68 69 70 |
# File 'lib/liquid/logger.rb', line 66 def debug(*args, &block) return unless debug? args = yield if block_given? @logger.debug(format(*args)) end |
#debug? ⇒ Boolean
62 63 64 |
# File 'lib/liquid/logger.rb', line 62 def debug? @java ? @logger.debug_enabled? : @logger.debug? end |
#error(*args, &block) ⇒ Object
96 97 98 99 100 |
# File 'lib/liquid/logger.rb', line 96 def error(*args, &block) return unless error? args = yield if block_given? @logger.error(format(*args)) end |
#error? ⇒ Boolean
92 93 94 |
# File 'lib/liquid/logger.rb', line 92 def error? @java ? @logger.error_enabled? : @logger.error? end |
#exception(exc, message = nil, attribs = {}) ⇒ Object
106 107 108 109 110 |
# File 'lib/liquid/logger.rb', line 106 def exception(exc, = nil, attribs = {}) @exception_handlers.each do |callback| callback.call(exc, , attribs) end end |
#info(*args, &block) ⇒ Object
76 77 78 79 80 |
# File 'lib/liquid/logger.rb', line 76 def info(*args, &block) return unless info? args = yield if block_given? @logger.info(format(*args)) end |
#info? ⇒ Boolean
72 73 74 |
# File 'lib/liquid/logger.rb', line 72 def info? @java ? @logger.info_enabled? : @logger.info? end |
#mute! ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/liquid/logger.rb', line 34 def mute! if @java @appender = org.apache.log4j.varia.NullAppender else @logger = ::Logger.new("/dev/null") end reload! end |
#reload! ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/liquid/logger.rb', line 22 def reload! return unless @java root = org.apache.log4j.Logger.getRootLogger appender = @appender.new appender.name = "default" appender.layout = org.apache.log4j.PatternLayout.new($conf.log.format) appender.threshold = org.apache.log4j.Level.toLevel($conf.log.level.to_s) appender.activateOptions root.removeAllAppenders root.addAppender(appender) end |
#trace(*args, &block) ⇒ Object
56 57 58 59 60 |
# File 'lib/liquid/logger.rb', line 56 def trace(*args, &block) return unless trace? args = yield if block_given? @logger.trace(format(*args)) end |
#trace? ⇒ Boolean
52 53 54 |
# File 'lib/liquid/logger.rb', line 52 def trace? @java ? @logger.trace_enabled? : @logger.trace? end |
#unmute! ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/liquid/logger.rb', line 43 def unmute! if @java @appender = org.apache.log4j.ConsoleAppender else @logger = ::Logger.new(STDOUT) end reload! end |
#warn(*args, &block) ⇒ Object
86 87 88 89 90 |
# File 'lib/liquid/logger.rb', line 86 def warn(*args, &block) return unless warn? args = yield if block_given? @logger.warn(format(*args)) end |
#warn? ⇒ Boolean
82 83 84 |
# File 'lib/liquid/logger.rb', line 82 def warn? @java ? @logger.warn_enabled? : @logger.warn? end |