log4j : generate log file name with parameters - java

Am using log4j as follows :
<appender name="LOGFILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="D:/direc/logs/back-${batch}-${dateexec}.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd-HH" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{dd MMM yyyy - HH:mm:ss,SSS} [%t] %-5p [%c] %M : (ligne %L) - %m %n" />
</layout>
</appender>
where {batch} and {dateexec} should refer to variables[from command parameters ] (batch name and execution time).
in this case the log file name is "back--.log"
Does any one knows how it works ?

Related

How to solve Log4J No Such Property warning/error?

I have a Log4J xml config file with appenders in it.
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<param name="File" value="/tomcat/website/webapps/app/logs/appInfo.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM dd HH:mm:ss} %F: %m%n" />
</layout>
</appender>
<appender name="rolling" class="org.apache.log4j.RollingFileAppender">
<param nawebsite/webapps/app/logs/app.log" />
<param name="MaxFileSize" value="1000KB" />
<param name="MaxBackupIndex" value="2" />
<param name="DatePattern" value="'.'yyyy-MM-dd'" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM dd HH:mm:ss} %F: %m%n" />
</layout>
</appender>
When I build my project, I get the following warnings/errors in the console which I am trying to get rid of:
log4j:WARN No such property [datePattern] in org.apache.log4j.RollingFileAppender.
log4j:WARN No such property [file] in org.apache.log4j.ConsoleAppender.
I did not make these log4j config xml files; they were already part of the project.
I've seen the DatePattern documentation online, and it seems to suggest exactly what is in the DatePattern value, so I have no idea why it's still showing me that warning. Same with file.
Did the person who made this before me just stick properties in these appenders that do not belong? I'm having a difficult time verifying this.
Every Appender has it's own configuration properties. file make sense for RollingFileAppender but doesn't make sense for ConsoleAppender.
If you switch one appender to another and do not update configuration properties, then you will get such warnings.
To fix them, you need to remove unused (or inapplicable) properties from your configuration:
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM dd HH:mm:ss} %F: %m%n" />
</layout>
</appender>
<appender name="rolling" class="org.apache.log4j.RollingFileAppender">
<param name="FileName" value="/tomcat/website/webapps/app/logs/app.log" />
<param name="MaxFileSize" value="1000KB" />
<param name="MaxBackupIndex" value="2" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM dd HH:mm:ss} %F: %m%n" />
</layout>
</appender>
Complete list of appenders and their parameters is available on the Apache Commons Log4j webpage:
https://logging.apache.org/log4j/2.x/manual/appenders.html

Making log4j write the same logs to multiple files

Pretty noob to this config; but I tried the config below in lo4j.xml It doesn't work. I want to write the same logs to two different files.
<appender name="FILE" class="com.compname.apt.appender.CustomAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="File" value="/home/c/logs/jboss/server.log"/>
<param name="Append" value="true"/>
<param name="Threshold" value="INFO"/>
<param name="BufferedIO" value="false"/>
<param name="BufferSize" value="8096"/>
<!-- Rollover at midnight each day by default -->
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<!-- The default pattern: Date Priority [Category] Message -->
<param name="ConversionPattern" value="'[%d] - %x %-5p [%c] %m%n'"/>
</layout>
</appender>
<appender name="flumeappender" class="com.compname.apt.appender.CustomAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="File" value="/home/y/logs/jboss/flume_ews.log"/>
<param name="Append" value="true"/>
<param name="Threshold" value="INFO"/>
<param name="BufferedIO" value="false"/>
<param name="BufferSize" value="8096"/>
<!-- Rollover at midnight each day by default -->
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<!-- The default pattern: Date Priority [Category] Message -->
<param name="ConversionPattern" value="'[%d] - %x %-5p [%c] %m%n'"/>
</layout>
</appender>
This is simple demo of log4j. Hopefully it is something you need.
#Destination
log4j.rootLogger=DEBUG, toConsole, toFile, DB
#Console
log4j.appender.toConsole=org.apache.log4j.ConsoleAppender
log4j.appender.toConsole.layout=org.apache.log4j.PatternLayout
log4j.appender.toConsole.layout.ConversionPattern=%d(HH:mm:ss) %5p [%t] - %c.%M - %m%n
#%x: Id, %d: Date, %C: Content,%p: Level, %m: Message
#log4j.appender.toConsole.layout.ConversionPattern=%x %d %C %p %m%n
#https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html
#DailyRollingFile
log4j.appender.toFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.toFile.File=./log/logfile.txt
log4j.appender.toFile.DatePattern='.' yyyy-MM-dd
log4j.appender.toFile.layout=org.apache.log4j.PatternLayout
log4j.appender.toFile.layout.ConversionPattern=%d(HH:mm:ss) %5p [%t] - %c.%M - %m%n
#log4j.appender.toFile.layout.ConversionPattern=%x %d %C %p %m%n
#JDBCAppender
# Define the file appender
log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DB.URL=jdbc:sqlserver://localhost:1433;databaseName=Trainees
# Set Database Driver
log4j.appender.DB.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
# Set database user name and password
log4j.appender.DB.user=sa
log4j.appender.DB.password=123456
# Set the SQL statement to be executed.
log4j.appender.DB.sql=INSERT INTO Logs(A,B,C,D) VALUES('%d(HH:mm:ss)', '%5p [%t]', '%c.%M', '%m%n')
# Define the xml layout for file appender
log4j.appender.DB.layout=org.apache.log4j.PatternLayout

