Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Multiple log files for classes/packages using log4j2 property file

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have migrated my multi-module project from log4j to log4j2 using the property file.
I am able to print log on console and log file as well for the whole project.
Below working log4j2 property configuration used to generate a single log file

loggers=my.db.adapter, my.project.logic, my.project.service,
my.project.ws.adapter, my.project.test, my.server.adapter, my.util

rootLogger.level=DEBUG appenders=s_console_append, s_rolling_append
rootLogger.appenderRefs=s_ar_console, s_ar_rolling
rootLogger.appenderRef.s_ar_console.ref=projectServiceConsole
rootLogger.appenderRef.s_ar_rolling.ref=projectServiceRollingFile

logger.my.db.adapter.name=my.db.adapter
logger.my.db.adapter.level=DEBUG
logger.my.project.logic.name=my.project.logic
logger.my.project.logic.level=DEBUG
logger.my.project.service.name=my.project.service
logger.my.project.service.level=DEBUG
logger.my.project.ws.adapter.name=my.project.ws.adapter
logger.my.project.ws.adapter.level=DEBUG
logger.my.project.test.name=my.project.test
logger.my.project.test.level=DEBUG
logger.my.server.adapter.name=my.server.adapter
logger.my.server.adapter.level=DEBUG logger.my.util.name=my.util
logger.my.util.level=DEBUG

appender.s_console_append.type=Console
appender.s_console_append.name=projectServiceConsole
appender.s_console_append.layout.type=PatternLayout
appender.s_console_append.layout.pattern=%d{dd.MM.yyyy HH\:mm\:ss}%-5p %t [%C{1}]\: %m%n

appender.s_rolling_append.type=RollingFile
appender.s_rolling_append.name=projectServiceRollingFile
appender.s_rolling_append.fileName=${sys:config.base}/logs/my-mw-${sys:logfilename}-app.log
appender.s_rolling_append.filePattern=${sys:config.base}/logs/my-mw-${sys:logfilename}-app-%d{yyyy-MM-dd}
appender.s_rolling_append.layout.type=PatternLayout
appender.s_rolling_append.layout.pattern=%d{dd.MM.yyyy HH\:mm\:ss}%-5p %t [%C{1}]\: %m%n
appender.s_rolling_append.policies.type=Policies
appender.s_rolling_append.policies.time.type=TimeBasedTriggeringPolicy
appender.s_rolling_append.policies.time.interval=1
appender.s_rolling_append.policies.time.modulate=true




But facing problems when trying to generate one/two separate log files from a single log4j2 property file for a class. the same functionality was working with log4j.
I tried to solve it by referring many posts, but unable to solve the issue.

following are the changes I did in the log4j2 property file to generate two files, one for the whole project and others for a single class

loggers=my.db.adapter, my.project.logic, my.project.service,
my.project.ws.adapter, my.project.test, my.server.adapter, my.util,
service rootLogger.level=DEBUG appenders=s_console_append,
s_rolling_append rootLogger.appenderRefs=s_ar_console, s_ar_rolling
rootLogger.appenderRef.s_ar_console.ref=projectServiceConsole
rootLogger.appenderRef.s_ar_rolling.ref=projectServiceRollingFile

logger.my.db.adapter.name=my.db.adapter
logger.my.db.adapter.level=DEBUG
logger.my.project.logic.name=my.project.logic
logger.my.project.logic.level=DEBUG
logger.my.project.service.name=my.project.service
logger.my.project.service.level=DEBUG
logger.my.project.ws.adapter.name=my.project.ws.adapter
logger.my.project.ws.adapter.level=DEBUG
logger.my.project.test.name=my.project.test
logger.my.project.test.level=DEBUG
logger.my.server.adapter.name=my.server.adapter
logger.my.server.adapter.level=DEBUG logger.my.util.name=my.util
logger.my.util.level=DEBUG

logger.service.name=service logger.service.level=debug
logger.appendar=service logger.appenderRefs=service
logger.service.appenderRef.service.ref=service


appender.s_console_append.type=Console
appender.s_console_append.name=projectServiceConsole
appender.s_console_append.layout.type=PatternLayout
appender.s_console_append.layout.pattern=%d{dd.MM.yyyy HH\:mm\:ss}%-5p %t [%C{1}]\: %m%n


appender.s_rolling_append.type=RollingFile
appender.s_rolling_append.name=projectServiceRollingFile
appender.s_rolling_append.fileName=${sys:config.base}/logs/my-mw-${sys:logfilename}-app.log
appender.s_rolling_append.filePattern=${sys:config.base}/logs/my-mw-${sys:logfilename}-app-%d{yyyy-MM-dd}
appender.s_rolling_append.layout.type=PatternLayout
appender.s_rolling_append.layout.pattern=%d{dd.MM.yyyy HH\:mm\:ss}%-5p %t [%C{1}]\: %m%n
appender.s_rolling_append.policies.type=Policies
appender.s_rolling_append.policies.time.type=TimeBasedTriggeringPolicy
appender.s_rolling_append.policies.time.interval=1
appender.s_rolling_append.policies.time.modulate=true

appender.service.type=RollingFile appender.service.name=service
appender.service.fileName=${sys:config.base}/logs/my-mw-${sys:logfilename}-geocode.log
appender.service.filePattern=${sys:config.base}/logs/my-mw-${sys:logfilename}-app-%d{yyyy-MM-dd}
appender.service.layout.type=PatternLayout
appender.service.layout.pattern=%d{dd.MM.yyyy HH\:mm\:ss} %-5p %t[%C{1}]\: %m%n
appender.service.policies.type=Policies
appender.service.policies.time.type=TimeBasedTriggeringPolicy
appender.service.policies.time.interval=1
appender.service.policies.time.modulate=true




Java code is added as below to generated other log file

public class Xyz { private static Logger logger =
LogManager.getLogger("service"); ....}



But unable to generate a separate log file for the class.
Please help me to correct the log4j2 configuration to resolve this issue.

Thanks in advance
reply
    Bookmark Topic Watch Topic
  • New Topic