Warnings while running an application - java

I configured log4j ,and I tested it on an application creating log files for threads but I am getting log4j warnings while it is running and log files are not being created for threads.
log4j:WARN No appenders could be found for logger(com.amazonaws.internal.config.InternalConfig).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN
See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<properties>
<property name="logMsgPattern">%d{HH:mm:ss} %-5level - %msg%n</property>
<property name="logDir">logs</property><!-- ${sys:testLogDir} -->
</properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${logMsgPattern}" />
</Console>
<Routing name="Routing">
<Routes pattern="$${ctx:threadId}">
<Route>
<RollingFile name="RollingFile-${ctx:threadId}"
fileName="${logDir}/${ctx:threadId}.log"
filePattern="${logDir}/%d{yyyy-MM-dd}/${ctx:threadId}-%d{yyyy-MM-dd}.log">
<PatternLayout pattern="${logMsgPattern}" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>
</Route>
</Routes>
</Routing>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console" />
</Root>
<logger name="test" level="INFO" />
<Root level="INFO">
<AppenderRef ref="Console" />
<AppenderRef ref="Routing" />
</Root>
</Loggers>
</Configuration>

I tried with logback instead of log4j and its solved. i can create multiple log files for multiple threads.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<statusListener class="ch.qos.logback.core.status.NopStatusListener" />
<property name="USER_HOME" value="\logs" />
<appender name="FILE-THREAD" class="ch.qos.logback.classic.sift.SiftingAppender">
<!-- This is MDC value -->
<!-- We will assign a value to 'logFileName' via Java code -->
<discriminator>
<key>logFileName</key>
<defaultValue>head0</defaultValue>
</discriminator>
<sift>
<!-- A standard RollingFileAppender, the log file is based on 'logFileName'
at runtime -->
<appender name="FILE-${logFileName}"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${USER_HOME}/${logFileName}.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} %mdc [%thread] %level %logger{35}
- %msg%n
</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>${USER_HOME}/${logFileName}.%i.log.zip
</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>10</MaxIndex>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
</sift>
</appender>

Related

How to save logs into file with timestamp with log4j2

i have a little problem. I had to change my logging implementation from org.apache.commons.logging to org.apache.logging.log4j. I saved my logs into a "log" directory and each file got a appended timestamp.
I used the following .xml file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.springframework.web" level="INFO"/>
<timestamp key="myTimestamp" timeReference="contextBirth" datePattern="yyyy-MM-dd'_'HH-mm-ss"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/logs/app-${myTimestamp}.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd_HH:mm:ss} - %msg%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>app.%i{yyyy-MM-dd_HH:mm:ss.SSS}}.log</FileNamePattern>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>30MB</MaxFileSize>
</triggeringPolicy>
</appender>
<logger name="com....." level="INFO" additivity="false">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</logger>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Now i wanted to extend my xml file with log4j2, because of some new environments. But i also want to save it in the "logs" directory again. I tried different methods via the documentation and http://www.baeldung.com, but i don't get. My application always writes in the my app.log again without timestamp. How can i realize it? Where is the mistake in my new .xml ?
I thought with this
filePattern="logs/ldapmicroservice.%d{MM-dd-yyyy-HH-mm}.log.gz"
i define the new pattern of my file?
This is my new .xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30"
packages="com......">
<Properties>
<Property name="LOG_PATTERN">
%d{yyyy-MM-dd HH:mm:ss.SSS} %5p %X{mdcData} --- %-40.40c{1.} : %m%n%ex
</Property>
</Properties>
<Appenders>
<RollingFile
name="FILE" fileName="/logs/app.log"
filePattern="logs/app.%d{MM-dd-yyyy-HH-mm}.log.gz"
ignoreExceptions="false">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss} %p %m%n</Pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
<Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${LOG_PATTERN}" />
</Console>
<CloudantAppender name="CloudantAppender"
database="logs"
......
<PatternLayout pattern="${LOG_PATTERN}"/>
</CloudantAppender>
</Appenders>
<Loggers>
<Logger name="com...." level="debug" additivity="false">
<AppenderRef ref="FILE" />
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="CloudantAppender" />
</Logger>
<Root level="info">
<AppenderRef ref="FILE" />
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="CloudantAppender" />
</Root>
</Loggers>
</Configuration>
I found the solution on my own:
<RollingFile
name="FILE" fileName="/logs/app-${date:yyyy-MM-dd'_'HH-mm-ss}.log"
filePattern="logs/app-%d{yyyy-MM-dd}-%i.log"
ignoreExceptions="false">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd'_'HH-mm-ss} %p %m%n</Pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>

