Log 4 J構成例(xml形式)
完全なxmlプロファイル(propertitiesファイルは機能が不十分で、フィルタを使用できません.個人的にはxmlフォーマットがはっきりしていて、読みやすいと思います).
テストプログラムを添付します.
別のプロファイル:
テストプログラムを添付します.
import java.net.URL;
import org.apache.log4j.xml.DOMConfigurator;
/**
* log4j , log4j
*
*/
public class Log4jXMLConfig
{
private static Log4jXMLConfig instance;
public static synchronized Log4jXMLConfig initia()
{
if (instance == null)
{
return new Log4jXMLConfig();
}
return instance;
}
private Log4jXMLConfig()
{
final URL url = Log4jXMLConfig.class.getResource("log4j.xml");
DOMConfigurator.configure(url);
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d - %c -%-4r [%t] %-5p %x - %m%n" />
</layout>
<!-- -->
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value="ERROR"/>
<param name="LevelMin" value="TRACE"/>
</filter>
</appender>
<appender name="FILE" class="org.apache.log4j.FileAppender">
<param name="File" value="C:/log4j1.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d - %c -%-4r [%t] %-5p %x - %m%n" />
</layout>
</appender>
<appender name="DATABASE" class="org.apache.log4j.jdbc.JDBCAppender">
<param name="URL" value="jdbc:oracle:thin:@192.168.0.59:1521:oanet"/>
<param name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<param name="user" value="hdczoa"/>
<param name="password" value="system"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="INSERT INTO hdczoa.LOG4J(stamp,thread, info_level,class,message) VALUES ('%d', '%t', '%p', '%c', %m)" />
</layout>
</appender>
<!-- ( ERROR !) -->
<appender name="MAIL"
class="org.apache.log4j.net.SMTPAppender">
<param name="threshold" value="debug" />
<!--
<param name="threshold" value="fatal"/>
-->
<!-- , 512K Email -->
<param name="BufferSize" value="512" /><!-- K -->
<param name="From" value="[email protected]" />
<param name="SMTPHost" value="smtp.163.com" />
<param name="Subject" value="juyee-log4jMessage" />
<param name="To" value="[email protected]" />
<param name="SMTPUsername" value="test" />
<param name="SMTPPassword" value="test" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%-d{yyyy-MM-dd HH:mm:ss.SSS} [%p]-[%c] %m%n" />
</layout>
</appender>
<appender name="ASYNC" class="org.apache.log4j.AsyncAppender">
<param name="BufferSize" value="256" />
<appender-ref ref="DATABASE" />
</appender>
<!-- <logger></logger> -->
<logger name="com.litt2.log4j" additivity="false">
<level value="WARN" />
<appender-ref ref="CONSOLE" />
</logger>
<!-- <category></category> -->
<category name="com.litt3">
<level value="DEBUG" />
<appender-ref ref="CONSOLE" />
<appender-ref ref="MAIL" />
</category>
<root>
<priority value="debug" />
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</log4j:configuration>
別のプロファイル:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- ========================== ================================ -->
<!-- %p , DEBUG,INFO,WARN,ERROR,FATAL -->
<!-- #%r log -->
<!-- #%c , -->
<!-- #%t -->
<!-- #%n ,Windows “\r
”,Unix “
” -->
<!-- #%d , ISO8601, , :%d{yyy MMM dd HH:mm:ss,SSS}, :2002 10 18 22:10:28,921 -->
<!-- #%l , 、 , 。 :Testlog4.main(TestLog4.java:10) -->
<!-- ========================================================================== -->
<!-- ========================== ================================ -->
<!-- Log4j appender : -->
<!-- org.apache.log4j.ConsoleAppender( ), -->
<!-- org.apache.log4j.FileAppender( ), -->
<!-- org.apache.log4j.DailyRollingFileAppender( ), -->
<!-- org.apache.log4j.RollingFileAppender( ), -->
<!-- org.apache.log4j.WriterAppender( ) -->
<!-- ========================================================================== -->
<!-- -->
<appender name="filelog_appender"
class="org.apache.log4j.RollingFileAppender">
<!-- File : -->
<param name="File" value="log/testlog4jxml_all.log" />
<!-- , -->
<param name="Append" value="true" />
<!-- -->
<param name="MaxFileSize" value="1MB" />
<!-- -->
<param name="MaxBackupIndex" value="10000" />
<!-- -->
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p (%c:%L)- %m%n" />
</layout>
</appender>
<!-- -->
<appender name="filelog_daily" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="log/daily.log" />
<param name="DatePattern" value="'daily.'yyyy-MM-dd'.log'" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss\} %-5p] [%t] (%c:%L) - %m%n" />
</layout>
</appender>
<!-- -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p: %m%n" />
<!-- "%-5p: [%t] [%c{3}.%M(%L)] | %m%n" -->
</layout>
</appender>
<appender name="EMAIL_QQ" class="org.apache.log4j.net.SMTPAppender">
<param name="Threshold" value="INFO"/>
<param name="BufferSize" value="128" />
<param name="SMTPHost" value="smtp.qq.com" />
<param name="SMTPUsername" value="cjjvictory" />
<param name="SMTPPassword" value="***" />
<param name="From" value="[email protected]" />
<param name="To" value="[email protected]" />
<param name="Subject" value=" " />
<param name="LocationInfo" value="true" />
<param name="SMTPDebug" value="true" />
<layout class="org.cjj.log4j.extend.PatternLayout_zh">
<param name="ConversionPattern" value="[%d{ISO8601}] %-5p %c %m%n"/>
</layout>
</appender>
<!--- , appender -->
<appender name="ASYNC_test" class="org.apache.log4j.AsyncAppender">
<param name="BufferSize" value="10"/>
<appender-ref ref="EMAIL_QQ"/>
</appender>
<!-- -->
<category name="org.cjj" additivity="false">
<!-- , 5 , , ERROR , -->
<level value="ERROR" />
<appender-ref ref="filelog_daily" />
<appender-ref ref="daily_appender" />
<appender-ref ref="console" />
<appender-ref ref="ASYNC_test" />
</category>
</log4j:configuration>