Class: Log4r::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/log4r/logger.rb,
lib/log4r/repository.rb,
lib/log4r/staticlogger.rb,
lib/log4r/loggerfactory.rb

Overview

See log4r/logger.rb

Direct Known Subclasses

LogServer, RootLogger

Defined Under Namespace

Classes: LoggerFactory, Repository

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_fullname, _level = nil, _additive = true, _trace = false) ⇒ Logger

Logger requires a name. The last 3 parameters are:

level

Do I have a level? (Otherwise, I’ll inherit my parent’s)

additive

Am I additive?

trace

Do I record the execution trace? (slows things a wee bit)

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/log4r/logger.rb', line 26

def initialize(_fullname, _level=nil, _additive=true, _trace=false)
  # validation
  raise ArgumentError, "Logger must have a name", caller if _fullname.nil?
  Log4rTools.validate_level(_level) unless _level.nil?
  validate_name(_fullname)
  
  # create the logger
  @fullname = _fullname
  @outputters = []
  @additive = _additive
  deal_with_inheritance(_level)
  LoggerFactory.define_methods(self)
  self.trace = _trace
  Repository[@fullname] = self
end

Instance Attribute Details

#additiveObject

Returns the value of attribute additive.



18
19
20
# File 'lib/log4r/logger.rb', line 18

def additive
  @additive
end

#fullnameObject (readonly)

Returns the value of attribute fullname.



17
18
19
# File 'lib/log4r/logger.rb', line 17

def fullname
  @fullname
end

#levelObject

Returns the value of attribute level.



17
18
19
# File 'lib/log4r/logger.rb', line 17

def level
  @level
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/log4r/logger.rb', line 17

def name
  @name
end

#outputtersObject

Returns the value of attribute outputters.



18
19
20
# File 'lib/log4r/logger.rb', line 18

def outputters
  @outputters
end

#parentObject

Returns the value of attribute parent.



17
18
19
# File 'lib/log4r/logger.rb', line 17

def parent
  @parent
end

#pathObject (readonly)

Returns the value of attribute path.



17
18
19
# File 'lib/log4r/logger.rb', line 17

def path
  @path
end

#traceObject

Returns the value of attribute trace.



18
19
20
# File 'lib/log4r/logger.rb', line 18

def trace
  @trace
end

Class Method Details

.[](_fullname) ⇒ Object

Get a logger with a fullname from the repository or nil if logger wasn’t found.



12
13
14
15
16
# File 'lib/log4r/staticlogger.rb', line 12

def self.[](_fullname)
  # forces creation of RootLogger if it doesn't exist yet.
  return RootLogger.instance if _fullname=='root' or _fullname=='global'
  Repository[_fullname]
end

.eachObject

Yields fullname and logger for every logger in the system.



29
30
31
32
33
# File 'lib/log4r/staticlogger.rb', line 29

def self.each
  for fullname, logger in Repository.instance.loggers
    yield fullname, logger
  end
end

.each_loggerObject



35
36
37
# File 'lib/log4r/staticlogger.rb', line 35

def self.each_logger
  Repository.instance.loggers.each_value {|logger| yield logger}
end

.get(_fullname) ⇒ Object

Like Logger[] except that it raises NameError if Logger wasn’t found.



20
21
22
23
24
25
26
# File 'lib/log4r/staticlogger.rb', line 20

def self.get(_fullname)
  logger = self[_fullname]
  if logger.nil?
    raise NameError, "Logger '#{_fullname}' not found.", caller
  end
  logger
end

.globalObject

Returns the root logger. Identical to Logger.root



7
# File 'lib/log4r/staticlogger.rb', line 7

def self.global; return root end

.log_internal(level = 1) ⇒ Object

Internal logging for Log4r components. Accepts only blocks. To see such log events, create a logger named ‘log4r’ and give it an outputter.



43
44
45
46
47
# File 'lib/log4r/staticlogger.rb', line 43

def self.log_internal(level=1)
  internal = Logger['log4r']
  return if internal.nil?
  internal.send(LNAMES[level].downcase, yield)
end

.rootObject

Returns the root logger. Identical to Logger.global



5
# File 'lib/log4r/staticlogger.rb', line 5

def self.root; return RootLogger.instance end

Instance Method Details

#==(other) ⇒ Object



145
146
147
# File 'lib/log4r/logger.rb', line 145

def ==(other)
  return true if self.object_id == other.object_id
end

#add(*_outputters) ⇒ Object

Add outputters by name or by reference. Can be done any time.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/log4r/logger.rb', line 119

def add(*_outputters)
  for thing in _outputters
    o = (thing.kind_of?(Outputter) ? thing : Outputter[thing])
    # some basic validation
    if not o.kind_of?(Outputter)
      raise TypeError, "Expected kind of Outputter, got #{o.class}", caller
    elsif o.nil?
      raise TypeError, "Couldn't find Outputter '#{thing}'", caller
    end
    @outputters.push o
    Logger.log_internal {"Added outputter '#{o.name}' to '#{@fullname}'"}
  end
  @outputters
end

#formatterObject

fix for rails 4+ <tk>



151
152
153
# File 'lib/log4r/logger.rb', line 151

def formatter
  nil
end

#is_root?Boolean

Returns:

  • (Boolean)


143
# File 'lib/log4r/logger.rb', line 143

def is_root?; false end

#levelsObject

Return array of defined levels.



82
83
84
# File 'lib/log4r/logger.rb', line 82

def levels
  LNAMES
end

#remove(*_outputters) ⇒ Object

Remove outputters from this logger by name only. Can be done any time.



135
136
137
138
139
140
141
# File 'lib/log4r/logger.rb', line 135

def remove(*_outputters)
  for name in _outputters
    o = Outputter[name]
    @outputters.delete o
    Logger.log_internal {"Removed outputter '#{o.name}' from '#{@fullname}'"}
  end
end