Class: Loggr::SLF4J::Logger

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  name = self.class.in_java_notation(name)
  @java_logger = Java::OrgSlf4j::LoggerFactory.getLogger(name.to_s)
  @java_marker = MarkerFactory[options[:marker]]
  @java_mdc = options[:mdc] || Loggr::SLF4J::MDC

  # seriously, this is handled by slf4j and pretty dynamic
  @level = Logger::UNKNOWN
  @auto_flushing = true
end

Instance Attribute Details

#auto_flushingObject (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

#closeObject (readonly)

Just to ensure compatiability with AS::BufferedLogger



31
32
33
# File 'lib/loggr/slf4j/logger.rb', line 31

def close
  @close
end

#flushObject (readonly)

Just to ensure compatiability with AS::BufferedLogger



31
32
33
# File 'lib/loggr/slf4j/logger.rb', line 31

def flush
  @flush
end

#java_loggerObject (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_markerObject (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_mdcObject (readonly)

Access raw SLF4J logger & marker instances



34
35
36
# File 'lib/loggr/slf4j/logger.rb', line 34

def java_mdc
  @java_mdc
end

#levelObject

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(*new_tags)
  old_tags = java_mdc[:tags].to_s
  java_mdc[:tags] = (old_tags.split(', ') + new_tags.flatten).join(', ')
  yield
ensure
  java_mdc[:tags] = old_tags.length == 0 ? nil : old_tags
end