Making log4j write the same logs to multiple files - java

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

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

log4j rolling appender issue for multiple log file creation

I am using log4j for my application for printing logs. Till now, I have only one log file(say X.log) and it is getting rolled after a fixed size, say, 4 MB. Now , I am trying to log some particular requests and responses to a different log file, Y. Log.
My requirement is to roll the Y.log file also, after 4 MB. I am using rolling file appender for both X and Y logs. Y.log will be created in the same location where X.log is available.
But it seems like now both the log files are not getting rolled over after 4 MB. It is keep on appending to the same logs and the size is increasing. Below is my log4j configuration.
<appender name="Xlogfile" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="true" />
<param name="file" value="/logs/X.log" />
<param name="MaxBackupIndex" value="200" />
<param name="MaxFileSize" value="4MB" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n"/>
</layout>
</appender>
<appender name="Ylogfile" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="true" />
<param name="file" value="/logs/Y.log" />
<param name="MaxBackupIndex" value="200" />
<param name="MaxFileSize" value="4MB" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n"/>
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref=" Xlogfile " />
<appender-ref ref=" Ylogfile " />
</root>
Could anyone please provide me a solution for this. Thanks in advance.
Try adding a policy to your appender, like this:
<Policies>
<SizeBasedTriggeringPolicy size="4mb"/>
</Policies>

log4j : generate log file name with parameters

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 ?

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

Logback different files for different levels

I have this appender in my logback.xml
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>logFile.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>logFile.%d{yyyy-MM-dd}.log</FileNamePattern>
<maxHistory>5</maxHistory>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{35} - %msg%n</Pattern>
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="FILE" />
</root>
So at the moment I am saving all my logs to one file. How can I make it like, one file saves only error logs and other saves all other?
And I want to keep using only 1 instance of logger in my code, something like this:
private static final Logger log = LoggerFactory.getLogger(Main.class);
Started looking for logback categories, found filters.
Just add filter description to your appender:
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
This is what I do for Jboss but I guess you'll get the solution.
Create a dedicated appender for error.log as follows,
<appender name="ErrorFile" class="org.jboss.logging.appender.DailyRollingFileAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="File" value="${jboss.server.log.dir}/error.log"/>
<param name="Threshold" value="ERROR"/>
<param name="Append" value="false"/>
<!-- Rollover at midnight each day -->
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<!-- Rollover at the top of each hour
<param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
-->
<layout class="org.apache.log4j.PatternLayout">
<!-- The default pattern: Date Priority [Category] Message\n -->
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
<!-- The full pattern: Date MS Priority [Category] (Thread:NDC) Message\n
<param name="ConversionPattern" value="%d %-5r %-5p [%c] (%t:%x) %m%n"/>
-->
</layout>
</appender>
Create an appender for the rest of them
<appender name="File" class="org.jboss.logging.appender.DailyRollingFileAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="File" value="${jboss.server.log.dir}/server.log"/>
<param name="Threshold" value="ALL"/>
<param name="Append" value="false"/>
<!-- Rollover at midnight each day -->
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<!-- Rollover at the top of each hour
<param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
-->
<layout class="org.apache.log4j.PatternLayout">
<!-- The default pattern: Date Priority [Category] Message\n -->
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
<!-- The full pattern: Date MS Priority [Category] (Thread:NDC) Message\n
<param name="ConversionPattern" value="%d %-5r %-5p [%c] (%t:%x) %m%n"/>
-->
</layout>
</appender>
Then create specific categories for both the above
<category name="your_file_name/package_name">
<priority value="ERROR"/>
<appender-ref ref="ErrorFILE"/>
</category>
<category name="your_file_name/package_name">
<priority value="ALL"/>
<appender-ref ref="File"/>
</category>

Categories

Resources