log4j2日志配置和使用demo

1.pom引入

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter</artifactId>
	<exclusions>
		<exclusion>
			<artifactId>spring-boot-starter-logging</artifactId>
			<groupId>org.springframework.boot</groupId>
		</exclusion>
	</exclusions>
</dependency>

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

2.log4j2.xml配置

log4j2.xml需要放在resources中

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="OFF" monitorInterval="1800">
    <!-- 该配置文件只可修改 baseLogPath 和 applicationName 其他值请勿动    -->
    <Properties>
        <Property name="rootLoggerLevel">INFO</Property>
        <Property name="bizLoggerLevel">INFO</Property>
        <Property name="warnLoggerLevel">WARN</Property>
        <Property name="exceptionLoggerLevel">ERROR</Property>
        <!-- 日志根目录 -->
        <Property name="baseLogPath">logs/</Property>
        <!-- 日志格式 -->
        <Property name="logPattern">%d{yyyy-MM-dd HH:mm:ss:SSS} - %p - %msg%ex%n</Property>
        <Property name="applicationName">applicationName</Property>
    </Properties>
    <Appenders>
        <!-- 控制台输出,请在调试环境下使用 -->
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="${logPattern}"/>
        </Console>
        <!-- 日志信息输出到文件配置 -->
        <RollingFile name="RollingFileInfo" fileName="${baseLogPath}/${applicationName}.log"
                     filePattern="${baseLogPath}/INFO/app-%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout pattern="${logPattern}" />
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="50 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="100">
                <Delete basePath="${baseLogPath}" maxDepth="2">
                    <IfFileName glob="*/app-*.log.gz">
                        <IfLastModified age="7d">
                        </IfLastModified>
                    </IfFileName>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>

        <RollingFile name="RollingFileError" fileName="${baseLogPath}/error.log"
                     filePattern="${baseLogPath}/error/error-%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout pattern="${logPattern}" />
            <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            <Policies>
                <!--每天分割一次-->
                <TimeBasedTriggeringPolicy />
                <!--文件每满50MB分割一次-->
                <SizeBasedTriggeringPolicy size="50 MB"/>
            </Policies>

            <DefaultRolloverStrategy max="100">
                <Delete basePath="${baseLogPath}" maxDepth="2">
                    <IfFileName glob="*/error-*.log.gz">
                        <IfLastModified age="7d">
                        </IfLastModified>
                    </IfFileName>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>

    </Appenders>

    <Loggers>
        <logger name="com.mmtrix.dao" level="debug" additivity="false">
            <appender-ref ref="Console"></appender-ref>
        </logger>
        <Root level="${rootLoggerLevel}">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFileInfo"/>
            <appender-ref ref="RollingFileError"/>
        </Root>
    </Loggers>
</Configuration>

3.使用

private static Logger logger = LoggerFactory.getLogger(DemoApplication.class);

public static void main(String[] args) {
	logger.info("系统启动中...");
	SpringApplication.run(DemoApplication.class, args);
	logger.info("系统启动成功");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值