Class: Log4jruby::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/log4jruby/logger.rb

Overview

Author

Lenny Marks

Wrapper around org.apache.log4j.Logger with interface similar to standard ruby Logger.

  • Ruby and Java exceptions are logged with backtraces.

  • fileName, lineNumber, methodName available to appender layouts via MDC variables(e.g. %XlineNumber)

Constant Summary collapse

BLANK_CALLER =

:nodoc:

['', '', '']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#tracingObject

turn tracing on to make fileName, lineNumber, and methodName available to appender layout through MDC(ie. %XfileName %XlineNumber %XmethodName)



16
17
18
# File 'lib/log4jruby/logger.rb', line 16

def tracing
  @tracing
end

Class Method Details

.[](name) ⇒ Object

get Logger for name



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/log4jruby/logger.rb', line 24

def[](name)
  name = name.nil? ? 'jruby' : "jruby.#{name.gsub('::', '.')}"
 
  log4j = Java::org.apache.log4j.Logger.getLogger(name)
  log4jruby = logger_mapping[log4j]
  
  unless log4jruby
    log4jruby = new(log4j)
  end
  
  log4jruby
end

.get(name, values = {}) ⇒ Object

same as [] but acceptions attributes



38
39
40
41
42
# File 'lib/log4jruby/logger.rb', line 38

def get(name, values = {})
  logger = self[name]
  logger.attributes = values
  logger
end

.logger_mappingObject



19
20
21
# File 'lib/log4jruby/logger.rb', line 19

def logger_mapping
  @logger_mapping ||= {}
end

.rootObject

Return root Logger(i.e. jruby)



45
46
47
48
49
50
51
52
53
# File 'lib/log4jruby/logger.rb', line 45

def root
  log4j = Java::org.apache.log4j.Logger.getLogger('jruby')
  
  log4jruby = logger_mapping[log4j]
  unless log4jruby
    log4jruby = new(log4j)
  end
  log4jruby
end

Instance Method Details

#attributes=(values) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/log4jruby/logger.rb', line 56

def attributes=(values)
  if values
    values.each_pair do |k, v|
      setter = "#{k}="
      send(setter, v) if respond_to?(setter)
    end
  end
end

#debug(object = nil, &block) ⇒ Object



89
90
91
92
93
# File 'lib/log4jruby/logger.rb', line 89

def debug(object = nil, &block)
  if debug?
    send_to_log4j(:debug, object, nil, &block)
  end
end

#debug?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/log4jruby/logger.rb', line 128

def debug?
  @logger.isEnabledFor(Java::org.apache.log4j.Priority::DEBUG)
end

#error(object = nil, &block) ⇒ Object



107
108
109
# File 'lib/log4jruby/logger.rb', line 107

def error(object = nil, &block)
  send_to_log4j(:error, object, nil, &block)
end

#fatal(object = nil, &block) ⇒ Object



115
116
117
# File 'lib/log4jruby/logger.rb', line 115

def fatal(object = nil, &block)
  send_to_log4j(:fatal, object, nil, &block)
end

#flushObject



85
86
87
# File 'lib/log4jruby/logger.rb', line 85

def flush
  #rails compatability
end

#info(object = nil, &block) ⇒ Object



95
96
97
98
99
# File 'lib/log4jruby/logger.rb', line 95

def info(object = nil, &block)
  if info?
    send_to_log4j(:info, object, nil, &block)
  end
end

#info?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/log4jruby/logger.rb', line 132

def info?
  @logger.isEnabledFor(Java::org.apache.log4j.Priority::INFO)
end

#levelObject



81
82
83
# File 'lib/log4jruby/logger.rb', line 81

def level
  @logger.level
end

#level=(level) ⇒ Object

Shortcut for setting log levels. (:debug, :info, :warn, :error)



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/log4jruby/logger.rb', line 66

def level=(level)
  @logger.level = case level
  when :debug
    then Java::org.apache.log4j.Level::DEBUG
  when :info
    then Java::org.apache.log4j.Level::INFO
  when :warn
    then Java::org.apache.log4j.Level::WARN
  when :error
    then Java::org.apache.log4j.Level::ERROR
  else
    raise NotImplementedError
  end
end

#log4j_loggerObject

return org.apache.log4j.Logger instance backing this Logger



124
125
126
# File 'lib/log4jruby/logger.rb', line 124

def log4j_logger
  @logger
end

#log_error(msg, error) ⇒ Object



111
112
113
# File 'lib/log4jruby/logger.rb', line 111

def log_error(msg, error)
  send_to_log4j(:error, msg, error)
end

#log_fatal(msg, error) ⇒ Object



119
120
121
# File 'lib/log4jruby/logger.rb', line 119

def log_fatal(msg, error)
  send_to_log4j(:fatal, msg, error)
end

#parentObject



152
153
154
# File 'lib/log4jruby/logger.rb', line 152

def parent
  logger_mapping[log4j_logger.parent] || Logger.root
end

#tracing?Boolean

Returns:

  • (Boolean)


140
141
142
143
144
145
146
147
148
149
150
# File 'lib/log4jruby/logger.rb', line 140

def tracing?
  if tracing.nil?
    if parent == Logger.root
      Logger.root.tracing == true
    else 
     parent.tracing?
    end
  else
    tracing == true
  end
end

#warn(object = nil, &block) ⇒ Object



101
102
103
104
105
# File 'lib/log4jruby/logger.rb', line 101

def warn(object = nil, &block)
  if warn?
    send_to_log4j(:warn, object, nil, &block)
  end
end

#warn?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/log4jruby/logger.rb', line 136

def warn?
  @logger.isEnabledFor(Java::org.apache.log4j.Priority::WARN)
end