Log4j.properties in Spring boot

后端 未结 3 1958
青春惊慌失措
青春惊慌失措 2020-12-08 03:16

How to load Custom Log4j.properties file in Spring boot

My code in application.properties is here

logging.file=E:/Apps_Tek/apps-webservices-log/apps-         


        
3条回答
  •  有刺的猬
    2020-12-08 03:49

    To exclude default logging and include the log4j dependency for spring boot in your pom.xml:

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

    if you wish to use log4j properties defined in you application.properties inside the log4j.properties file need to add below property in application.properties file.

    logging.config = src/main/resources/log4j2.properties

    Then spring boot will read the properties from log4j2.properties file

    In log4j2.properties file add the below properties

    name=PropertiesConfig
    appenders = console, file
    
    appender.console.type = Console
    appender.console.name = ConsoleAppender
    appender.console.layout.type = PatternLayout
    appender.console.layout.pattern = %d{MM:dd HH:mm:ss.SSS} [%t] [%level] [%logger{36}] - %msg%n
    
    appender.file.type = File
    appender.file.name = FileAppender
    appender.file.fileName=/home/ubuntu/application.log
    appender.file.layout.type=PatternLayout
    appender.file.layout.pattern= %d{MM:dd HH:mm:ss.SSS} [%t] [%level] [%logger{36}] - %msg%n
    
    loggers=file
    logger.file.name=com.project
    logger.file.level = debug
    logger.file.appenderRefs = file
    logger.file.appenderRef.file.ref = FileAppender
    
    rootLogger.level = debug
    rootLogger.appenderRefs = stdout
    rootLogger.appenderRef.stdout.ref = ConsoleAppender
    

    Is there any other work around which doesnot require to specify 'logging.config = src/main/resources/log4j2.properties' inside application.properties file..?

    Note : Spring boot version i am using is 2.1.3.RELEASE

    Reference : https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html

提交回复
热议问题