Property | Description |
---|---|
log4j.categoryFactory
| The {@link org.apache.log4j.spi.CategoryFactory} implementation to use. |
log4j.factory.server
| The value assigned to the server attribute. |
log4j.factory.component
| The value assigned to the component attribute. |
log4j.factory.version
| The value assigned to the version attribute. |
log4j.factory.messageBundle
| The name of bundle file to populate the message
ResourceBundle . Because the
ResourceBundle.getBundle method is used to load
the message file, the ".properties" extension
should not be included as part of this property.
|
A sample configuration might look like this.
log4j.categoryFactory=org.apache.log4j.examples.appserver.AppServerCategoryFactory log4j.factory.server=TestServer log4j.factory.component=TestComponent log4j.factory.version=SomeVersion log4j.factory.messageBundle=app_messages log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.examples.appserver.AppServerPatternLayout log4j.appender.stdout.layout.ConversionPattern=[%h:%s:%b:%v] %m%n |
The only change needed to your source file is to invoke the {@link
org.apache.log4j.examples.appserver.AppServerCategory#getInstance(String)}
instead of the usual {@link org.apache.log4j.Category#getInstance(String)}
to acquire an implementation of Category
.
import org.apache.log4j.Category; import org.apache.log4j.PropertyConfigurator; import org.apache.log4j.examples.appserver.AppServerCategory; ... PropertyConfigurator.configure("test.properties"); Category cat = AppServerCategory.getInstance("some.cat"); ... cat.info("This is an INFO statement."); |
PropertyConfigurator
out of it all
together. This could be useful if you want only some of your
categories to be AppServerCategory
instances.
Simply create an AppServerCategoryFactory
instance.
The constructor automatically installs the new instance as the
default factory for future calls to {@link
org.apache.log4j.examples.appserver.AppServerCategory#getInstance(String)}.
import org.apache.log4j.Category; import org.apache.log4j.examples.appserver.AppServerCategoryFactory; import org.apache.log4j.examples.appserver.AppServerCategory; ... org.apache.log4j.BasicConfigurator.configure(); new AppServerCategoryFactory("MyServer", null, null); ... Category cat = AppServerCategory.getInstance("my.category"); cat.info("Entry"); |
AppServerCategory
with
PropertyConfigurator
or DOMConfigurator
requires a note of caution. Since the very manual method
does not provide the log4j.categoryFactory
property,
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. One way to around this is decribed
below.
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.
SimpleSocketServer
used in
conjunction with the SocketAppender
.
The configuration of SimpleSocketServer
receiving
serialized AppServerLoggingEvent
classes is much the
same as with the standard LoggingEvent
class. The main
difference to keep in mind is that the layout implementation should be
org.apache.log4j.examples.appserver.AppServerPatternLayout
.
This will allow for the interpretation of %h, %s, %b and %v symbols.