Parameter value explanation in log4j.xml

log4j.xml
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
From parameter value pattern I can get that %d{yyyy-MM-dd HH:mm:ss} stands for some date format specifier, but I want to know about other patterns like what %-5p, %c{1}:%L, etc stands for?
Check out log4j pattern layout

log4j writing to rolled over file

We have set log4j rollover as 500mb upon 500 mb it is creating a file new file log.1 but still it is sometimes writing to log.1 and to log and sometimes only to log and some times to log.1.
I am using apache tomcat.
It is so inconsistent.
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p: %c - %m%n" />
</layout>
</appender>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="maxFileSize" value="500MB" />
<param name="maxBackupIndex" value="5" />
<param name="Append" value="true"/>
<param name="File" value="c:/logs/web.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %p [%c] - %m%n" />
</layout>
</appender>
Could you please let me know any reason for this?
Regards,
Adam

log4j debug messages not showing in console despite isDebugEnabled being true

I am using the following code within my project to log debug messages with log4j
private static final Logger LOG = Logger.getLogger(MyClass.class)
// ...
if(LOG.isDebugEnabled()) {
LOG.debug("my log message");
}
I can confirm that my log4j configuration is correct by adding a break point at the line where the debug message is written, i.e. LOG.isDebugEnabled() does return true.
Interestingly, my debug message does not show up in the console of my IDE (IntelliJ), however when changing LOG.debug() to LOG.info(), the info message is logged as expected.
Any ideas what I should be looking for in order to find out what's going wrong here?
EDIT: here's my log4j.properties file
log4j.appender.Stdout=org.apache.log4j.ConsoleAppender
log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.Stdout.layout.conversionPattern=%-5p [%d{dd.MM.yy HH:mm:ss}] %C{1} - %m [thread: %t]\n
log4j.appender.Stdout.threshold=info
log4j.appender.StandaloneFile=org.apache.log4j.RollingFileAppender
log4j.appender.StandaloneFile.File=logs/standalone.log
log4j.appender.StandaloneFile.MaxFileSize=5MB
log4j.appender.StandaloneFile.MaxBackupIndex=20
log4j.appender.StandaloneFile.layout=org.apache.log4j.PatternLayout
log4j.appender.StandaloneFile.layout.ConversionPattern=%-5p [%d{dd.MM.yy HH:mm:ss}] %C{1} - %m [thread: %t]\n
log4j.appender.StandaloneFile.threshold=info
log4j.rootLogger=info, Stdout, StandaloneFile
log4j.logger.com.myPacke.package1=info, Stdout, StandaloneFile
log4j.logger.com.myPacke.package2=DEBUG
log4j.appender.Stdout.threshold=info
Should be:
log4j.appender.Stdout.threshold=debug
You just set the console threshold to be info, so you're not getting debug level logs.
Be aware you also set the RollingFileAppender threshold to info as #Stephen C commented.
Make sure your configuration has below appender...We have used log4j.xml so i am adding from xml
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<param name="Threshold" value="info" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<appender name="logfile" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="log/dcm_migration.log" />
<param name="Append" value="false" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c{1}] %m %n" />
</layout>
</appender>

Categories

Resources