-
I understand that an appender in log4j is another channel for which it will log a statement, however I cannot seem to get them to work.
This is the bulk of my log4j.xml file:
<!-- ================================= -->
<!-- Preserve messages in a local file -->
<!-- ================================= -->
<!-- A time/date based rolling appender -->
<!-- Rollover at midnight each day -->
<!-- Rollover at the top of each hour
<param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
-->
<!-- The default pattern: Date Priority [Category Message\n -->
<!-- The full pattern: Date MS Priority [Category](Thread:NDC) Message\n
<param name="ConversionPattern" value="%d %-5r %-5p [%c](%t:%x) %m%n"/>
-->
<!-- A time/date based rolling appender for BPM Custom Agents -->
<!-- Rollover at midnight each day -->
<!-- Rollover at the top of each hour
<param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
-->
<!-- The full pattern: Date MS Priority [Category] (Thread:NDC) Message\n
<param name="ConversionPattern" value="%d %-5r %-5p [%c] (%t:%x) %m%n"/>
-->
There are 2 appenders defined, one is FILE, which writes to server.log, and the other is CUSTOM_AGENTS, which writes to custom_agents.log.
The problem is, I cannot seem to get anything to write to the second appender.
For instance, in my class ConfigManager, in package org.test:
private static Logger log = Logger.getLogger(ConfigManager.class);
log.error("Test")
This will write to server.log:
ERROR [org.test.ConfigManager] Test
I want to do the same thing, however to custom_agents.log insted. If I try what I've read on the internet, which is:
private static Logger log = Logger.getLogger("CUSTOM_AGENTS");
log.error("Test")
This instead writes to server log:
ERROR [CUSTOM_AGENTS] Test
What code do I need to write the following line to custom_agents.log? :
ERROR [org.test.ConfigManager] Test
I hope I have put this in a clear format
Many thanks, any help would be greatly appreciated
-
add this lines to log4j.xml
and then use this lines for logging:
private static Logger log = Logger.getLogger(ConfigManager.class);
log.error("Test")
log will be appeared in both appenders
Regards.
-
I will give that a go, many thanks for your post.
-
Using the below log4j.xml as an example you would just define a new logger which will only apply to the 'named' logger you were referring to in your code (the root logger is the default logger - everything will use it if it cannot find the named logger - or a parent of it - for example if you refer to logger named com.myclass it will look for loggers defined in your log4j.xml (or log4j.properties file) in this order:
com.myclass
com
root
So in my log4j.xml file everywhere I have 'UseThisName' for your example you would change it to 'CUSTOM_AGENTS'. In my log4j.xml file the logger 'UseThisName' is using the appender TOFILE (because of the line ). I believe it will also use the ROOT loggers appender as well (unless you set the setting telling it not to).
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<!-- ******************************************************************************* -->
<!-- * This xml file requires that the log4j.dtd file be co-located with it -->
<!-- ******************************************************************************* -->
<!-- ******************************************************************************* -->
<!-- * APPENDER DEFINITIONS - BEGIN * -->
<!-- ******************************************************************************* -->
<!-- * Note: you must have an appender (logging output destination) * -->
<!-- * and a log4j.logger defined for each logger or you will * -->
<!-- * get the following warnings when starting log4j: * -->
<!-- * log4j:WARN No appenders could be found for logger (UseThisName). * -->
<!-- * log4j:WARN Please initialize the log4j system properly. * -->
<!-- ******************************************************************************* -->
<!-- * STDOUT Appender * -->
<!-- * Must name it and define the class to reference * -->
<!-- * As well need to defined the pattern layout which then requires * -->
<!-- * a conversion pattern which follows the c languages printf format * -->
<!-- * %r = number of milliseconds elapsed since program started * -->
<!-- * %d = date/time stamp in yyyy-MM-dd HH:mm:ss,mmm format * -->
<!-- * %p = log4j priority (FATAL, ERROR ...) * -->
<!-- * %t = name of thread making log request * -->
<!-- * %c = name of the logger (usually the class name) * -->
<!-- * %m = the log message * -->
<!-- * \n = end of line * -->
<!-- ******************************************************************************* -->
<!-- ******************************************************************************* -->
<!-- * TOFILE Appender * -->
<!-- * Must name it and define the class to reference * -->
<!-- * As well need to defined the pattern layout which then requires * -->
<!-- * a conversion pattern which follows the c languages printf format * -->
<!-- ******************************************************************************* -->
<!--param name="ConversionPattern" value="%d %p [%t] %c{1} - %m\n"/-->
<!-- ******************************************************************************* -->
<!-- * APPENDER DEFINITIONS - END * -->
<!-- ******************************************************************************* -->
<!-- * * -->
<!-- ******************************************************************************* -->
<!-- * LOGGER DEFINITIONS - BEGIN * -->
<!-- ******************************************************************************* -->
<!-- ******************************************************************************* -->
<!-- * LOGGER DEFINITIONS - END * -->
<!-- ******************************************************************************* -->
<!-- * * -->
<!-- ******************************************************************************* -->
<!-- * ROOT LOGGER DEFINITION - BEGIN * -->
<!-- ******************************************************************************* -->
<!-- ******************************************************************************* -->
<!-- * Define the root loggers priority and appender (good practice) * -->
<!-- * priority = DEBUG * -->
<!-- * appender = STDOUT * -->
<!-- ******************************************************************************* -->
<!-- ******************************************************************************* -->
<!-- * ROOT LOGGER DEFINITION - END * -->
<!-- ******************************************************************************* -->