org.apache.log4j
. At log4j
initialization you create an
instance of
AppServerCategoryFactory
populated with the attributes you desire. You can either supply these
in the factory's constructor
or set them afterwards. The host name is determined automatically
using the java.net
API and can be reset manually. It
is ok to set any of the attributes to null.
After creating an appropriate AppServerCategoryFactory
instance, set a static reference to it using the
AppServerCategory.setFactory
method. This
sets everything up for acquiring an
AppServerCategory
reference whenever you need one.
import org.apache.log4j.spi.CategoryFactory; import org.apache.log4j.Category; import org.apache.log4j.examples.appserver.AppServerCategoryFactory; import org.apache.log4j.examples.appserver.AppServerCategory; ... org.apache.log4j.BasicConfigurator.configure(); CategoryFactory factory = new AppServerCategoryFactory("MyServer", null, null); AppServerCategory.setFactory( factory ); ... Category cat = AppServerCategory.getInstance("my.category"); cat.info("Entry"); |
AppServerCategory
with
PropertyConfigurator
or
DOMConfigurator
requires a note of caution. By default these configurators
do not know that
Category
has been subclassed. Upon
constructing a configuration from a set of properties, it will
construct Category
instances rather than instances
of the desired subclass. There at least two ways to work around
this.
By instanciating any anticipated AppServerCategory
instances before calling a configurator, the configurator will
use the instances you create rather than trying to (incorrectly)
create its own. The consequence is that you can
not always predict which categories will be created. Their
dynamic nature is the reason for externalizing their configuration
in the first place. This drawback is limited to category names
explicitly defined in the configuration.
Another work-around is to take advantage of a recently added feature
allowing you to specify the type of factory used for creating
Category
instances to a DOMConfigurator
or a PropertyConfigurator
. The consequence
is that only the default constructor is used to create
the factory instance. For AppServerCategoryFactory
,
this will only populate the hostname attribute. The server name,
component name and version name will be blank. For many cases
this may be satisfactory.