使用java log4j記錄程式log
1.至apache下載2.4.1版本,然後引用到java專案中
2.將下面的XML存成log4j2.XML,存在專案的/SRC/目錄下
<Configuration status="warn">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5p] %d %c - %m%n" />
</Console>
<File name="File" fileName="log/my.log">
<PatternLayout pattern="%m%n" />
</File>
<RollingFile name="RollingFile" fileName="log/fileAdd.log"
filePattern="log/$${date:yyyy-MM}/fileAdd-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout pattern="[%-5p] %d %c - %m%n" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="fileAdd" level="INFO" additivity="false">
<AppenderRef ref="File" />
<AppenderRef ref="RollingFile" />
</Logger>
<Root level="WARN">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>
</Configuration>
3.在專案中import下面2個LIB
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
4.程式有需要輸出LOG的地方加入以下的程式即可。
log.warn("There are filesAdd2 start now.");
是否輸出檔案的控制都在log4j2.xml中去設定
2.將下面的XML存成log4j2.XML,存在專案的/SRC/目錄下
<Configuration status="warn">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5p] %d %c - %m%n" />
</Console>
<File name="File" fileName="log/my.log">
<PatternLayout pattern="%m%n" />
</File>
<RollingFile name="RollingFile" fileName="log/fileAdd.log"
filePattern="log/$${date:yyyy-MM}/fileAdd-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout pattern="[%-5p] %d %c - %m%n" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="fileAdd" level="INFO" additivity="false">
<AppenderRef ref="File" />
<AppenderRef ref="RollingFile" />
</Logger>
<Root level="WARN">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>
</Configuration>
3.在專案中import下面2個LIB
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
4.程式有需要輸出LOG的地方加入以下的程式即可。
log.warn("There are filesAdd2 start now.");
是否輸出檔案的控制都在log4j2.xml中去設定
留言
張貼留言