Rolling log files issue with Spark - java

I have a rolling log file setup in my log4j.xml file. The logs are getting updated properly in the log file location i setup for. But, the logs are not getting archived properly and so I am missing the old log data.
Given below is my log4j.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<!--<Property name="log-path">/adshome/rcast31/logs</Property>-->
<Property name="log-path">/gmdl/grd/rawData/logs</Property>
<Property name="grd-log-name">grd-spark-info</Property>
<Property name="archive">${log-path}/archive</Property>
<Property name="retentionAge">45d</Property>
</Properties>
<Appenders>
<RollingFile name="info-image" fileName="${log-path}/${grd-log-name}.log"
filePattern="${archive}/${grd-log-name}-%d{yyyy-MM-dd}.log">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L (%t) - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${archive}" maxDepth="1">
<IfFileName glob="${grd-log-name}*.log*" />
<IfLastModified age="${retentionAge}" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.aexp.grd" level="INFO" additivity="false">
<appender-ref ref="info-image"/>
</Logger>
<Root level="INFO">
<AppenderRef ref="info-image" level="INFO"/>
</Root>
</Loggers>
</Configuration>
I see separate log files with older dates are created but it doesn't have that day's logging information in it. Either is is empty or only getting rolled up partially. Please let me know what I am missing!

Related

I changed spring boot app to spring mvc but my log4j2.xml is not working?

I change spring boot to spring MVC, but I can't see any log information in my log file but it populates in the console. Please let me know the changes I need to make in the log4j2.xml file so that log info will be saved to log files.
what is monitorinterval=30 in the below XML file?
Thank you soo much for the response
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Properties>
<Property name="LOG_PATTERN">
%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} --- [%15.15t] %-40.40c{1.} : %m%n%ex
</Property>
</Properties>
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${LOG_PATTERN}"/>
</Console>
<!-- Rolling File Appender -->
<RollingFile name="FileAppender" fileName="logs/prism.log"
filePattern="logs/prism-%d{yyyy-MM-dd}-%i.log.zip">
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5 MB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.heymath" level="debug"
additivity="false">
<AppenderRef ref="ConsoleAppender"/>
<AppenderRef ref="FileAppender"/>
</Logger>
<Root level="info">
<AppenderRef ref="ConsoleAppender"/>
<AppenderRef ref="FileAppender"/>
</Root>
</Loggers>
</Configuration>
monitorInterval=30 tells Log4j2 to check every 30 seconds to see if the logging configuration has changed and reconfigure if it has.
I see nothing obviously wrong with your configuration. It should be logging all info, warn, error and fatal messages to the file and console. Debug messages from com.heymath loggers should be going there as well.
I would suggest changing the status="WARN" to status="DEBUG" on the configuration element. If you do not see logs from log4j it means that your configuration file was not found and is not being used. Instead, it is using the default configuration which logs errors to the console.

log4j is logging in the console but not creating a log file

I am using log4j in my project. The project is working fine but it is not creating a log file.
Here is the log4j.xml
<Configuration status="warn">
<Properties>
<Property name="logHome">D:/logs</Property>
</Properties>
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%-5p: %c - %m%n" />
</Console>
<RollingFile name="rollingFile" immediateFlush="true" fileName="${logHome}/mySample.log" filePattern="${logHome}/mySample-%i.log">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} |[%t]| %-5p | %c{1} | %L - %m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5 MB" />
</Policies>
<DefaultRolloverStrategy max="10" />
</RollingFile>
</Appenders>
<Loggers>
<Logger name="org.springframework" level="info" includeLocation="true" />
<Logger name="org.hibernate" level="info" includeLocation="true" />
<Logger name="org.project" level="debug" includeLocation="true" />
<Root level="info" includeLocation="true">
<AppenderRef ref="rollingFile" />
<AppenderRef ref="console" />
</Root>
</Loggers>
</Configuration>
Any help is appreciated. Thanks in advance.
I tried all the ways out but nothing worked for me. At the end I had to switch to slf4j.Logger and slf4j.LoggerFactory. With the help of this I was able to generate logs.
Example:
private static final Logger LOGGER=LoggerFactory.getLogger(MyClass.class);

Log4j2.xml doesn't create file suddenly in java hibernate framework

