dynamic log file name log4j xml file config with timestamp - java

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);

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

How to give separate log file only jar file package logs

get a logger instance named "com.foo"
Logger logger = Logger.getLogger("com.foo");
Now set its level. Normally you do not need to set the level of a logger programmatically. This is usually done in configuration files.
logger.setLevel(Level.INFO);
this is an example of log4j2 configuration
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<Appender type="File" name="File" path="location/to/logging/space/file.txt">
<Layout type="PatternLayout" pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Appender>
</Appenders>
<Loggers>
<Logger name="com.foo" level="trace" additivity="false">
<AppenderRef ref="File"/>
</Logger>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

How to resolve this error with RoutingAppender in Log4J - "AuditLogger cannot be located"?

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

Log4j2 regular expressions

I am working with the log4j2 configuration file settings to try and make the config file use regular expressions so the it can use multiple classes in the logger property and name attribute. This is what I have tried so far that doesn't appear to work.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="ERROR_FILE" fileName="../log/error.log">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
<File name="EVENT_FILE" fileName="../log/event.log">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<Loggers>
<Logger name="foo.test" level="trace"
additivity="false">
<AppenderRef ref="Console" />
</Logger>
<Logger name="foo.*" level="debug"
additivity="false">
<AppenderRef ref="ERROR_FILE" level="ERROR"/>
<AppenderRef ref="EVENT_FILE" level="INFO"/>
</Logger>
<Root level="trace">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
Has anybody ever been able to successfully use regular expressions or even globs in the log4j2 config file?
I figured out I do not need to put regular expressions at all in the package name. For example if in my foo package i had foo.bar, foo.foo, and foo.test. I would put foo in the Logger property name attribute to log the ERROR and INFO level logs from any any of 3 packages that begins with foo as such.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="ERROR_FILE" fileName="../log/error.log">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
<File name="EVENT_FILE" fileName="../log/event.log">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<Loggers>
<Logger name="foo.test" level="trace"
additivity="false">
<AppenderRef ref="Console" />
</Logger>
<Logger name="foo" level="debug"
additivity="false">
<AppenderRef ref="ERROR_FILE" level="ERROR"/>
<AppenderRef ref="EVENT_FILE" level="INFO"/>
</Logger>
<Root level="trace">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>

Defining file location in log4j2.xml after deploying WAR

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.

Categories

Resources