log4j writing downsteam's logs to a file - log4j2

I have a java application. I have created a log4j2.xml under src folder and that works as expected.
This java application depends on another java package. I tried creating a similar log4j2.xml in this package to format all the logging done in package, But this does not seem to work. And all the logs in the package seems to be going to console in default formatting.
Thoughts?

In this case you need to have different Appenders
SO the config will be as below
<!-- Package 1 appender declaration -->
<appender name="one" class="org.apache.log4j.FileAppender">
...
</appender>
<!-- Package 2 appender declaration -->
<appender name="two" class="org.apache.log4j.FileAppender">
...
</appender>
<!-- logger package com.myapp.app.one -->
<logger name="com.myapp.app.one">
<level value="DEBUG" />
<appender-ref ref="one"/>
</logger>
<!-- logger for package com.myapp.app.two -->
<logger name="com.myapp.app.two">
<level value="DEBUG" />
<appender-ref ref="two"/>
</logger>

Related

log4j2 TimeBasedTriggeringPolicy not working as expected

I have configured the log4j2.xml file in such a way that application.log file will be created and it should be rollover daily.
<?xml version="1.0" encoding="UTF-8"?>
<!-- ===================================================================== -->
<!-- Log4j2 Configuration -->
<!-- ===================================================================== -->
<Configuration status= "INFO">
<!-- Common properties used in all appenders -->
<Properties>
<Property name="logBaseDirectory">/apps/wsserver/8.5/bpm/logs/log4j/sbl</Property>
<Property name="logPattern">%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5p %c{1} %X{transaction.id} %m%n</Property>
<Property name="maxFileAge">90d</Property>
</Properties>
<!-- Define required appenders -->
<Appenders>
<!-- ============================================================================================================================================ -->
<!-- log4j 1.x to 2.x migratoin steps -->
<!-- DailyRollingFileAppender rolling file appender is no longer availble in log4j2. So we need to use RollingFile with TimeBasedTriggeringPolicy -->
<!-- Right most value in filePattern will be considered as Interval for TimeBasedTriggeringPolicy rolling -->
<!-- Orignal log file will be application.log and archive will be application-20200917.log.zip - Auto compression of file -->
<!-- Remove the all archived logs which are older than 90 days -->
<!-- ============================================================================================================================================ -->
<RollingFile name="application" fileName="${logBaseDirectory}/application.log" filePattern="${logBaseDirectory}/application-%d{yyyy-MM-dd}.log.zip">
<PatternLayout pattern="${logPattern}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy />
<CronTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${logBaseDirectory}" maxDepth="1">
<IfFileName glob="application-*.log.zip" />
<IfLastModified age="${maxFileAge}" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<!-- Define the list of loggers required -->
<Loggers>
<logger name="MediationServices" level="TRACE" additivity="false">
<appender-ref ref="application" />
</logger>
<root level="TRACE" additivity="false">
<appender-ref ref="application" />
</root>
</Loggers>
</Configuration>
But in JVM, applicatoin.log file is getting rollover after 10MB and if three times it is rolled, the first file is getting overwritten. That means at any point of time i am having application.log and application-2020-10-16.log.zip.
Why log4j2 (v2.13) is rolling over the files for every 10MB even though configured as daily? Any pointers identifying issue in log4j2 configuration is much apricated.
Issue has been identified. AS <SizeBasedTriggeringPolicy /> is defined in configuration file, log4j2 is considering 10MB default value as file size and getting rollover. After removing this tag, issue has been resolved.

Log4j2 daily log files

