log4j配置

maven里面加入log4j夹包
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.3</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.9</version>
</dependency>



 log4j.xml配置如下: 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
	<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}] %7r %6p - %.65c - %m%n" />
		</layout>
		<filter class="org.apache.log4j.varia.LevelRangeFilter">
			<param name="levelMin" value="INFO" />
			<param name="levelMax" value="ERROR" />
			<param name="AcceptOnMatch" value="true" />
		</filter>
	</appender>
	<appender name="infoFile" class="org.apache.log4j.DailyRollingFileAppender">
		<!-- <param name="File" value="F:/logs/sdy-info.log" /> -->
		<param name="Append" value="true" />
		<param name="MaxBackupIndex" value="10" />
		<param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss}] %7r %6p - %.65c - %m%n" />
		</layout>
		<filter class="org.apache.log4j.varia.LevelRangeFilter">
			<param name="levelMin" value="INFO" />
			<param name="levelMax" value="INFO" />
			<param name="AcceptOnMatch" value="true" />
		</filter>
	</appender>
	<appender name="errorFile" class="org.apache.log4j.DailyRollingFileAppender">
		<!--<param name="File" value="F:/logs/sdy-error.log" />-->
		<param name="Append" value="true" />
		<param name="MaxBackupIndex" value="10" />
		<param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss}] %7r %6p - %.65c - %m%n" />
		</layout>
		<filter class="org.apache.log4j.varia.LevelRangeFilter">
			<param name="levelMin" value="ERROR" />
			<param name="levelMax" value="ERROR" />
			<param name="AcceptOnMatch" value="true" />
		</filter>
	</appender>
        <!-- ibatis日志输出配置 -->
        <appender name="IBatis" class="org.apache.log4j.ConsoleAppender">
                <layout class="org.apache.log4j.PatternLayout">
                        <param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" />
                </layout>
                <filter class="org.apache.log4j.varia.LevelRangeFilter">
                        <param name="LevelMin" value="DEBUG" />
                        <param name="LevelMax" value="DEBUG" />
                </filter>
        </appender>
        <logger name="com.ibatis" additivity="true">
                <level value="DEBUG" />
        </logger>
        <logger name="java.sql.Connection" additivity="true">
                <level value="DEBUG" />
        </logger>
        <logger name="java.sql.Statement" additivity="true">
                <level value="DEBUG" />
        </logger>
        <logger name="java.sql.PreparedStatement" additivity="true">
                <level value="DEBUG" />
                <appender-ref ref="IBatis" />
        </logger>
        <!-- 该配置会把日志写入到另一台机器,进行日志管理 -->
        <appender name="remoteFile" class="org.apache.log4j.net.SocketAppender">
		<param name="RemoteHost" value="192.168.1.222" /><!-- 指定服务器的主机名 -->
		<param name="Port" value="4567" /><!-- 端口号 -->
		<param name="Threshold" value="INFO" /><!-- log级别 -->
		<param name="ReconnectionDelay" value="10000" /><!-- 超时时间 -->
		<param name="LocationInfo" value="true" />
	</appender>
        <root>
                <priority value="info" />
                <appender-ref ref="console" />
                <appender-ref ref="IBatis" />
                <appender-ref ref="remoteFile" />
		<appender-ref ref="infoFile" />
		<appender-ref ref="errorFile" />
	</root>
</log4j:configuration>



java:

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
private static final Log LOG = LogFactory.getLog(ClassName.class);
LOG.info("info message");
LOG.error("error message");








posted @ 2022-10-27 08:48  何童鞋  阅读(49)  评论(0编辑  收藏  举报