Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j.properties in Spring boot

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-webservices.log
logging.level.*=INFO
logging.config=log4j.properties

My code in log4j.properties is here

log4j.rootLogger=INFO,ConsoleAppender,FileAppender

log4j.appender.ConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.ConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ConsoleAppender.layout.ConversionPattern=%-7p %d [%t] %c [%X{userName}] [%X{accessToken}] - %m%n

log4j.appender.FileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.FileAppender.File=E:/Apps_Tek/apps-webservices-log/apps-webservices.log
log4j.appender.FileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.FileAppender.layout.ConversionPattern=%-7p %d [%t] %c [%X{userName}] [%X{accessToken}] - %m%n

But i am not getting any expected output i.e., spring boot is not loading log4j.properties file. Spring boot is having its own default logging.

log4j.properties file is in src/main/resources

My question is how to map log4j.properties file with logging.config property in application.properties if it is in src/main/resources.

Please suggest all the required changes.

Thanks for any help in advance.

like image 892
Varun Raj Avatar asked Mar 24 '15 05:03

Varun Raj


People also ask

Where does log4j properties file go in spring boot?

By default, Log4J 2 looks for a properties file with the name log4j2. properties in the classpath. In a Spring Boot application, the log4j2. properties file will typically be in the resources folder.

Can we use log4j in Spring boot?

Spring Boot also supports either Log4j or Log4j 2 for logging configuration, but only if one of them is on the classpath. If you are using the starter poms for assembling dependencies that means you have to exclude Logback and then include your chosen version of Log4j instead.


3 Answers

If you want spring boot to use log4j instead of its own default logging (logback) then you have to exclude default logging and include the log4j dependency for spring boot in your pom.xml:

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

that way is going to look for your log4j.properties file located in src/main/resources.

Also if you wish to use properties defined in you application.properties inside the log4j.properties file

for example, you want to have log.file.path defined in your application.properties and use it on log4j.properties

Then you have to define filtering inside your pom.xml:

<filters>     <filter>src/main/resources/application.properties</filter> </filters> <resources>     <resource>         <directory>src/main/resources</directory>         <includes>             <include>log4j.properties</include>         </includes>                  <filtering>true</filtering>     </resource> </resources> 

That way you can have:

log4j.appender.file.File=${log.file.path}/${project.artifactId}.log 

in your log4j.properties file

like image 88
Diego Marinelli Avatar answered Oct 13 '22 19:10

Diego Marinelli


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

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

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

like image 43
J Febi Avatar answered Oct 13 '22 20:10

J Febi


My guess is that your pom.xml file is not set up to include the correct log4j dependency. If you do that it should automatically work. See their example at spring-boot-sample-actuator-log4j. Their project includes the artifact spring-boot-starter-log4j which should then allow your log4j.properties to be picked up from the current src/main/resources location and you should no longer need the logging.* entries in your properties. It may also be possible that you need to exclude their spring-boot-starter-logging as I see they are doing that as well. If this does not seem to resolve it please post your POM file in a location I can see it. Please let me know if this works for you.

like image 30
Rob Baily Avatar answered Oct 13 '22 21:10

Rob Baily