Having problem with integrating OpenJPA with log4j2.
OpenJPA + log4j worked fined
<appender name="OPENJPA_LOG" class="org.apache.log4j.RollingFileAppender">
<param name="maxFileSize" value="5MB" />
<param name="maxBackupIndex" value="10" />
<param name="file" value="./logs/openjpa.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c:%L - %m%n" />
</layout>
</appender>
<logger name="openjpa.jdbc.SQL" additivity="false">
<level value="TRACE" />
<appender-ref ref="OPENJPA_LOG" />
</logger>
But it is not working with log4j2.
<RollingFile name="OPENJPA_LOG" fileName="./logs/openjpa.log"
append="true" filePattern="./logs/openjpa.%i.log.gz"
ignoreExceptions="false">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} - %c [%thread] - %m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB" />
</Policies>
<DefaultRolloverStrategy max="5" />
</RollingFile>
<Logger name="openjpa.jdbc.SQL" level="TRACE" additivity="false" >
<AppenderRef ref="OPENJPA_LOG" />
</Logger>
How to fix it.
If you still have issues with this you have to double check that in the persistence.xml you set <property name="openjpa.Log" value="slf4j"/>.
Indeed open jpa is not compatible with log4j2 natively so if you use <property name="openjpa.Log" value="log4j"/> you will run into a java.lang.ClassNotFoundException: org.apache.log4j.Priority.
Once openjpa logs are route properly to slf4j it is ok. I hope your application use sllf4j.
Be sure to use proper versions of log4j2 slf4j and bridge and remove all previous log4j1.x and old slf4j dependencies.
Related
1)I want to generate the log file on daily basis using log4j2.xml. I am using the RollingFile appender tag to achieve the same.However, it is appending the logs on the same file.
<RollingFile name="FrameworkAppender" filePattern="${jboss.server.home.dir}/xyz_app-%d{yyyy-MM-dd}-%i.log" >
<PatternLayout>
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</PatternLayout>
<LevelRangeFilter minLevel="ERROR" maxLevel="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
<Policies>
<CronTriggeringPolicy schedule="0 0 0 * * ?" />
<SizeBasedTriggeringPolicy size="250 MB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
2)Below is the category tag we have used in log4j.xml ,i want to know what is the equivalent of category tag in log4j2.xml ?
<category name="biz.theXYZ" additivity="false">
<level value="INFO" />
<appender-ref ref="FrameworkAppender" />
<appender-ref ref="ConsoleAppender" />
</category>
3)How to define SNMPTrapAppender with all the properties in log4j2?
<appender name="TRAP_LOG_APPENDER" class="org.apache.log4j.ext.SNMPTrapAppender">
<param name="ImplementationClassName" value="org.apache.log4j.ext.JoeSNMPTrapSender"/>
<param name="EnterpriseOID" value="1.3.6.1.4.1.2854.1"/>
<param name="ApplicationTrapOID" value="1.3.6.1.4.1.24.12.10.22.64"/>
<param name="ManagementHost" value="127.0.0.1"/>
<param name="ManagementHostTrapListenPort" value="162"/>
<param name="LocalIPAddress" value="127.0.0.1"/>
<param name="LocalTrapSendPort" value="161"/>
<param name="GenericTrapType" value="6"/>
<param name="SpecificTrapType" value="12345678"/>
<param name="CommunityString" value="public"/>
<param name="ForwardStackTraceWithTrap" value="true"/>
<param name="Threshold" value="INFO"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d,%p,[%t],[%c],%m%n"/>
</layout>
</appender>
you should add
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
For the second question, the equivalent in log4j2 is:
<Loggers>
<Logger name="biz.theXYZ" level="info">
<AppenderRef ref="FrameworkAppender"/>
<AppenderRef ref="ConsoleAppender"/>
</Logger>
</Loggers>
Make sure to include the FrameworkAppender and ConsoleAppender in the Appenders section of your log4j2 file.
I have the following log4j2 xml configuration that I'm using to log TRACE and DEBUG level messages.
<Configuration status="WARN" packages="" monitorInterval="300">
<Properties>
<Property name="log-path">/logs/webapp</Property>
</Properties>
<Appenders>
<RollingFile name="TRACE" append="true" immediateFlush="true" fileName="${log-path}/trace.log" filePattern="${log-path}/trace.log.%d{MM-dd-yyyy}">
<PatternLayout>
<pattern>%d{DEFAULT}: %-20C{1} : %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${log-path}" maxDepth="1">
<IfFileName glob="*/trace.log.*" />
<IfLastModified age="14d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<RollingFile name="DETAIL" append="true" immediateFlush="true" fileName="${log-path}/detail.log" filePattern="${log-path}/detail.log.%d{MM-dd-yyyy}">
<PatternLayout>
<pattern>%d{DEFAULT}: %-20C{1} : %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy max="15"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="TRACE" level="trace"/>
<AppenderRef ref="DETAIL" level="debug"/>
</Root>
</Loggers>
</Configuration>
The intention for this configuration is that the trace log files 14 days and older would be deleted but this is not the case. I have much older trace log files on my system still, the oldest being trace.log.11-02-2016
I'm aware of file deletion issues in log4j2 up to version 2.5 and 2.5 introduced the Delete configuration element but I am using 2.6.2.
Has anyone come across this and been able to get it to work either via an implementation version update or a configuration change?
We have a web application and we are using log4net for logging. Recently I installed hangfire and now my log table (sqlserver db) is full of log enteries from hangfire. I want to minimise this to error and exceptions only. I believe I can set it by setting LogLevel to error but where exactly I need to do that.
Thanks
Add this logger to your web.config (log4net section)
<logger additivity="false" name="Hangfire">
<level value="ERROR" />
<appender-ref ref="HangfireLoggerAppender" />
</logger>
Adding to what #Martino mentioned above here is the exact location to add that tag in log4net section:
<log4net debug="true">
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
<file type="log4net.Util.PatternString" value="%property{LogFileName}" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %level %logger - %message%newline" />
</layout>
</appender>
<logger additivity="false" name="Hangfire">
<level value="ERROR" />
<appender-ref ref="HangfireLoggerAppender" />
</logger>
</log4net>
I'd like to configure log4j 2 with two different patterns in same appender. i.e., Whenever there is an error, a specific pattern should be present in the log file. I am not trying two different log files, but two different pattern in same log file. Whenever there is an error, I would see "MYDOMAINDOTCOM_SUPPORT_NEEDED" and this string will trigger an automatic email to support team.
I have the below configuration which prints error message in "RollingFile" appender only and "RollingFileError" appender is ignored. What am I missing ?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
<Properties>
<Property name="log-path">/documents/log</Property>
</Properties>
<Appenders>
<RollingFile name="RollingFile" fileName="${log-path}/myexample.log"
filePattern="${log-path}/$${date:yyyy-MM}/myexample-%d{yyyy-MM-dd}-%i.log"
immediateFlush="true">
<PatternLayout>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %X{packetRefId} - %msg%n</pattern>
<!-- %d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n -->
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="100 KB" />
</Policies>
<DefaultRolloverStrategy max="4" />
</RollingFile>
<RollingFile name="RollingFileError" fileName="${log-path}/myexample.log"
filePattern="${log-path}/$${date:yyyy-MM}/myexample-%d{yyyy-MM-dd}-%i.log"
immediateFlush="true">
<param name="threshold" value="error" />
<PatternLayout>
<pattern>MYDOMAINDOTCOM_SUPPORT_NEEDED %d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %X{packetRefId} - %msg%n</pattern>
<!-- %d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n -->
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="100 KB" />
</Policies>
<DefaultRolloverStrategy max="4" />
</RollingFile>
</Appenders>
<Loggers>
<Logger name="org.springframework.beans.factory" level="info" additivity="false">
<AppenderRef ref="Console"/>
</Logger>
<Logger name="root" level="debug" additivity="false">
<appender-ref ref="RollingFile" level="debug" />
<appender-ref ref="RollingFileError" level="error" />
</Logger>
<Root level="debug" additivity="false">
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>
</Configuration>
You should never configure two appenders to write to the same file. Having two rolling file appenders that both use the same file and roll over to the same file pattern is never going to work correctly.
Also, your configuration would end up with all error messages being logged twice; once with the RollingFile appender due to its debug level, and once on the RollingFileError appender due to its error level.
Instead, you should have a single rolling file appender and use a PatternSelector to decide which pattern to use. See http://logging.apache.org/log4j/2.x/manual/layouts.html#Pattern_Selectors for documentation on pattern selectors.
I have a simple Netbeans 7.1.2 (NON MAVEN) project that use glassfish 3.1 server for testing.
I created a log4j2.xml file and placed it on the classpath
here it is
<?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">
<param name="Threshold" value="debug"/>
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} [%t] %-5p %c{1} - %m%n"/>
</layout>
</appender>
<appender name="rolling-file" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="c:\tmp\Program-Name.log"/>
<param name="MaxFileSize" value="500KB"/>
<param name="MaxBackupIndex" value="4"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %l - %m%n"/>
</layout>
</appender>
<logger name="org.hibernate">
<level value="info" />
</logger>
<root>
<priority value ="debug" />
<appender-ref ref="console" />
<appender-ref ref="rolling-file" />
</root>
</log4j:configuration>
the project uses hibernate to store data from a web service to a database.
However I am not able to log anything. I can see the logs of hibernate into the Netbeans IDE but I cannot see the created on the filesystem file.
I have this error when calling the web service
SEVERE: ERROR StatusLogger Unknown object "logger" of type org.apache.logging.log4j.core.config.LoggerConfig is ignored
SEVERE: ERROR StatusLogger root contains an invalid element or attribute "priority"
SEVERE: ERROR StatusLogger Unknown object "root" of type org.apache.logging.log4j.core.config.LoggerConfig is ignored
could someone pls help or give some advice I googled and stackoverflowed but without chance.
Paolo
I think that your log4j2.xml file is mixed with the old log4j style xml.
I'm having the same trouble as you when it comes to appending hibernate log to my application's log file. But I think that I can help with the logger error.
Try this file instead (and note the changes that I made):
<?xml version="1.0" encoding="UTF-8" ?>
<configuration name="SOME_PROJ_NAME" status="OFF">
<appenders>
<RollingFile name="rolling-file" fileName="c:/tmp/Program-Name.log" filePattern="c:/tmp/$${date:yyyy-MM}/Program-Name-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<pattern>%d{ABSOLUTE} [%t] %-5p %c{1} - %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="6" modulate="true"/>
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
</RollingFile>
</appenders>
<loggers>
<root level="info"
<appender-ref ref="rolling-file"/>
</root>
<logger name="org.hibernate level="info">
<appender-ref ref="rolling-file"/>
</logger>
</loggers>
</configuration>
It is quite similar to the one that I'm using. Note that this file should be on classpath for log4j2 auto configuration to kick in.
The most important thing you should do when using the net to configure log4j2 is that you're looking at log4j2 and not log4j style configuration.
I suggest that you look in http://logging.apache.org/log4j/2.x/ for further info. They have a good downloadable pdf that you can check out.
In addition, check out my question about a similar problem.