比较全的log4j示例
<?xml version="1.0" encoding="UTF-8"?> <configuration status="off"> <!--先定义所有的appender --> <appenders> <!--这个输出控制台的配置 --> <Console name="Console" target="SYSTEM_OUT"> <!--这个都知道是输出日志的格式 --> <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n%ex" /> <!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) --> <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY" /> </Console> <RollingFile name="RollingFile" fileName="/log/CFDMS.log" filePattern="/log/CFDMS.%d{yyyy-MM-dd}.log"> <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n%ex" charset="UTF-8" /> <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY" /> <Policies> <!-- 每天一个文件 --> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> <!-- 保存365天的记录 --> <DefaultRolloverStrategy max="365" /> </Policies> </RollingFile> <RollingFile name="RollingFileSuccess" fileName="/log/CFDMSSuccess.log" filePattern="/log/CFDMSSuccess.%d{yyyy-MM-dd}.log"> <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n%ex" charset="UTF-8" /> <Filters> <!-- 显示error级别之外的信息 --> <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/> <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <Policies> <!-- 每天一个文件 --> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> <!-- 保存365天的记录 --> <DefaultRolloverStrategy max="365" /> </Policies> </RollingFile> <RollingFile name="RollingFileError" fileName="/log/CFDMSError.log" filePattern="/log/CFDMSError.%d{yyyy-MM-dd}.log"> <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n%ex" charset="UTF-8" /> <Filters> <!-- 只显示error级别的信息 --> <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <Policies> <!-- 每天一个文件 --> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> <!-- 保存365天的记录 --> <DefaultRolloverStrategy max="365" /> </Policies> </RollingFile> <!-- 自定义一个log文件在程序中直接使用 --> <RollingFile name="liuyxlog" fileName="/log/liuyx.log" append="true" filePattern="/log/liuyx$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz"> <PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/> <SizeBasedTriggeringPolicy size="4 MB"/> </RollingFile> </appenders> <!--然后定义logger,只有定义了logger并引入的appender,appender才会生效 --> <loggers> <!-- 3rdparty Loggers --> <logger name="org.springframework.core" level="warn"> </logger> <logger name="org.springframework.beans" level="warn"> </logger> <logger name="org.springframework.context" level="warn"> </logger> <logger name="org.springframework.web" level="warn"> </logger> <logger name="org.jboss.netty" level="warn"> </logger> <logger name="org.apache.http" level="warn"> </logger> <logger name="com.mchange.v2" level="warn"> </logger> <!-- 自定义一个log文件在程序中直接使用 --> <logger name="liuyxlogger" additivity="TRUE" level="ALL"> <appender-ref ref="liuyxlog" level="ALL" /> </logger> <!--建立一个默认的root的logger --> <root level="info"> <appender-ref ref="RollingFile" /> <appender-ref ref="RollingFileSuccess" /> <appender-ref ref="RollingFileError" /> <appender-ref ref="Console" /> </root> </loggers> </configuration>
package com.inspur; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class WhereIsMyLogFile { private static Logger logger = LogManager.getLogger("liuyxlogger"); private static Logger logger2 = LogManager.getLogger(WhereIsMyLogFile.class); public static void main(String[] args) { logger.trace("hello liuyx log"); logger2.info("from logger2"); logger2.error("im an error"); } }
输出结果
如果想输出到tomcat根目录下,可以在路径前加上${sys:catalina.home}