I want my log files created by log4j2 to have the date pattern in their file name, including the current active file. That is, if todays date is 2016-12-15, I want the current log-file to be lager-2016-12-15.log. When the date changes, I want a new file to be created named lager-2016-12-16.log.
With RollingFileAppender I am not able to get the current active log file to have date pattern in the filename. My Log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="layoutPattern">%d{ISO8601} [%t] %-5p [%X{REQUEST_ID}] [%X{CLIENT_ID}] [%X{USER_ID}] %c- %m%n</Property>
<Property name="logDir">${sys:catalina.home}/logs/</Property>
<Property name="fileName">${logDir}lager-${date:yyyy-MM-dd-HHmm}.log</Property>
<Property name="filePattern">${logDir}lager-%d{yyyy-MM-dd-HHmm}.log</Property>
</Properties>
<Appenders>
<RollingFile name="LAGER" append="true"
fileName="${fileName}"
filePattern="${filePattern}">
<PatternLayout pattern="${layoutPattern}" charset="UTF-8"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
</Appenders>
<loggers>
<Logger name="org.apache.catalina.core.ContainerBase.[Catalina].[localhost]" level="WARN" additivity="false">
<AppenderRef ref="LAGER" />
<AppenderRef ref="CONSOLE"/>
</Logger>
<Root level="INFO">
<AppenderRef ref="LAGER"/>
<AppenderRef ref="CONSOLE"/>
</Root>
</loggers>
</Configuration>
With this config the copying when it rolls over is messed up. Removing the date pattern from the fileName property fixes this, but the the current file does not have the date in its name.
I am running this on a tomee 7.0.1.
There may already be an outstanding feature request for this. Would this match your requirements? https://issues.apache.org/jira/browse/LOG4J2-1101
If so, please comment on that JIRA ticket. If you're able to contribute a patch (ideally with unit test) it is likely to get resolved quickly.

Log4j2 RollingFile appenders clashing

Log4j2 RollingFile appenders clashing
Below is a simplified debug version of our log4j2 configuration file (We rollover nightly not every minute!).This configuration, instead of it creating a Rollover file each minute (as per theTimeBasedTriggeringPolicy) will create one rollover file (non-tarred), containing JSON formatted logging, which will be overwritten every 20KB(although it will end up being slightly greater than 20KB (See Screenshot).We also get the following errors (abbreviated with "..."):-
2016-10-07 08:47:34,433 default-workqueue-4 ERROR Unable to copy file /.../logs/logFile-2016-10-07-08:47:11.log to /.../logs/logFile-2016-10-07-08:47:11.log: java.nio.file.NoSuchFileException /.../logs/logFile-2016-10-07-08:47:11.log
If we switch the order of the timeBasedRollingFileJsonLayout appender and the sizeBasedRollingFilePatternLayoutWithZippedArchive appender then no rollover occurs at all.
If we remove the sizeBasedRollingFilePatternLayoutWithZippedArchive appender then the timeBasedRollingFileJsonLayout appender works as expected.
We have the two different appenders for different environments, where the logs may or may not be hooked up to ELK.In our real log4j2 config file we use properties to select the appropriate appender for the environment.I have removed the properties from this file for clarity and to rule them out as a possible cause of the problem.
We are using log4j 2.6.2.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %logger{36} - %msg %n" />
</Console>
<RollingFile name="timeBasedRollingFileJsonLayout" append="true" fileName="logs/logFile.log" filePattern="logs/logFile-%d{yyyy-MM-dd-HH:mm}.log">
<JSONLayout properties="true" compact="true" eventEol="true" />
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
<RollingFile name="sizeBasedRollingFilePatternLayoutWithZippedArchive" append="true" fileName="logs/logFile.log" filePattern="logs/logFile-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %logger{36} - %msg %n" />
<Policies>
<SizeBasedTriggeringPolicy size="20KB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<logger name="logger.one" level="info" additivity="false">
<AppenderRef ref="timeBasedRollingFileJsonLayout" />
</logger>
<Logger name="logger.two" level="info" additivity="false">
<AppenderRef ref="timeBasedRollingFileJsonLayout" />
</Logger>
<Logger name="logger.three" level="info" additivity="false">
<AppenderRef ref="timeBasedRollingFileJsonLayout" />
</Logger>
<Root level="info">
<AppenderRef ref="timeBasedRollingFileJsonLayout" level="all" />
</Root>
</Loggers>
</Configuration>
I am at a loss to understand why you think this should work. You have two appenders trying to write to the same file trying to rollover based on different criteria and rollover to files with different names. It is no surprise that you are getting the file is in use error since two things have it open at once.

How to set LogLevel to error in log4net for hangfire

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>

netbeans glassfish hibernate log4j2

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.

Resources