SpringBoot配置Log4j并且生成本地文件

文件目录:

SpringBoot配置Log4j并且生成本地文件_第1张图片

log4j.properties

#log4j 配置
#log4j定义了8个级别的log优先级从高到低依次为:OFF、FATAL、ERROR、WARN、INFO、DEBUG、TRACE、 ALL。
#CONSOLE前面的DEBUG是控制台需要打印什么DEBUG级别以上的日志的信息DEBUG, CONSOLE, ERROR, WARN, INFO, DEBUG, ALL
log4j.rootLogger=ERROR, CONSOLE, ERROR, WARN, INFO, DEBUG, ALL
#-----------------------------------------------------------------------------------------------------
#输出信息到控制台CONSOLE
log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender 
log4j.appender.CONSOLE.Target = System.out
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
#-----------------------------------------------------------------------------------------------------
#输出ERROR 级别以上的日志到error.log
log4j.logger.ERROR=ERROR
log4j.appender.ERROR = org.apache.log4j.DailyRollingFileAppender
log4j.appender.ERROR.File =logs/error/error.log
log4j.appender.file.DatePattern ='.'yyyy-MM-dd
log4j.appender.ERROR.Threshold = ERROR 
log4j.appender.ERROR.Append = true
log4j.appender.ERROR.layout = org.apache.log4j.PatternLayout
log4j.appender.ERROR.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
#-----------------------------------------------------------------------------------------------------
#输出WARN 级别以上的日志到warn.log
log4j.logger.WARN=WARN
log4j.appender.WARN = org.apache.log4j.DailyRollingFileAppender
log4j.appender.WARN.File =logs/warn/warn.log
log4j.appender.file.DatePattern ='.'yyyy-MM-dd
log4j.appender.WARN.Threshold = WARN
log4j.appender.WARN.Append = true
log4j.appender.WARN.layout = org.apache.log4j.PatternLayout
log4j.appender.WARN.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
#-----------------------------------------------------------------------------------------------------
#输出INFO级别以上的内容到info.log中
log4j.logger.INFO=INFO
log4j.appender.INFO = org.apache.log4j.DailyRollingFileAppender
log4j.appender.INFO.File = logs/info/info.log
log4j.appender.file.DatePattern ='.'yyyy-MM-dd
log4j.appender.INFO.Threshold = INFO 
log4j.appender.INFO.Append = true
log4j.appender.INFO.layout = org.apache.log4j.PatternLayout
log4j.appender.INFO.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
#-----------------------------------------------------------------------------------------------------
#输出DEBUG 级别以上的日志到debugger.log
log4j.logger.DEBUG=DEBUG
log4j.appender.DEBUG = org.apache.log4j.DailyRollingFileAppender
log4j.appender.DEBUG.File = logs/debugger/debugger.log
log4j.appender.file.DatePattern ='.'yyyy-MM-dd
log4j.appender.DEBUG.Threshold = DEBUG
log4j.appender.DEBUG.Append = true
log4j.appender.DEBUG.layout = org.apache.log4j.PatternLayout
log4j.appender.DEBUG.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
#-----------------------------------------------------------------------------------------------------
#输出ALL级别的日志到all.log
log4j.logger.ALL=ALL
log4j.appender.ALL = org.apache.log4j.DailyRollingFileAppender
log4j.appender.ALL.File = logs/all/all.log
log4j.appender.file.DatePattern ='.'yyyy-MM-dd
log4j.appender.ALL.Threshold = ALL 
log4j.appender.ALL.Append = true
log4j.appender.ALL.layout = org.apache.log4j.PatternLayout
log4j.appender.ALL.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

 

application.yml


server:
  port: 8011

spring:
  datasource:
    username: 
    password: 
    url: jdbc:
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource


mybatis:
  mapper-locations: classpath:mapping/*.xml
  type-aliases-package: com.xjgz.xxty.videoremote.dto






 

pom文件:(需要排除原来存在的log4j)



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.1.RELEASE
         
    
    com.xjgz.xxty
    videoremote
    0.0.1-SNAPSHOT
    videoremote
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
            
        

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.3
        


        
        
            mysql
            mysql-connector-java
            runtime
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.springframework.boot
            spring-boot-starter-log4j
            1.3.8.RELEASE
        

        
            org.springframework.boot
            spring-boot-starter-tomcat
            provided
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        

        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.10
        



        
            com.alibaba
            fastjson
            1.2.35
        




    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        

        
            
                true
                src/main/resources
                
                    *.properties
                    *.yml
                    */*.xml
                
            
        
    


 

使用:

protected static Logger logger = LoggerFactory.getLogger(CompleteScheduleConfig.class);


logger.error("error:",e);

 

你可能感兴趣的:(工具包的简单使用,SpringBoot)