Home > database >  Java Process Not Able To Write Logs To Console
Java Process Not Able To Write Logs To Console

Time:01-16

I am implementing log4j2 in my java application where I trying to write data to console using log4j2 configuration file. I have created a jar called Interface.jar . Soft Links are created for this jar and are run with different arguments . 2 soft links created are Interface1 and Interface2 with different arguments . Interface1 and Interface2 have different log4j2 configuration file path.

System.setProperty("log4j.configurationFile", System.getenv("LOG4J_INTERFACE1"));
System.setProperty("log4j.configurationFile", System.getenv("LOG4J_INTERFACE2"));

Log4j Configuration File For Interface1 is :

status = warn
appenders=console
property.LOG_PATTERN = %d{yyyy-MM-dd} %d{HH:mm:ss,SSS zzz}|%p|INTERFACE1||||||%m%n
appender.console.type = Console
appender.console.name = LogToConsole
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = ${LOG_PATTERN}
logger.Log4JExample.name = Log4JExample
logger.Log4JExample.level = debug
logger.Log4JExample.additivity = false
logger.Log4JExample.appenderRef.console.ref = LogToConsole
rootLogger.level = info
rootLogger.appenderRef.stdout.ref = LogToConsole

Log4j Configuration File For Interface2 is :

status = warn
appenders=console
property.LOG_PATTERN = %d{yyyy-MM-dd} %d{HH:mm:ss,SSS zzz}|%p|INTERFACE2||||||%m%n
appender.console.type = Console
appender.console.name = LogToConsole2
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = ${LOG_PATTERN}
logger.Log4JExample.name = Log4JExample
logger.Log4JExample.level = debug
logger.Log4JExample.additivity = false
logger.Log4JExample.appenderRef.console.ref = LogToConsole2
rootLogger.level = info
rootLogger.appenderRef.stdout.ref = LogToConsole2

The issue is that when I start both the process , Interface1 is able to write to console but Interface2 is not able to do so. Both the process are running fine. One challenge I see is that both these jars have save main class Interface.class .

 LogManager.getLogger(Interface.class);

Can anyone shed some light on how can I stream the data to console from multiple processes using soft links.

CodePudding user response:

Need to have 2 appenders. You can use give all the appenders with comma separated.

I can see both loggers have different format and both needs to write the console. In those cases you need to use Marker filter for different loggers.

below is the sample log4j2.properties file


appenders = console, console1

appender.console.type = Console
appender.console.name = LogToConsole
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd} %d{HH:mm:ss,SSS zzz}|%p|INTERFACE1||||||%m%n

appender.console1.type = Console
appender.console1.name = LogToConsole2
appender.console1.layout.type = PatternLayout
appender.console1.layout.pattern = %d{yyyy-MM-dd} %d{HH:mm:ss,SSS zzz}|%p|INTERFACE2||||||%m%n

loggers = rolling, interface2
logger.rolling.name = org.apache.logging.log4j.core.appender.rolling


#configure rootLogger and attach all the appenders to it
rootLogger.level = info
rootLogger.appenderRef.console.ref = LogToConsole

logger.interface2.name = Interface2
logger.interface2.additivity = false
logger.interface2.level = debug
logger.interface2.appenderRef.console1.ref = LogToConsole2

CodePudding user response:

It seems you are setting a system property like so:

System.setProperty("log4j.configurationFile", System.getenv("LOG4J_INTERFACE1"));

while you should be setting log4j2.configurationFile. Notice the log4j2 prefix.

I also recommend setting another system property

java -Dlog4j2.debug=true ...

just to see more what log4j2 is doing. You can turn it off once you are happy.

  • Related