CHARMS1
asked on
Log4J question
Can you have more than one Appender in a config file. And do i set a specific appender to a specific class in a config file?
Thanks,
Mark.
Thanks,
Mark.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can even have a class to use two (or more) appenders.
You can certainly have more than one appender. I would recommend using the DomConfigurator to config log4j with an XML instead.
Example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="cdms" class="org.apache.log4j.Da ilyRolling FileAppend er">
<param name="File" value="/shoapp/clms/logs/c dms.std.lo g"/>
<layout class="org.apache.log4j.Pa tternLayou t">
<param name="ConversionPattern" value="%d{yyyy.MM.dd HH:mm:ss.SSS} %-5p [%t]: %m%n"/>
</layout>
</appender>
<appender name="cdms.xml" class="org.apache.log4j.Da ilyRolling FileAppend er">
<param name="File" value="/shoapp/clms/logs/c dms.log.xm l"/>
<layout class="org.apache.log4j.xm l.XMLLayou t"/>
</appender>
<appender name="stdout" class="org.apache.log4j.Co nsoleAppen der">
<layout class="org.apache.log4j.Pa tternLayou t">
<param name="ConversionPattern" value="%d{yyyy.MM.dd HH:mm:ss.SSS} %-5p [%t]: %m%n"/>
</layout>
</appender>
<logger name="com.cdms.logger">
<level value="debug"/>
<appender-ref ref="cdms"/>
<appender-ref ref="cdms.xml"/>
</logger>
<root>
<level value="info"/>
<appender-ref ref="stdout"/>
</root>
</log4j:configuration>
Java Code . . .
class level:
private static Logger logger = null;
methods in class:
public void createLogger() throws Exception {
##Put your path to the XML in here
DOMConfigurator.configure( "/shoapp/c lms/cdmslo g4j.xml");
setLogger(Logger.getLogger ("CmAdminT ools.logge r"));
}
/**
* @param logger
*/
public static void setLogger(Logger loggerObj) {
logger = loggerObj;
}
Example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="cdms" class="org.apache.log4j.Da
<param name="File" value="/shoapp/clms/logs/c
<layout class="org.apache.log4j.Pa
<param name="ConversionPattern" value="%d{yyyy.MM.dd HH:mm:ss.SSS} %-5p [%t]: %m%n"/>
</layout>
</appender>
<appender name="cdms.xml" class="org.apache.log4j.Da
<param name="File" value="/shoapp/clms/logs/c
<layout class="org.apache.log4j.xm
</appender>
<appender name="stdout" class="org.apache.log4j.Co
<layout class="org.apache.log4j.Pa
<param name="ConversionPattern" value="%d{yyyy.MM.dd HH:mm:ss.SSS} %-5p [%t]: %m%n"/>
</layout>
</appender>
<logger name="com.cdms.logger">
<level value="debug"/>
<appender-ref ref="cdms"/>
<appender-ref ref="cdms.xml"/>
</logger>
<root>
<level value="info"/>
<appender-ref ref="stdout"/>
</root>
</log4j:configuration>
Java Code . . .
class level:
private static Logger logger = null;
methods in class:
public void createLogger() throws Exception {
##Put your path to the XML in here
DOMConfigurator.configure(
setLogger(Logger.getLogger
}
/**
* @param logger
*/
public static void setLogger(Logger loggerObj) {
logger = loggerObj;
}
> Can you have more than one Appender in a config file. And do i set a specific appender to a specific class in a config file?
You can have as many appenders youi you want and they would be specified in your log4j.xml.
You should attach your appenders to loggers, not to a specific class, what you assign to what is up to you.
You can have as many appenders youi you want and they would be specified in your log4j.xml.
You should attach your appenders to loggers, not to a specific class, what you assign to what is up to you.
You can use how many appender you want. One appender is file appender ( write log to file), one appender is SMTP appender (send log to an email), one appender to console (output log to console),....
Even file appender you can have many file appender such as DailyRolingFileAppender, RollingFileAppender
Appender is associate with logger which have a name. If you want your class asscociate with that appender you just set the logger of that class have a logger with the name you set for appender.
Phuoc H.Nguyen
Even file appender you can have many file appender such as DailyRolingFileAppender, RollingFileAppender
Appender is associate with logger which have a name. If you want your class asscociate with that appender you just set the logger of that class have a logger with the name you set for appender.
Phuoc H.Nguyen
:-)