Defining file location in log4j2.xml after deploying WAR - java

I have gotten my print to file to work for log4j2 logging, but when deploying my project as a WAR onto a tomcat server, I cant seem to find where the log file is sent. I have the following log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="AE" fileName="/AE.log" append="false">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} -
%msg%n"/>
</File>
<File name="BP" fileName="/BP.log" append="false">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} -
%msg%n"/>
</File>
<File name="CBK" fileName="/CBK.log" append="false">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} -
%msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console" level="debug" />
<AppenderRef ref="AE" level="debug"/>
<AppenderRef ref="BP" level="debug"/>
<AppenderRef ref="CBK" level="debug"/>
</Root>
</Loggers>
</Configuration>
Is there a way that I can properly define my filename paths so that after having deployed my WAR file, my logs are sent to somewhere such as WEB-INF?

About the disappearing log files, could it be that the user that is running the tomcat server does not have permission to write files to the root directory?
About your filename path question, it is possible to use system properties in the path by using the ${sys:some.property} syntax.

Related

Print the log file name set in log4J2 xml in Java code

Below is my log4j2.xml'
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="LogToConsole" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="LogToFile" fileName="logs/myapp_-${date:yyyyMMddHHmmss}.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="LogToConsole"/>
<AppenderRef ref="LogToFile"/>
</Root>
</Loggers>
</Configuration>
It generates log files based on timestamp in logs/ directory.
I want to print the logfile name ( value of LogToFile variable ) in my java code and give to user so that he can come to know what is the exact location and name of the log file of his run.
Any idea if this is possible?
Kind Regards,
Abhi

dynamic log file name log4j xml file config with timestamp

I have bellow xml config file with static log filename "CRM_Update_dubg_Logs.log", I want to make this name with timestamp, java log4j 2.9.1
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS}{GMT+3} [%t] %-5level
%logger{36} - %msg%n" />
</Console>
<File name="MyFile" fileName="CRM_Update_Logs.log" immediateFlush="true" append="true">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS}{GMT+3} %c{2} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<File name="dubg_Logs" fileName="CRM_Update_dubg_Logs.log" immediateFlush="true" append="true">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS}{GMT+3} %c{2} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console"/>
<AppenderRef ref="MyFile"/>
<AppenderRef ref="dubg_Logs"/>
</Root>
<Logger level="ALL" additivity="false" name="dubg">
<AppenderRef ref="dubg_Logs"/>
</Logger>
</Loggers>
Edit filename in xml
fileName="CRM_Update_Logs.log"
to be
fileName="${sys:logFilename}_CRM_Update_Logs.log"
and add this to your java code
Date date = new Date();
String LogDate= new SimpleDateFormat("yyyyMMdd").format(date);
System.setProperty("logFilename", LogDate);

setting up log4j2 in new web app

I'm upgrading my web app to include log4j2 and having difficulty configuring. I have my log4j2.xml file located in WEB-INF/classes folder. I want log4j2 to write to my log file named jed.log in WEB-INF/logs/ folder. My xml file looks like so:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="MyFile" fileName="/WEB-INF/logs/jed.log" immediateFlush="false" append="false">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console" />
<AppenderRef ref="MyFile"/>
</Root>
</Loggers>
</Configuration>
Log4j2 is definitely logging to the console so I know that part is working, but I can't seem to make it see the log file. For the fileName attribute I've tried setting it to "/WEB-INF/logs/jed.log", "WEB-INF/logs/jed.log", "../logs/jed.log". Nothing seems to work.

Log file not rolling automatically in jetty 9.3.16

Currently, I am moving the server and upgrade the jetty from 9.2 to 9.3. But unfortunately, I found that the logging file doesn't role automatically in 9.3, however, it was working fine in jetty 9.2.
The file name only changed when I restart the server.
I am thinking, do I miss some config in jetty 9.3? Thanks.
1, In 9.3.16
2, Previously in jetty 9.2.
My log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} [%M](%L) - %msg%n"/>
</Console>
<File name="log" fileName="log/test.log" append="false">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
</File>
<RollingFile name="RollingFile" fileName="log/app.log"
filePattern="log/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>
<SizeBasedTriggeringPolicy size="50MB"/>
</RollingFile>
</appenders>
<loggers>
<root level="info">
<appender-ref ref="RollingFile"/>
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>

Logging file is not created

I use log4j2 in my eclipse plugin for logging. I created the following log4j2.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
<Appenders>
<File name="File" fileName="MyLog.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="File" immediateFlush="true"/>
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
I can see the logs in the console, but the log file seems not even to be created. In the mean time, I'm quite desperated. What am I doing wrong?
Nothing wrong with the configuration file. If you are using eclipse to test log file will get generated in root folder of the project. You may want to use fileName="target/MyLog.log" or absolute file path to to move file to anticipated folder.

Categories

Resources