Log4j disabling console logging - java

I need to make logger that will only log to file, but when I set XML likie this:
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<param name="maxFileSize" value="1MB" />
<param name="maxBackupIndex" value="5" />
<param name="file" value="${SERVER_HOME}/logs/mylog-log.txt" />
<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>
<logger name="com.packageforlogging" additivity="true">
<level value="DEBUG" />
<appender-ref ref="file" />
</logger>
It stills logs into console, because there are other loggers to which i don't have access. How I can do it?

Related

log4j issue: redirect logg to many files

Actually i am using log4j 1.2 with an xml configration file;
Is it possible to redirect the log of each class in a specified file; i want to logg in 3 files.
<appender name="file" class="org.apache.log4j.FileAppender" >
<!-- <param name="Threshold" value="INFO" /> -->
<param name="File" value="log/archive.log" />
<param name="Append" value="true" />
<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>
<appender name="file" class="org.apache.log4j.FileAppender" >
<!-- <param name="Threshold" value="INFO" /> -->
<param name="File" value="log/check.log" />
<param name="Append" value="true" />
<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>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="DEBUG" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %c{1}: %m [%L]%n" />
</layout>
</appender>
<logger name="org.apache.axis">
<level value="DEBUG"/>
</logger>
<root>
<priority value ="DEBUG"/>
<appender-ref ref="file" />
<appender-ref ref="console" />
</root>
you can file solution with log4j: Log output of a specific class to a specific appender

How to make logger.trace() and logger.info() work together

I have an issue logging TRACE function in distinct file from INFO function, i prepared a classic log4j.xml file containing all the required configuration.
logger.info() is working.
logger.trace() is not working.
<appender name="file" class="org.apache.log4j.FileAppender">
<param name="append" value="true" />
<param name="file" value="C:/logs/transaction.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="TRACE" />
<param name="LevelMax" value="TRACE" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<appender name="fileINFO" class="org.apache.log4j.FileAppender">
<param name="append" value="true" />
<param name="file" value="C:/logs/server.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<logger name="ma.prg.net">
<level value="trace" />
<appender-ref ref="file" />
</logger>
<logger name="ma.prg.net">
<level value="info" />
<appender-ref ref="fileINFO" />
</logger>
logger.trace() was working fine before i added fileINFO appender.
Thanks in advance.
You cannot configure the same logger twice. Just change your logger configuration to:
<logger name="ma.prg.net">
<level value="trace" />
<appender-ref ref="file" />
<appender-ref ref="fileINFO" />
</logger>
With this you send the logging message to both appenders. But as you already configured each appender individually with LevelMin and LevelMax, each filters out the messages it should print.

Log4j logging two times in logging file

Here is my log4j.xml
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="mylog" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="myLogfile.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<param name="append" value="true"/>
<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>
<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>
<logger name="com.mypackage1">
<level value="DEBUG" />
<appender-ref ref="mylog" />
<appender-ref ref="console" />
</logger>
<root>
<level value="DEBUG" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
in myLogfile.log file i am getting the logging from "com.mypackage1" two times.
Please help put with the issue
If you are redirecting your console output to the same log file from shell, like you mentioned in the comment, then yes, your log file will have duplicate entries. One solution would be to use below log4j config in your program and redirect the console output to a different file - e.g. console.out, that will have your logs and std out separate. If you must have std out in the same file, then redirect std out to the same file.
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="mylog" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="myLogfile.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<param name="append" value="true"/>
<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>
<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>
<logger name="com.mypackage1">
<level value="DEBUG" />
<appender-ref ref="mylog" />
<!-- <appender-ref ref="console" /> -->
</logger>
<root>
<level value="DEBUG" />
<!-- <appender-ref ref="console" /> -->
<appender-ref ref="myLog" />
</root>
</log4j:configuration>

Spring WebFlow Project. I can't stop log4j from printing debug statements

I am working on a Spring WebFlow Project. I can't stop log4j from printing debug statements. can someone please tell me how to get it to stop putting debug statments on the console and log.
I see this warn as my server starts:
log4j:WARN Continuable parsing error 42 and column 11
log4j:WARN The content of element type "logger" must match "(level?,appender-ref*)".
Below is my log4j.xml file:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c - %m%n" />
</layout>
</appender>
<appender name="logfile" class="org.apache.log4j.FileAppender">
<param name="File" value="logs/disater_relief.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c - %m%n" />
</layout>
</appender>
<appender name="rollinglogfile" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="logs/school_visit.log" />
<param name="immediateFlush" value="true" />
<param name="append" value="true" />
<param name="maxFileSize" value="1MB" />
<param name="maxBackupIndex" value="3" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c - %m%n" />
</layout>
</appender>
<appender name="dailyrollinglogfile" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="./logs/school_visit.log" />
<param name="immediateFlush" value="true" />
<param name="append" value="true" />
<param name="datePattern" value=" '.' yyyy-MM-dd " />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c - %m%n" />
</layout>
</appender>
<appender name="email" class="org.apache.log4j.net.SMTPAppender">
<param name="BufferSize" value="512" />
<param name="SMTPHost" value="xxx" />
<param name="From" value="xx" />
<param name="To" value="xx" />
<param name="Subject" value="School Visit Web - SYSTEM ERROR" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>
<root>
<priority value="WARN" />
<appender-ref ref="email" />
<appender-ref ref="console" />
<appender-ref ref="dailyrollinglogfile" />
</root>
</log4j:configuration>
This is just a log4j issue. It appears that <priority> is not a valid sub-element of the logger element <root>. Rather, you should have <level> instead.

How to get log4j to log my firms classes only

Below is a code of the log4j.xml file we are using at my firm. We been trying to change it to log only output from classes with made that is in org.xxxx. can someone let me know if this can be done and how to do it
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="logfile" class="org.apache.log4j.FileAppender">
<param name="File" value="logs/disater_relief.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="rollinglogfile" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="logs/ennrollment.log" />
<param name="immediateFlush" value="true" />
<param name="append" value="true" />
<param name="maxFileSize" value="1MB" />
<param name="maxBackupIndex" value="3" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="dailyrollinglogfile" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="logs/ennrollment.log" />
<param name="immediateFlush" value="true" />
<param name="append" value="true" />
<param name="datePattern" value=" '.' yyyy-MM-dd "/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<root>
<priority value="debug" />
<appender-ref ref="console" />
<appender-ref ref="dailyrollinglogfile" />
</root>
</log4j:configuration>
Your root looger defines debug logging for all components. You should change it to warn (so you still see warnings) and add an extra logger for your own components:
<logger name="com.xxx.mycomponent">
<level value="trace"/>
<!-- appenders ... -->
</logger>
<root>
<priority value="warn" />
<appender-ref ref="console" />
<appender-ref ref="dailyrollinglogfile" />
</root>
Simply turn off the logging for the package you don't want to see in your logs...
<!-- Limit JBoss categories -->
<category name="org.jboss">
<priority value="OFF"/>
</category>

Categories

Resources