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
Related
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);
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.
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>
I'm getting the following error when I run my Log4j code :
2015-07-04 19:08:04,385 ERROR Appender AuditLogger cannot be located. Route ignored
I'm trying to use the RoutingAppender (I learned about it from this question ).
My log4j config files looks like this :
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<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="OutputLogFile.log" immediateFlush="false" append="false">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<Routing name="Routing">
<Routes pattern="$${sd:type}">
<Route>
<RollingFile name="Rolling-${sd:type}" fileName="${sd:type}.log"
filePattern="${sd:type}.%i.log.gz">
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
<SizeBasedTriggeringPolicy size="500" />
</RollingFile>
</Route>
<Route ref="AuditLogger" key="Audit"/>
</Routes>
</Routing>
</Appenders>
<Loggers>
<Root level="all">
<Appender-Ref ref="Console"/>
<Appender-Ref ref="MyFile"/> <!-- added_in now -->
</Root>
</Loggers>
</Configuration>
And my code is just a simple client/server pair (with some logger.debug("xyz") code spread throughout) .
thanks
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.