0

i want to implement spring 5 with log4j2 profile, like dev, qa and prod.

At present, This is how i run my spring 5 with log4j2, i load the log4j2 properties in command line:

mvn tomcat8:run -Dlog4j.configurationFile=file:%path_to_log4j2_prop%/log4j2.properties

and this is my log4j2 properties:

status = debug
name = PropertiesConfig
 
#Make sure to change log file path as per your need
property.environment=D:/WhiteCoats-New-Workspace/spring-5
property.servicesfilename = ${environment}/wc.out
 
filters = threshold
 
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
 
appenders = services
 
appender.services.type = RollingFile
appender.services.name = ServiceFile
appender.services.fileName = ${servicesfilename}
appender.services.filePattern = ${environment}/wc.out-%d{yyyy-MM-dd}-%i
appender.services.layout.type = PatternLayout
appender.services.layout.pattern = [ %d{yyyy-MMM-dd HH:mm:ss a} ] - [%t] %-5level %logger{36} - %msg%n
appender.services.policies.type = Policies
appender.services.policies.time.type = TimeBasedTriggeringPolicy
appender.services.policies.time.interval = 1
appender.services.policies.time.modulate = true
appender.services.policies.size.type = SizeBasedTriggeringPolicy
appender.services.policies.size.size=5MB
appender.services.strategy.type = DefaultRolloverStrategy
appender.services.strategy.max = 10

loggers = services
 
#services logs
 
logger.services.name = com.vm
logger.services.level = debug
logger.services.additivity = false
logger.services.appenderRef.services.ref = ServiceFile

rootLogger.level = info
rootLogger.appenderRef.stdout.ref = ServiceFile

In spring 3, i used log4j and configured the path in web.xml.

how do i implement profiles for log4j2 for spring 5?

1 Answers1

0

In Spring Boot, if you need to change the property log4j.configurationFile it is very simple within the application.yml. You define the default, and the override value in case you use a profile. For example:

log4j:
    configurationFile: %path_to_log4j2_prop%
---
spring:
    profiles: development
log4j:
    configurationFile: %path_to_dev_log4j2_prop%
---
spring:
    profiles: production
log4j:
    configurationFile: %path_to_prod_log4j2_prop%

If you use Spring only, than you can use @Configuration with @Profile and @PropertySource to load different property files according to your profile, and in those files you can set your log4j.configurationFile property based on your needs.

Tudor Culda
  • 71
  • 1
  • 4