rolling in logback logging is not working

I am trying to create logger policy in a spring based project.
The issue I am facing is related to rolling policy. the logfile.log is created and is working fine but the rolling file rollingfile.log.%d{yyyy-MM-dd}.log is not created.
Note : In below logger I wish to created a rolling policy which creates a new file when rollingfile.log exceds more than 10 Mb. So I can have backup of logs without having one single huge logger file. Is there any way to do that?
Given below is my logback.xml.
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true" scanPeriod="30 seconds">
<property name="LOG_PATH" value="D:/coinLogs" />
<property name="LOG_ARCHIVE" value="${LOG_PATH}/archive" />
<appender name="Console-Appender" class="ch.qos.logback.core.ConsoleAppender">
<layout>
<pattern>[%d{yyyy-MM-dd HH:mm:ss}] - [%X{requestId}] - %p %c -- %m%n
</pattern>
</layout>
</appender>
<appender name="File-Appender" class="ch.qos.logback.core.FileAppender">
<file>${LOG_PATH}/logfile.log</file>
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss}] - [%X{requestId}] - %p %c -- %m%n
</pattern>
<outputPatternAsHeader>true</outputPatternAsHeader>
</encoder>
</appender>
<appender name="RollingFile-Appender"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/rollingfile.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_ARCHIVE}/rollingfile.log%d{yyyy-MM-dd}.log
</fileNamePattern>
<maxHistory>30</maxHistory>
<totalSizeCap>10MB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<appender name="Async-Appender" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="RollingFile-Appender" />
</appender>
<logger name="coinPay.logbackxml" level="info" additivity="false">
<appender-ref ref="Console-Appender" />
<appender-ref ref="File-Appender" />
<appender-ref ref="Async-Appender" />
</logger>
<!-- To remove extra hibernate logs -->
<logger name="org.hibernate">
<level value="info" />
</logger>
<root>
<appender-ref ref="Console-Appender" />
<appender-ref ref="File-Appender" />
</root>
</configuration>
any help will be appreciated. Thank you in advance :D
I think you should add in the root element of the configuration, the Async-Appender appender which wraps your RollingFile-Appender.
Otherwise it is probably not considered as enabled by logback:
<root>
<appender-ref ref="Console-Appender" />
<appender-ref ref="File-Appender" />
<appender-ref ref="Async-Appender" />
</root>

Time and size based logger backup policy

I am trying to create logger policy in a spring based project.
In below logger I wish to created a rolling policy which creates a new file when rollingfile.log exceds more than 10 Mb. So I can have backup of logs without having one single huge logger file. Is there any way to do that?
Given below is my logback.xml.
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true" scanPeriod="30 seconds">
<property name="LOG_PATH" value="D:/coinLogs" />
<property name="LOG_ARCHIVE" value="${LOG_PATH}/archive" />
<appender name="Console-Appender" class="ch.qos.logback.core.ConsoleAppender">
<layout>
<pattern>[%d{yyyy-MM-dd HH:mm:ss}] - [%X{requestId}] - %p %c -- %m%n
</pattern>
</layout>
</appender>
<appender name="File-Appender" class="ch.qos.logback.core.FileAppender">
<file>${LOG_PATH}/logfile.log</file>
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss}] - [%X{requestId}] - %p %c -- %m%n
</pattern>
<outputPatternAsHeader>true</outputPatternAsHeader>
</encoder>
</appender>
<appender name="RollingFile-Appender"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/rollingfile.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_ARCHIVE}/rollingfile.log%d{yyyy-MM-dd}.log
</fileNamePattern>
<maxHistory>30</maxHistory>
<totalSizeCap>10MB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<appender name="Async-Appender" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="RollingFile-Appender" />
</appender>
<logger name="coinPay.logbackxml" level="info" additivity="false">
<appender-ref ref="Console-Appender" />
<appender-ref ref="File-Appender" />
<appender-ref ref="Async-Appender" />
</logger>
<!-- To remove extra hibernate logs -->
<logger name="org.hibernate">
<level value="info" />
</logger>
<root>
<appender-ref ref="Console-Appender" />
<appender-ref ref="File-Appender" />
<appender-ref ref="Async-Appender" />
</root>
</configuration>
The total size cap is not doing what you want.
<totalSizeCap>10MB</totalSizeCap>
It caps the total size of all your archived log files. You could have a look at TimeBasedArchiveRemover::capTotalSize to understand what it is doing.
What you need to do is change TimeBasedRollingPolicy to SizeAndTimeBasedRollingPolicy and set <maxFileSize>10MB</maxFileSize>
So your configuration should include:
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_ARCHIVE}/rollingfile.log%d{yyyy-MM-dd}.log
</fileNamePattern>
<maxFileSize>10MB<maxFileSize>
<maxHistory>30</maxHistory>
</rollingPolicy>

