Class: RightSupport::Log::FilterLogger
- Defined in:
- lib/right_support/log/filter_logger.rb
Overview
A logger than encapsulates an underlying Logger object and filters log entries before they are passed to the underlying Logger. Can be used to for various log- processing tasks such as filtering sensitive data or tagging log lines with a context marker.
FilterLogger implements method_missing and respond_to? and proxies unknown method calls to its underlying logger; this allows it to be used as a decorator.
Direct Known Subclasses
Constant Summary collapse
- SEVERITY_TO_METHOD =
{ DEBUG => :debug, INFO => :info, WARN => :warn, ERROR => :error, FATAL => :fatal, }
Instance Attribute Summary collapse
-
#actual_logger ⇒ Logger
readonly
A reference to the underlying logger that receives filtered log calls.
Instance Method Summary collapse
-
#<<(msg) ⇒ Object
Proxies to the encapsulated Logger object.
-
#add(severity, message = nil, progname = nil, &block) ⇒ Object
Add a log line, filtering the severity and message before calling through to the underlying logger’s #add method.
-
#close ⇒ Object
Proxies to the encapsulated Logger object.
-
#debug(message = nil, &block) ⇒ Object
Log a message, filtering the severity and/or message and dispatching to the corresponding severity-method of the underlying logger.
-
#debug? ⇒ Boolean
Proxies to the encapsulated Logger object.
-
#error(message = nil, &block) ⇒ Object
Log a message, filtering the severity and/or message and dispatching to the corresponding severity-method of the underlying logger.
-
#error? ⇒ Boolean
Proxies to the encapsulated Logger object.
-
#fatal(message = nil, &block) ⇒ Object
Log a message, filtering the severity and/or message and dispatching to the corresponding severity-method of the underlying logger.
-
#fatal? ⇒ Boolean
Proxies to the encapsulated Logger object.
-
#info(message = nil, &block) ⇒ Object
Log a message, filtering the severity and/or message and dispatching to the corresponding severity-method of the underlying logger.
-
#info? ⇒ Boolean
Proxies to the encapsulated Logger object.
-
#initialize(actual_logger) ⇒ FilterLogger
constructor
Initialize a new instance of this class.
-
#level ⇒ Object
Proxies to the encapsulated Logger object.
-
#level=(new_level) ⇒ Object
Proxies to the encapsulated Logger object.
- #method_missing(meth, *args) ⇒ Object
- #respond_to?(meth, *args) ⇒ Boolean
-
#warn(message = nil, &block) ⇒ Object
Log a message, filtering the severity and/or message and dispatching to the corresponding severity-method of the underlying logger.
-
#warn? ⇒ Boolean
Proxies to the encapsulated Logger object.
Constructor Details
#initialize(actual_logger) ⇒ FilterLogger
Initialize a new instance of this class.
Parameters
- actual_logger(Logger)
-
The actual, underlying Logger object
51 52 53 |
# File 'lib/right_support/log/filter_logger.rb', line 51 def initialize(actual_logger) @actual_logger = actual_logger end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object
55 56 57 58 |
# File 'lib/right_support/log/filter_logger.rb', line 55 def method_missing(meth, *args) return @actual_logger.__send__(meth, *args) if @actual_logger.respond_to?(meth) super end |
Instance Attribute Details
#actual_logger ⇒ Logger (readonly)
Returns a reference to the underlying logger that receives filtered log calls.
44 45 46 |
# File 'lib/right_support/log/filter_logger.rb', line 44 def actual_logger @actual_logger end |
Instance Method Details
#<<(msg) ⇒ Object
Proxies to the encapsulated Logger object. See Logger#<< for info.
157 158 159 |
# File 'lib/right_support/log/filter_logger.rb', line 157 def <<(msg) @actual_logger << msg end |
#add(severity, message = nil, progname = nil, &block) ⇒ Object
Add a log line, filtering the severity and message before calling through to the underlying logger’s #add method.
Parameters
- severity(Integer)
-
one of the Logger severity constants
- message(String)
-
the message to log, or nil
- progname(String)
-
the program name, or nil
Block
If message == nil and a block is given, yields to the block in order to capture the log message. This matches the behavior of Logger, but ensures the severity and message are still filtered.
Return
the result of the underlying logger’s #add
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/right_support/log/filter_logger.rb', line 139 def add(severity, = nil, progname = nil, &block) severity ||= UNKNOWN return true if severity < level if .nil? if block_given? = yield else = progname end end severity, = filter(severity, ()) return true if severity.nil? return @actual_logger.add(severity, ) if end |
#close ⇒ Object
Proxies to the encapsulated Logger object. See Logger#close for info.
162 163 164 |
# File 'lib/right_support/log/filter_logger.rb', line 162 def close @actual_logger.close end |
#debug(message = nil, &block) ⇒ Object
Log a message, filtering the severity and/or message and dispatching to the corresponding severity-method of the underlying logger.
See #info for more information.
68 69 70 71 72 73 74 |
# File 'lib/right_support/log/filter_logger.rb', line 68 def debug( = nil, &block) severity, = filter(DEBUG, ()) return true if severity.nil? meth = SEVERITY_TO_METHOD[severity] raise ArgumentError, "Filter emitted unknown severity #{severity.inspect}" unless meth @actual_logger.__send__(meth, , &block) end |
#debug? ⇒ Boolean
Proxies to the encapsulated Logger object. See Logger#debug? for info.
177 |
# File 'lib/right_support/log/filter_logger.rb', line 177 def debug?; @actual_logger.debug?; end |
#error(message = nil, &block) ⇒ Object
Log a message, filtering the severity and/or message and dispatching to the corresponding severity-method of the underlying logger.
See #info for more information.
104 105 106 107 108 109 110 |
# File 'lib/right_support/log/filter_logger.rb', line 104 def error( = nil, &block) severity, = filter(ERROR, ()) return if severity.nil? meth = SEVERITY_TO_METHOD[severity] raise ArgumentError, "Filter emitted unknown severity #{severity.inspect}" unless meth @actual_logger.__send__(meth, , &block) end |
#error? ⇒ Boolean
Proxies to the encapsulated Logger object. See Logger#error? for info.
186 |
# File 'lib/right_support/log/filter_logger.rb', line 186 def error?; @actual_logger.error?; end |
#fatal(message = nil, &block) ⇒ Object
Log a message, filtering the severity and/or message and dispatching to the corresponding severity-method of the underlying logger.
See #info for more information.
116 117 118 119 120 121 122 |
# File 'lib/right_support/log/filter_logger.rb', line 116 def fatal( = nil, &block) severity, = filter(FATAL, ()) return true if severity.nil? meth = SEVERITY_TO_METHOD[severity] raise ArgumentError, "Filter emitted unknown severity #{severity.inspect}" unless meth @actual_logger.__send__(meth, , &block) end |
#fatal? ⇒ Boolean
Proxies to the encapsulated Logger object. See Logger#fatal? for info.
189 |
# File 'lib/right_support/log/filter_logger.rb', line 189 def fatal?; @actual_logger.fatal?; end |
#info(message = nil, &block) ⇒ Object
Log a message, filtering the severity and/or message and dispatching to the corresponding severity-method of the underlying logger.
See #info for more information.
80 81 82 83 84 85 86 |
# File 'lib/right_support/log/filter_logger.rb', line 80 def info( = nil, &block) severity, = filter(INFO, ()) return true if severity.nil? meth = SEVERITY_TO_METHOD[severity] raise ArgumentError, "Filter emitted unknown severity #{severity.inspect}" unless meth @actual_logger.__send__(meth, , &block) end |
#info? ⇒ Boolean
Proxies to the encapsulated Logger object. See Logger#info? for info.
180 |
# File 'lib/right_support/log/filter_logger.rb', line 180 def info?; @actual_logger.info?; end |
#level ⇒ Object
Proxies to the encapsulated Logger object. See Logger#level for info.
167 168 169 |
# File 'lib/right_support/log/filter_logger.rb', line 167 def level @actual_logger.level end |
#level=(new_level) ⇒ Object
Proxies to the encapsulated Logger object. See Logger#level= for info.
172 173 174 |
# File 'lib/right_support/log/filter_logger.rb', line 172 def level=(new_level) @actual_logger.level = new_level end |
#respond_to?(meth, *args) ⇒ Boolean
60 61 62 |
# File 'lib/right_support/log/filter_logger.rb', line 60 def respond_to?(meth, *args) super(meth, *args) || @actual_logger.respond_to?(meth, *args) end |
#warn(message = nil, &block) ⇒ Object
Log a message, filtering the severity and/or message and dispatching to the corresponding severity-method of the underlying logger.
See #info for more information.
92 93 94 95 96 97 98 |
# File 'lib/right_support/log/filter_logger.rb', line 92 def warn( = nil, &block) severity, = filter(WARN, ()) return true if severity.nil? meth = SEVERITY_TO_METHOD[severity] raise ArgumentError, "Filter emitted unknown severity #{severity.inspect}" unless meth @actual_logger.__send__(meth, , &block) end |
#warn? ⇒ Boolean
Proxies to the encapsulated Logger object. See Logger#warn? for info.
183 |
# File 'lib/right_support/log/filter_logger.rb', line 183 def warn?; @actual_logger.warn?; end |