Log4j.xml
log4j.properties
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="log_file" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="c:/ntsky_exception.log" />
<param name="append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
</layout>
</appender>
<appender name="log_console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</layout>
</appender>
<!--实现按自定义包输出日志-->
<logger name="com.my.bbs">
<level value="INFO"/>
<appender-ref ref="log_file"/>
</logger>
<logger name="com.my.package">
<level value="DEBUG"/>
<appender-ref ref="log_console"/>
</logger>
</log4j:configuration>
log4j.properties
log4j.logger.com.my.bbs=debug,appender1
log4j.logger.com.my.package=info,appender2
#输出到控制台
log4j.appender.appender1=org.apache.log4j.ConsoleAppender
#设置输出样式
log4j.appender.appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.appender1.layout.ConversionPattern=[%d{yy/MM/dd HH:mm:ss:SSS}][%C-%M] %m%n
#输出到文件(这里默认为追加方式)
log4j.appender.appender2=org.apache.log4j.FileAppender
#设置文件输出路径
#【1】文本文件
log4j.appender.appender2.File=c:/Log4JDemo07_Dao.log
#设置文件输出样式
log4j.appender.appender2.layout=org.apache.log4j.PatternLayout
log4j.appender.appender2.layout.ConversionPattern=[%d{HH:mm:ss:SSS}][%C-%M] -%m%n