Can someone explain me why Log4j2 is saving logs into .gz file? Why not .txt?
I've changed configuration in .xml file to save in .txt and it works, but I'm not sure if it's proper way to use logs?
EDIT
My log4j2.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" disableAnsi="false" />
</Console>
<RollingFile name="RollingFile"
fileName="./logs/trade-system-logger-log4j2.log"
filePattern="./logs/$${date:yyyy-MM}/trade-system-logger-log4j2-%d{-dd-MMMM-yyyy}-%i.log.gz">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
<Policies>
<!-- rollover on startup, daily and when the file reaches
10 MegaBytes -->
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy
size="10 MB" />
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<!-- LOG everything at INFO level -->
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
</Root>
<!-- LOG "com.baeldung*" at TRACE level -->
<Logger name="com.tradesystem" level="trace"></Logger>
</Loggers>
You are using a RollingFile Appender and this will archive the logs in a compressed file.
Please read the documentation: https://logging.apache.org/log4j/2.x/manual/appenders.html#RollingFileAppender
because of your filePattern=....log.gz . rolling file will save in .gz file.
try this
filePattern="./logs/$${date:yyyy-MM}/trade-system-logger-log4j2-%d{-dd-MMMM-yyyy}-%i.log">
the rolling file will save with extension '.log'
reference: http://logging.apache.org/log4j/extras/apidocs/org/apache/log4j/rolling/TimeBasedRollingPolicy.html
Related
When log4j2.xml is in the jar (in resources), then everything works. But when I moved log4j2.xml outside the jar, then log file is created but it's empty all the time. I run my application in that way:
java.exe -Dfile.encoding=UTF-8 -Dlog4j.configurationFile=C:\config\log4j2.xml -jar C:\app\myApp.jar
Here is my log4j2.xml file:
<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/application.log"
filePattern="logs/application-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
<DefaultRolloverStrategy max="5"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="FileAppender" />
</Root>
</Loggers>
Try using the URI: file://c:/config/log4j2.xml
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>
This is my scenario:
For my website I have used log4j2 for rolling log files. currently I am generating 3 log file which consist of 2 rolling file (trace & error) and 1 routing appender.
1) Trace log of entire date (rolling file appender)
2) Error log of entire date (rolling file appender)
3) logged users activity log for entire day (routing file appender)
Below is my log4j2.xml using for the above scenario, which works exactly as per the requirement.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Configuration status="WARN" name="mywebsite" monitorInterval="30">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="[%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%logger{1}] - %msg%n" />
</Console>
<RollingFile name="error-log" append="true"
fileName="D:/client/error [${date:yyyy-MM-dd}].log" filePattern="D:/lient/error-%d{yyyy-MM-dd}.log">
<PatternLayout
pattern="[%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%logger{1}] - %msg%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
<SizeBasedTriggeringPolicy size="250 MB" />
</Policies>
</RollingFile>
<RollingFile name="trace-log" append="true"
fileName="D:/client/trace [${date:yyyy-MM-dd}].log" filePattern="D:/client/trace-%d{yyyy-MM-dd}.log">
<PatternLayout
pattern="[%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%logger{1}] - %msg%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
<SizeBasedTriggeringPolicy size="250 MB" />
</Policies>
</RollingFile>
<Routing name="RoutingAppender">
<Routes pattern="$${ctx:logFileName}">
<Route>
<RollingFile name="Rolling-${ctx:logFileName}" append="true"
fileName="D:/userlog/${ctx:logFileName}~${date:yyyy-MM-dd}.log"
filePattern="D:/userlog/${date:yyyy-MM}/%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout
pattern="[%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%logger{1}] - %msg%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
<SizeBasedTriggeringPolicy size="512 MB" />
</Policies>
</RollingFile>
</Route>
<Route ref="Console" key="${ctx:logFileName}" />
</Routes>
</Routing>
</Appenders>
<Loggers>
<Root level="trace" additivity="false">
<Appender-Ref ref="Console"/>
<Appender-Ref ref="trace-log" level="trace" />
<Appender-Ref ref="error-log" level="error" />
<Appender-Ref ref="RoutingAppender" />
</Root>
</Loggers>
</Configuration>
I am new to log4j, I somehow managed to configure the log4j xml from the sources in internet.
Rolling file were created for every day and file append-er created for each user and catches the respective events in the respective files for the entire day, until a new file rolls out on next day.
Issue is :
When a new file rolls on, few events of current day were added in the previous day log file.
For example the trace.log file of 1st November has the few log events(say around 5-10 lines) entries of 2nd November.
This happens for file append-er too .
For example file append-er will generate log file based on user log's in say log file named john-01-112015.log was created yesterday. and same user log's in 2nd November a new file will be rolled out as per the configuration. but the few logs of 2nd November is added in the john-01-112015.log as said in the previous scenario.
Is there anything wrong in the log4j2.xml ? guys help me to solve this issue.
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.
I am trying log4j2 to create log file to the system I am developing right, I have followed the instruction on their site and there is no error occurred when I run it, but the log is not saved on where I set it (ex. "D:\logs\app.log").
Here is My log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
<appenders>
<RollingFile name="MyRollingFile" fileName="D:/logs/app.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="250 MB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingFile>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</appenders>
<loggers>
<logger name="Log_RollingFile" level="TRACE" additivity="false">
<appender-ref ref="MyRollingFile"/>
</logger>
<root level="ERROR">
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>
I tried to :
delete app.log to see if my configuration (D:\logs\app.log) works. When I run the application it creates app.log so I think it means that it sees the configuration and the only thing is it is NOT SAVING the log.info that I did in java application
Change root level to "TRACE", and it prints the log.info.
[EDIT:]
I have also these libraries on my classpath
log4j-api-2.0-beta3.jar
log4j-core-2.0-beta3.jar
Am I missing something on RollingFile configuration or a library (maybe)?
Thanks in advance.
Your logger name is incorrect.
As explained in the configuration instructions you linked to, the logger should be named according to the package/classes you wish to capture logging for.
In their example the logger named com.foo.Bar would log everything from the Bar class in package com.foo with TRACE level.