Class: Loggr::SLF4J::Logger
- Inherits:
-
Object
- Object
- Loggr::SLF4J::Logger
- Includes:
- Loggr::Severity
- Defined in:
- lib/loggr/slf4j/logger.rb
Overview
A logger which is backed by SLF4J, thus only useable in a JRuby environment.
Constant Summary
Constants included from Loggr::Severity
Loggr::Severity::DEBUG, Loggr::Severity::ERROR, Loggr::Severity::FATAL, Loggr::Severity::INFO, Loggr::Severity::TRACE, Loggr::Severity::UNKNOWN, Loggr::Severity::WARN
Instance Attribute Summary collapse
-
#auto_flushing ⇒ Object
readonly
Just to ensure compatiability with AS::BufferedLogger.
-
#close ⇒ Object
readonly
Just to ensure compatiability with AS::BufferedLogger.
-
#flush ⇒ Object
readonly
Just to ensure compatiability with AS::BufferedLogger.
-
#java_logger ⇒ Object
readonly
Access raw SLF4J logger & marker instances.
-
#java_marker ⇒ Object
readonly
Access raw SLF4J logger & marker instances.
-
#java_mdc ⇒ Object
readonly
Access raw SLF4J logger & marker instances.
-
#level ⇒ Object
Basically has no impact, because is handled by SLF4J.
Class Method Summary collapse
-
.in_java_notation(name) ⇒ Object
If a class, module or object is used converts ‘Foo::Bar::SomeThing` to java notation: `foo.bar.SomeThing`.
Instance Method Summary collapse
-
#initialize(name, options = {}) ⇒ Logger
constructor
Create a new Logger instance for the given name.
-
#mapped(hash = {}) ⇒ Object
A more describtive alternative to tagged is mapped, which just makes use of the MDC directly, basically.
-
#tagged(*new_tags) ⇒ Object
Uses the mapped diagnostic context to add tags, like ActiveSupport 3.2’s TaggedLogger.
Constructor Details
#initialize(name, options = {}) ⇒ Logger
Create a new Logger instance for the given name
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/loggr/slf4j/logger.rb', line 38 def initialize(name, = {}) name = self.class.in_java_notation(name) @java_logger = Java::OrgSlf4j::LoggerFactory.getLogger(name.to_s) @java_marker = MarkerFactory[[:marker]] @java_mdc = [:mdc] || Loggr::SLF4J::MDC # seriously, this is handled by slf4j and pretty dynamic @level = Logger::UNKNOWN @auto_flushing = true end |
Instance Attribute Details
#auto_flushing ⇒ Object (readonly)
Just to ensure compatiability with AS::BufferedLogger
31 32 33 |
# File 'lib/loggr/slf4j/logger.rb', line 31 def auto_flushing @auto_flushing end |
#close ⇒ Object (readonly)
Just to ensure compatiability with AS::BufferedLogger
31 32 33 |
# File 'lib/loggr/slf4j/logger.rb', line 31 def close @close end |
#flush ⇒ Object (readonly)
Just to ensure compatiability with AS::BufferedLogger
31 32 33 |
# File 'lib/loggr/slf4j/logger.rb', line 31 def flush @flush end |
#java_logger ⇒ Object (readonly)
Access raw SLF4J logger & marker instances
34 35 36 |
# File 'lib/loggr/slf4j/logger.rb', line 34 def java_logger @java_logger end |
#java_marker ⇒ Object (readonly)
Access raw SLF4J logger & marker instances
34 35 36 |
# File 'lib/loggr/slf4j/logger.rb', line 34 def java_marker @java_marker end |
#java_mdc ⇒ Object (readonly)
Access raw SLF4J logger & marker instances
34 35 36 |
# File 'lib/loggr/slf4j/logger.rb', line 34 def java_mdc @java_mdc end |
#level ⇒ Object
Basically has no impact, because is handled by SLF4J
28 29 30 |
# File 'lib/loggr/slf4j/logger.rb', line 28 def level @level end |
Class Method Details
.in_java_notation(name) ⇒ Object
If a class, module or object is used converts ‘Foo::Bar::SomeThing` to java notation: `foo.bar.SomeThing`. Symbols and Strings are left as is!
93 94 95 96 97 98 99 |
# File 'lib/loggr/slf4j/logger.rb', line 93 def self.in_java_notation(name) return name.to_s if name.respond_to?(:to_str) || name.is_a?(Symbol) name = name.is_a?(Module) ? name.name.to_s : name.class.name.to_s parts = name.split('::') last = parts.pop parts.map { |p| p.downcase }.push(last).join('.') end |
Instance Method Details
#mapped(hash = {}) ⇒ Object
A more describtive alternative to tagged is mapped, which just makes use of the MDC directly, basically.
82 83 84 85 86 87 88 |
# File 'lib/loggr/slf4j/logger.rb', line 82 def mapped(hash = {}) old_keys = hash.keys.inject({}) { |hsh,k| hsh[k] = java_mdc[k]; hsh } hash.each { |key, value| java_mdc[key] = value } yield ensure old_keys.each { |key, value| java_mdc[key] = value } end |
#tagged(*new_tags) ⇒ Object
Uses the mapped diagnostic context to add tags, like ActiveSupport 3.2’s TaggedLogger.
72 73 74 75 76 77 78 |
# File 'lib/loggr/slf4j/logger.rb', line 72 def tagged(*) = java_mdc[:tags].to_s java_mdc[:tags] = (.split(', ') + .flatten).join(', ') yield ensure java_mdc[:tags] = .length == 0 ? nil : end |