I have a maven project in NetBeans with hibernate framework. For a long time it was doing its job correctly. After some modification in database, console log is still working but writing to file has stopped without any error or exception. I have deleted log file directory and restart project, but this time it couldn't even create file path directory.
here is my log4j2.xml configuration(in src/main/resources)
I have changed path to D:\logs and D:/logs, and tried different levels (debug, info, error). Also tried to run my "Server.jar" from command line by administrator. And it should be mentioned that I tried the solution in Log4J creates log file but does not write to it and lots of other suggested solutions, but no success achieved.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug">
<Properties>
<Property name="path">${sys:user.home}/logs/server</Property>
</Properties>
<Appenders >
<RollingFile name="connections" append="true" fileName="${path}/connections.log"
filePattern="${path}/connections-%d{yyyy-MM-dd}-%i.log" >
<!-- log pattern -->
<PatternLayout>
<pattern> %d{yyyy-MM-dd HH:mm:ss} [%-4level] - %msg %n</pattern>
</PatternLayout>
<!-- set file size policy -->
<Policies>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="unexpected-events" append="true" fileName="${path}/unexpected-events.log"
filePattern="${path}/unexpected-events-%d{yyyy-MM-dd}-%i.log" >
<!-- log pattern -->
<PatternLayout>
<pattern> %d{yyyy-MM-dd HH:mm:ss} [%-4level] - %msg %n</pattern>
</PatternLayout>
<!-- set file size policy -->
<Policies>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="readouts" append="true" fileName="${path}/readouts.log"
filePattern="${path}/readouts-%d{yyyy-MM-dd}-%i.log" >
<!-- log pattern -->
<PatternLayout>
<pattern> %d{yyyy-MM-dd HH:mm:ss} [%-4level] - %msg %n</pattern>
</PatternLayout>
<!-- set file size policy -->
<Policies>
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="connections" level="info" additivity="false">
<appender-ref ref="connections" level="debug"/>
<!--<appender-ref ref="console-log" level="debug"/>-->
</Logger>
<Logger name="unexpected-events" level="info" additivity="false">
<appender-ref ref="unexpected-events" level="debug"/>
<!--<appender-ref ref="console-log" level="debug"/>-->
</Logger>
<Logger name="readouts" level="info" additivity="false">
<appender-ref ref="readouts" level="debug"/>
<!--<appender-ref ref="console-log" level="debug"/>-->
</Logger>
<Root level="error" additivity="false">
<!--<AppenderRef ref="console-log"/>-->
</Root>
</Loggers>
</Configuration>
and this is how I used it in my java class:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
......
private static final Logger connectionsLog = LogManager.getLogger("connections");
....
connectionsLog.info("device" + deviceNumber + " disconnected");
I have seen weirdness like this when both commons-logging and jcl-over-slf4j are on the classpath together. Excluding commons-logging from the Maven classpath, anywhere it appears, often helps.
By special thanks to user944849 , I understood that the problem could be related to maven dependencies. Then I checked previous version of my project (which has correct logging) and compare dependencies and found out that a library "log4j-to-slf4j" is added (which has added because of importing "org.springframework.boot"). Then first I exclude it from dependency in pom.xml file, as a result it worked fine in NetBeans IDE but not out of it. Then I removed that dependency from pom.xml and problem solved like a charm!

log4j2 unable to delete old files

I have searched everywhere for the answer. I have to delete files after the number of file archived is more than 10.
This is my log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="C:/temp/logs/app.log"
filePattern="C:/temp/logs/app.log.%i">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="1000 kb" />
</Policies>
<DefaultRolloverStrategy max="10">
<Delete basePath="C:/temp/logs/" maxDepth="1">
<IfFileName glob="app*.log*">
<IfLastModified age="2m">
<IfAny>
<IfAccumulatedFileCount exceeds="11" />
</IfAny>
</IfLastModified>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>
</Configuration>
But it does not delete all the files in the folder. For example in the screenshot below, it does not delete the old log file:
logs
I have gone through all the documentation and lots of discussions but I couldn't solve this. Any help would be appreciated.
This is a similar question to one asked and might provide some help on what you are trying to do = Log4j2 - Configure RolloverStrategy to delete old log files.
The comments should explain what each condition under Delete would do. In your example, if you like app-log-12-20* to be deleted, then you need to either remove either the IfLastModified or IfAccumulatedFileCount - you will see the file getting deleted
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="C:/temp/logs/app.log"
filePattern="C:/temp/logs/app.log.%i">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="1000 kb" />
</Policies>
<DefaultRolloverStrategy max="10">
<Delete basePath="C:/temp/logs/" maxDepth="1">
<IfAll>
<!-- Looks for file names starting with app*.log*. Any file including app.log.1 will satisfy this condition. Could delete current log app.log -->
<IfFileName glob="app*.log*" />
<!-- Looks for files that are older than 2 mins -->
<IfLastModified age="2m" />
<!-- This means there need to be 11 fails that satisfy the above conditions, that i.e. their name is app*log* and have time stamp greater than 2 mins. Keeps the most recent files that satisfy the condition -->
<IfAccumulatedFileCount exceeds="11" />
</IfAll>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>

issue with my log4j2 setup

I was using log4j2 and it was logging statements for me with no issues. I might have made some changes (moved from info to debug and back) but its quite possibly I might have messed up the config in some other fashion. I am copying my config file below (I have not moved any log4j2 and slf4j jar files from my project). Any thoughts?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug" name="TestApp" packages="">
<Appenders>
<RollingRandomAccessFile name="RollingRandomAccessFile" fileName="logs/test.log" immediateFlush="false" append="false"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="500 MB"/>
</Policies>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<AsyncLogger name="FATAL_LOGGER" level="fatal" includeLocation="true" additivity="false">
<AppenderRef ref="RollingRandomAccessFile"/>
</AsyncLogger>
<Root level="debug" includeLocation="false">
<AppenderRef ref="RollingRandomAccessFile"/>
</Root>
</Loggers>
</Configuration>
I tested the config in a test project and it works without any issues. Ensure that you do not have any locks on the file and have correct read/write privileges on the file/directory.

Categories

Resources