springboot log4j2配置参考-highlight-IDE控制台彩色日志

导读

  • 查看log4j官方样式说明,其中highlight相关说明如下图springboot log4j2配置参考-highlight-IDE控制台彩色日志_第1张图片
  • 查看ANSI COLOR NAME,如下图所示
    springboot log4j2配置参考-highlight-IDE控制台彩色日志_第2张图片
  • 关于windows下highlight不生效的问题说明

解决方法很简单,一个字:disableAnsi=“false”

在这里插入图片描述

开始配置

step1 maven

add log4j2 and exclude logback或log4j

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-log4j2artifactId>
dependency>

<dependency>  
    <groupId>org.springframework.bootgroupId>  
    <artifactId>spring-boot-starter-webartifactId>  
    <exclusions>
        <exclusion>  
            <groupId>org.springframework.bootgroupId>  
            <artifactId>spring-boot-starter-loggingartifactId>  
        exclusion>  
    exclusions>  
dependency> 

step2 application.properties

logging.config=classpath:log4j2.xml

step3 log4j2.xml

这里有针对windowns平台highlight生效的配置 disableAnsi=“false”




<configuration status="WARN" monitorInterval="30">
    
    <properties>
        
        <property name="LOG_HOME">/opt/logsproperty>
        
        <property name="LOG_NAME">user-serviceproperty>
        
        <property name="LOG_FORMAT">[%d{yyyy-MM-dd HH:mm:ss.SSS}] %p %t %c[%L] - %m %nproperty>
        <property name="LOG_FORMAT_CONSOLE">%d{yyyy-MM-dd HH:mm:ss.SSS} %highlight{%-5level} [%t] %highlight{%c{1.}.%M(%L)}: %msg%n%throwableproperty>
        
        <property name="BACKUP_HOME">${LOG_HOME}/$${date:yyyy-MM}property>
        
        <property name="BACK_HZ">%d{yyyy-MM-dd}property>
    properties>
    <appenders>
        
        <console name="console" target="SYSTEM_OUT">
            
            <PatternLayout pattern="${LOG_FORMAT_CONSOLE}" disableAnsi="false" noConsoleNoAnsi="false"/>
        console>

        
        <RollingFile name="infoLog" fileName="${LOG_HOME}/${LOG_NAME}-info.log"
                     filePattern="${BACKUP_HOME}/info/${LOG_NAME}-info-${BACK_HZ}-%i.log.gz">
            <PatternLayout pattern="${LOG_FORMAT}"/>
            <Policies>
                
                <TimeBasedTriggeringPolicy modulate="true" interval="4"/>
                <SizeBasedTriggeringPolicy size="50 MB" />
            Policies>
            
            <DefaultRolloverStrategy max="10">
                
                <Delete basePath="${BACKUP_HOME}/info/" maxDepth="2">
                    
                    <IfFileName glob="${LOG_NAME}-info*.log.gz"/>
                    
                    <IfLastModified age="7d"/>
                Delete>
            DefaultRolloverStrategy>
			
            <Filters>
                <ThresholdFilter level="WARN" onMatch="DENY" onMisMatch="NEUTRAL"/>
                <ThresholdFilter level="INFO" onMatch="ACCEPT" onMisMatch="DENY"/>
            Filters>
        RollingFile>

        
        <RollingFile name="warnLog" fileName="${LOG_HOME}/${LOG_NAME}-warn.log"
                     filePattern="${BACKUP_HOME}/warn/${LOG_NAME}-warn-${BACK_HZ}-%i.log.gz">
            <PatternLayout pattern="${LOG_FORMAT}"/>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="4"/>
                <SizeBasedTriggeringPolicy size="50 MB" />
            Policies>
            <DefaultRolloverStrategy max="15">
                <Delete basePath="${BACKUP_HOME}/warn/" maxDepth="1">
                    <IfFileName glob="${LOG_NAME}-warn*.log.gz"/>
                    <IfLastModified age="7d"/>
                Delete>
            DefaultRolloverStrategy>
			
            <Filters>
                <ThresholdFilter level="ERROR" onMatch="DENY" onMisMatch="NEUTRAL"/>
                <ThresholdFilter level="WARN" onMatch="ACCEPT" onMisMatch="DENY"/>
            Filters>
        RollingFile>

        
        <RollingFile name="errorLog" fileName="${LOG_HOME}/${LOG_NAME}-error.log"
                     filePattern="${BACKUP_HOME}/error/${LOG_NAME}-error-${BACK_HZ}-%i.log.gz">
            <PatternLayout pattern="${LOG_FORMAT}"/>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="4"/>
                <SizeBasedTriggeringPolicy size="50 MB" />
            Policies>
            <DefaultRolloverStrategy max="15">
                <Delete basePath="${BACKUP_HOME}/error/" maxDepth="1">
                    <IfFileName glob="${LOG_NAME}-error*.log.gz"/>
                    <IfLastModified age="7d"/>
                Delete>
            DefaultRolloverStrategy>
			
            <ThresholdFilter level="ERROR"/>
        RollingFile>
    appenders>


    <loggers>
        
        <root level="info">
            <appender-ref ref="console"/>
            <appender-ref ref="infoLog"/>
            <appender-ref ref="warnLog"/>
            <appender-ref ref="errorLog"/>
        root>

        <logger name="com.alibaba.nacos.client.config.impl" level="warn">logger>
        
        
    loggers>
configuration>

你可能感兴趣的:(springcloud)