Logback rolling logging not working

I am trying to create logger policy in a spring based project.
The issue I am facing is related to rolling policy. the logfile.log is created and is working fine but the rolling file rollingfile.log.%d{yyyy-MM-dd}.log is not created.
Given below is my logback.xml.
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true" scanPeriod="30 seconds">
<property name="LOG_PATH" value="D:/coinLogs" />
<property name="LOG_ARCHIVE" value="${LOG_PATH}/archive" />
<appender name="Console-Appender" class="ch.qos.logback.core.ConsoleAppender">
<layout>
<pattern>[%d{yyyy-MM-dd HH:mm:ss}] - [%X{requestId}] - %p %c -- %m%n</pattern>
</layout>
</appender>
<appender name="File-Appender" class="ch.qos.logback.core.FileAppender">
<file>${LOG_PATH}/logfile.log</file>
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss}] - [%X{requestId}] - %p %c -- %m%n
</pattern>
<outputPatternAsHeader>true</outputPatternAsHeader>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${LOG_ARCHIVE}/rollingfile.log.%d{yyyy-MM-dd}.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<logger name="coinPay.logbackxml" level="info" additivity="false">
<appender-ref ref="Console-Appender" />
<appender-ref ref="File-Appender" />
</logger>
<!-- To remove extra hibernate logs -->
<logger name="org.hibernate">
<level value="info" />
</logger>
<root>
<appender-ref ref="Console-Appender" />
<appender-ref ref="File-Appender" />
</root>
</configuration>
any help will be appreciated. Thank you in advance :D
You need to specify ch.qos.logback.core.rolling.RollingFileAppender as class for your appender. A FileAppender can not roll.
I believe you're missing the %i parameter, which should be replaced by the rolling file index (i.e. 'mylog.2017-03-13.0.txt', 'mylog.2017-03-13.1.txt', etc.)
In addition, like Uwe Allner, I'm using RollingFileAppender instead of FileAppender.
(I also suggest adding a totalSizeCap to the configuration, in order to control to total size)
Here's my entire appender configuration:
<appender name="SQL_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/mylog.txt</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level- %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<maxHistory>3</maxHistory>
<totalSizeCap>5GB</totalSizeCap>
<fileNamePattern>${LOG_HOME}/archived/mylog.%d{yyyy-MM-dd}.%i.txt.zip</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>20MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>

Configure log4j 2 using xml

I'd like to have log messages shown in three ways:
- on console
- on a single file log/logs/log.log file
- on a monthly changed file of the kind log/logs/log-201610.log
I wrote this configuration (using other questions here and several sources I can't find):
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss,SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="basic" fileName="log/log.log">
<PatternLayout
pattern="%d{dd/MM/yy HH:mm:ss,SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
<RollingFile name="RollingFile" fileName="log/logs/app.log"
filePattern="log/logs/log-%d{yyyyMM}.log">
<PatternLayout>
<Pattern>"%d{HH:mm:ss,SSS} [%t] %-5level %logger{36} - %m%n"</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="ALL">
<AppenderRef ref="console" />
</Root>
<Logger name="sistema" level="ALL" additivity="true">
<AppenderRef ref="basic" />
</Logger>
<Logger name="sistemabis" level="ALL" additivity="true">
<AppenderRef ref="RollingFile" />
</Logger>
</Loggers>
</Configuration>
It seems to work properly (doesn't give errors), but I get an empty file in log/logs/app.log . In my file dependencies.xml I have this:
<dependency org="org.apache.logging.log4j" name="log4j-api" rev="2.5" transitive="false"/>
<dependency org="org.apache.logging.log4j" name="log4j-core" rev="2.5" transitive="false"/>
but I am afraid I'm still missing something.
Most likely, one day I'll remove the logging on the single full file (so I need to be able to distinguish among appenders). No size bound is needed.
Where am I wrong?
EDIT: according to what I read in comments, I edited to show the situa
According to comments, I modified dependencies and replaced some lines in the configuration file:
<Pattern>%d{dd/MM/yy HH:mm:ss,SSS} [%t] %-5level %logger{36} - %msg%n</Pattern>
<Logger name="sistema" level="ALL" additivity="true"> <!-- name รจ il nome del pacchetto radice da cui prendere. -->
<AppenderRef ref="basic" />
<AppenderRef ref="RollingFile" />
</Logger>
It seems to work properly. On 1 November, I'll know more about this.

Categories