I'm trying to log to a file, if the logging is called from a specific package, and otherwise log to console. So I set the additivity to false for the two loggers which should be logging to file, so nothing would be logged on the console for these two packages. My log4j.xml is as follows:
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<param name="Threshold" value="TRACE"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} [%t] %-5p %c - %m%n"/>
</layout>
</appender>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<!-- Set your own log file location -->
<param name="File" value="c:/temp/security-toolkit.log" />
<!-- Specify the number of backup -->
<param name="MaxBackupIndex" value="10"/>
<!-- Specify the file size -->
<param name="MaxFileSize" value="10MB"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p[%X{event} %X{user_name}:%X{user_sid}#%X{user_last_host_address} -> /%X{application_name}/%X{module_name}] %m%n"/>
</layout>
</appender>
<root>
<priority value="INFO"/>
<appender-ref ref="console"/>
</root>
<logger name="com.foo.security" additivity="false">
<level value="INFO" />
<appender-ref ref="file"/>
</logger>
<logger name="org.foo.esapi" additivity="false">
<level value="WARN" />
<appender-ref ref="file"/>
</logger>
However, logs from com.foo.security are still being logged to the console. Does anyone know what the issue could be here?
Related
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>
I have a situation where I have 2 appenders, one writing to console, another to file. I'd like to log everything with DEBUG level except for one class.
For this class I want to log DEBUG to console and ERROR to file.
I figured out that this kind of filtering is done by category tag, however I haven't managed to split different levels to different appenders on a specific class.
is there a way to do it?
Current configuration is the following :
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="encoding" value="UTF-8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601}: %p [%t] %c - %m%n"/>
</layout>
</appender>
<appender name="file-bl" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="append" value="true"/>
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern" value="log/server-bl2.log.%d{yyyy-MM-dd}.gz"/>
<param name="ActiveFileName" value="log/server-bl2.log"/>
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601}: %p [%t] %c - %m%n"/>
</layout>
</appender>
<logger name="org">
<level value="DEBUG"/>
<appender-ref ref="console"/>
<appender-ref ref="file-bl"/>
</logger>
<logger name="org.springframework" additivity="false">
<level value="ERROR"/>
<appender-ref ref="file-bl"/>
</logger>
<root>
<priority value="DEBUG" />
<appender-ref ref="console" />
<appender-ref ref="file-bl" />
</root>
</log4j:configuration>
Ok, so in the end I came up with a solution like this :
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="encoding" value="UTF-8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601}: %p [%t] %c - %m%n"/>
</layout>
</appender>
<appender name="file-bl" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="append" value="true"/>
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern" value="log/server-bl.log.%d{yyyy-MM-dd}.gz"/>
<param name="ActiveFileName" value="log/server-bl.log"/>
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601}: %p [%t] %c - %m%n"/>
</layout>
</appender>
<logger name="org.springframework" additivity="false">
<level value="DEBUG"/>
<appender-ref ref="console"/>
</logger>
<logger name="org.mybatis" additivity="false">
<level value="DEBUG"/>
<appender-ref ref="console"/>
</logger>
<root>
<priority value="DEBUG" />
<appender-ref ref="console" />
<appender-ref ref="file-bl"/>
</root>
</log4j:configuration>
The root level of logging is DEBUG and there a 2 appenders : console and file-bl. Then, there 2 exceptions : org.springframework and org.mybatis.
For them only 1 appender applies - console.
Though, it's not an exact solution to the problem I described in the beginning since I don't log org.springframework and org.mybatis at all.
I am not able to see the SQL generated by MyBatis in JBoss server logs ,
I have added below in jboss-log4j.xml to enable it , it did work before but for some reason have stopped working. Is there any other configuration we need to do to enable it.
<logger name="org.apache.ibatis">
<level value="debug"/>
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="java.sql">
<level value="debug"/>
<appender-ref ref="CONSOLE"/>
</logger>
<logger name="java.sql.Statement">
<level value="debug"/>
<appender-ref ref="CONSOLE"/>
</logger>
<root>
<!--
Set the root logger priority via a system property. Note this is parsed by log4j,
so the full JBoss system property format is not supported; e.g.
setting a default via ${jboss.server.log.threshold:WARN} will not work.
-->
<priority value="${jboss.server.log.threshold}"/>
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
The problem was with the jboss-log4j.xml threshold value in CONSOLE appender ,
After changing INFO to DEBUG it worked without any issues
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="Target" value="System.out"/>
<param name="Threshold" value="INFO"/>
<layout class="org.apache.log4j.PatternLayout">
<!-- The default pattern: Date Priority [Category] Message\n -->
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
</layout>
</appender>
to
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="Target" value="System.out"/>
<param name="Threshold" value="DEBUG"/>
<layout class="org.apache.log4j.PatternLayout">
<!-- The default pattern: Date Priority [Category] Message\n -->
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
</layout>
</appender>
In a external file I would like to log the output of my com.foo.test class to a specific file but whats happening is its appending the console log as well as file log. Below is my xml configuration,
<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="Append" value="false"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
</layout>
</appender>
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="Target" value="System.out"/>
<param name="Threshold" value="INFO"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<appender name="MYTESTAPPENDER" class="org.jboss.logging.appender.RollingFileAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="File" value="${jboss.server.home.dir}/log/mytest.log"/>
<param name="Append" value="false"/>
<param name="MaxFileSize" value="3MB"/>
<param name="MaxBackupIndex" value="3"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
<logger name="testsplitlogger" additivity="false">
<level value="info"/>
<appender-ref ref="MYTESTAPPENDER"/>
</logger>
<root>
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
<appender-ref ref="MYTESTAPPENDER"/>
</root>
And My Java code is,
private static final Logger logger = Logger.getLogger("testsplitlogger");
Update:
The problem is its logging all the console output and other appenders output to mytest.log file instead I want to append only the info messages from my testsplitlogger for specific class and other logs from console and file should be written to server.log.
Am I doing anything wrong?
Please see the <root> xml config. You are allowing the log to be directed on all three different files.
CONSOLE (system.out)
FILE(in this case server.log) and
MYTESTAPPENDER(mytest.log)
To your requirement you need to define like this.
<root>
<priority value="info"/>
<!--<appender-ref ref="CONSOLE"/>-->
<!--<appender-ref ref="FILE"/>-->
<appender-ref ref="MYTESTAPPENDER"/>
</root>
EDIT:
It seems you have updated the question since I last posted... So my following suggestion has already been taken care of by you.
The testsplitlogger must contain the appender reference as well.
<logger name="testsplitlogger" additivity="false">
<level value="info" />
<appender-ref ref="MYTESTAPPENDER" />
</logger>
Here is a good link you can refer to http://wiki.apache.org/logging-log4j/Log4jXmlFormat
You need to create a Category for that:
<category name="my.company.package" additivity="false">
<priority value="info" />
<appender-ref ref="MYTESTAPPENDER" />
</category>
I have implement a log4j.xml file having two loggers.
Both EventLogger and ErrorLogger logged in to textfile. Also I create console appender and appended it to the root. My problem is when I use EventLogger or ErrorLogger it both write to the console and to the text file also. I need to avoid that. Can some one please help me.
Following I have showed you the xml file
<?xml version="1.0" encoding="UTF-8" ?>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="ErrorAPPENDER" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="./log/FIX5SvrError.log"/>
<param name="Append" value="true"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-4r [%t] %-5p %x - %m%n" />
</layout>
</appender>
<appender name="EventAPPENDER" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="./log/FIX5SvrEvent.log"/>
<param name="Append" value="true"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-4r [%t] %-5p %x - %m%n" />
</layout>
</appender>
<appender name="ConsoleAPP" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n"/>
</layout>
</appender>
<logger name="ErrorLogger">
<level value="debug"/>
<appender-ref ref="ErrorAPPENDER" />
</logger>
<logger name="EventLogger">
<level value="debug"/>
<appender-ref ref="EventAPPENDER" />
</logger>
<root>
<priority value ="debug" />
<appender-ref ref="ConsoleAPP" />
</root>
</log4j:configuration>
For the loggers whose output you don't want percolating up, you need to set additivity = false.
<logger name="ErrorLogger" additivity="false">
<level value="debug"/>
<appender-ref ref="ErrorAPPENDER" />
</logger>
<logger name="EventLogger" additivity="false">
<level value="debug"/>
<appender-ref ref="EventAPPENDER" />
</logger>
Search for additivity in the manual for more details.