Class: Commons::Logging::LogFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/commons/logging/log_factory.rb

Overview

Abstract base log factory class.

Example:

require 'commons/ruby/log4r'

module Example
  class Sample
    LOG = Log4r::Logger.get_logger(self.name)
    #@@log = Log4r::Logger.get_logger(self.name)    # for Ruby 1.9 or later.
    # ...
    if LOG.debug?
      LOG.debug('This is DEBUG log.')
    end
    LOG.info('This is INFO log.')
    # ...
  end
end

Direct Known Subclasses

Impl::LogFactoryImpl

Constant Summary collapse

FACTORY_PROPERTY =

Commons.Logging.LogFactory property name.

'commons.logging.LogFactory'
FACTORY_DEFAULT =

Default implementation class name of Commons.Logging.LogFactory.

'Commons::Logging::Impl::LogFactoryImpl'
FACTORY_PROPERTIES =

Default properties file (relative path on $LOAD_PATH)

'commons-logging.properties'
@@factories =

Cached factories.

Hash.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogFactory

Returns a new instance of LogFactory.



68
69
70
# File 'lib/commons/logging/log_factory.rb', line 68

def initialize
  # do nothing.
end

Class Method Details

.cache_factory(factory) ⇒ Object

Caches log factory instance.



148
149
150
151
152
# File 'lib/commons/logging/log_factory.rb', line 148

def self.cache_factory(factory)
  unless factory == nil
    @@factories['defaultKey'] = factory
  end
end

.get_cached_factoryObject

Obtains cached factory.



135
136
137
138
139
140
141
# File 'lib/commons/logging/log_factory.rb', line 135

def self.get_cached_factory
  if @@factories.has_key?('defaultKey')
    return @@factories['defaultKey']
  else
    return nil
  end
end

.get_factoryObject

Gets Commons.Logging.LogFactory instance.

throws Commons::Logging::LogConfigurationError if the implementation class is not available or cannot be instantiated.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/commons/logging/log_factory.rb', line 81

def self.get_factory
  factory = nil
  
  factory = get_cached_factory
  unless factory == nil
    return factory
  end
  
  props = nil
  props_path = Commons::Lang::ClassLoader.get_resource(FACTORY_PROPERTIES)
  File.open(props_path, 'r') {|file|
    props = Commons::Util::Properties.new
    props.load(file)
  }
  
  # 1st, try the system property.
  factory_class = ENV[FACTORY_PROPERTY]
  unless factory_class == nil
    factory = new_factory(factory_class)
  end
  
  # 2nd try a properties file.
  if factory == nil && props != nil
    factory_class = props.get_property(FACTORY_PROPERTY)
    unless factory_class == nil
      factory = new_factory(factory_class)
    end
  end
  
  # 3rd, try the fallback implementation class
  if factory == nil
    factory = new_factory(FACTORY_DEFAULT)
  end
  
  # cache the factory
  if factory != nil
    cache_factory(factory)
    if props != nil
      props.property_names.each {|name|
        factory.set_attribute(name, props.get_property(name))
      }
    end
  end
  
  return factory
end

.get_log(name) ⇒ Object



129
130
131
# File 'lib/commons/logging/log_factory.rb', line 129

def self.get_log(name)
  return get_factory.get_instance(name)
end

.new_factory(factory_class) ⇒ Object

Creates and returns new Commons_Logging_LogFactory instance.

throws Commons::Logging::LogConfigurationError



161
162
163
164
165
166
167
168
169
170
# File 'lib/commons/logging/log_factory.rb', line 161

def self.new_factory(factory_class)
  begin
    Commons::Lang::ClassLoader::load(factory_class)
    return eval(factory_class).new
  rescue LoadError => le
    raise LogConfigurationError, le.message
  rescue Exception => e
    raise LogConfigurationError, e.message
  end
end