I added Log4j2 into my project
implementation 'org.springframework.boot:spring-boot-starter-log4j2'
and added next properties:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n%xwEx"/>
<!-- Крутое, но бесцветное логирование-->
<!-- <PatternLayout pattern="%highlight{%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36}.%M():%L - %msg%n}{FATAL=red blink, ERROR=red, WARN=yellow bold, INFO=black, DEBUG=green bold, TRACE=blue}"/>-->
</Console>
<RollingFile name="File" fileName="C:/Users/Mi/output.log" filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz" immediateFlush="true">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
<Logger name="org.apache.logging.log4j.filelogger" level="info" additivity="false">
<AppenderRef ref="File"/>
</Logger>
</Loggers>
</Configuration>
App write logs to console, but not in file. As I wait, app shall write logs into file by url C:/Users/Mi/output.log, it creates automatically, I can't delete it while app is wotking, but there no logs. Please advice, what to do.
You are only logging stuff from org.apache.logging.log4j.filelogger package into the "File" appender. The Logger name property is the package/class reference
You should do something like this to get logging to file instead of console:
<Root level="info">
<AppenderRef ref="File"/>
</Root>
If you want console and file do:
<Root level="info">
<AppenderRef ref="File"/>
<AppenderRef ref="Console"/>
</Root>
Related
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>
So I am trying to log the info and error messages to both the console and the log file.
The messages get saved successfully in the log file but do not get logged in the console. Have attached the xml file below:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="basePath">./DemoLogs</Property>
</Properties>
<Appenders>
<RollingFile name="File" fileName="${basePath}/app.log" filePattern="${basePath}/app-%d{yyyy-MM-dd}.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %c{2} -%msg%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="100 KB"/>
</Policies>
</RollingFile>
<Console name="Console">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="all">
<AppenderRef ref="File"/>
</Root>
<Logger name="Demo" level="debug" additivity="false">
<AppenderRef ref="File"/>
</Logger>
</Loggers>
</Configuration>
You need to add a reference to the console appender.
<Root level="all">
<AppenderRef ref="File"/>
<AppenderRef ref="Console"/>
</Root>
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.
I am using log4j2. But the problem that I am facing is that it logs all logs. I want to ... log from specific package to a specific file & other package to another file. I am using log4j2.xml for configuration.
Please can someone help?
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Loggers>
<Root level="DEBUG" additivity="false">
<AppenderRef level="DEBUG" ref="CONSOLE" />
<AppenderRef level="DEBUG" ref="fileAppender" />
</Root>
<Logger name="com.pkg.test.logging.method" level="DEBUG"
additivity="false">
<Appender-ref ref="fileAppender" level="DEBUG" />
</Logger>
</Loggers>
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout
pattern="%highlight{[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1}: %L - %msg%n%throwable}" />
</Console>
<RollingFile name="fileAppender" fileName="./log.log"
filePattern="./log-%d{yyyy-MM-dd}.log">
<PatternLayout
pattern="%highlight{[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1}: %L - %msg%n}" />
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
</Policies>
</RollingFile>
</Appenders>
Just answered the question.
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Loggers>
<Root level="DEBUG" additivity="false">
<AppenderRef level="DEBUG" ref="CONSOLE" />
</Root>
<Logger name="com.pkg.test.logging.method" level="DEBUG"
additivity="false">
<Appender-ref ref="fileAppender" level="DEBUG" />
</Logger>
</Loggers>
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout
pattern="%highlight{[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1}: %L - %msg%n%throwable}" />
</Console>
<RollingFile name="fileAppender" fileName="./log.log"
filePattern="./log-%d{yyyy-MM-dd}.log">
<PatternLayout
pattern="%highlight{[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1}: %L - %msg%n}" />
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
</Policies>
</RollingFile>
</Appenders>
Removed the <AppenderRef level="DEBUG" ref="fileAppender" /> from root logger. Thus it started logging logs based on packages.
From Log4J Manual:
Adding a specific logger for a class: (you can refer to packages here too)
<Logger name="org.apache.logging.log4j.test2" level="debug" additivity="false">
<AppenderRef ref="File"/>
</Logger>
Adding a specific appender:
<Appender type="File" name="File" fileName="${filename}">
<Layout type="PatternLayout">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</Layout>
</Appender>
The <Loggers> section of the config looks correct. But, if all other logs not belonging to the expected package is also getting logged, it is the Root logger which is responsible for these logs, and not the package level logger. It may not be active because of the way you are creating the logger instance in your code. E.g: If one is using slf4j to create the logger instance, then this is how we would need to create the logger instance:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Logger logger = LoggerFactory.getLogger(Test.class);
If one gets the logger instance using - Logger logger = LoggerFactory.getLogger("Test") then, the config will not work as expected.
Also, another answer mentioned that the solution was to remove the fileAppender that the package logger was using from the root logger. This, in fact, is not a problem. You are free to use the same appenders in root and package level logger. My answer in context of log4j-api 2.14.1
I'm working with log4j 2, version 2.4 on Jboss 6.4, my log4j2.xml is in WEB-INF/classes placed. I can deploy my war without errors about my log4j config, and I can log properly in my RollingFile name="myLog", but something I can't understand is why appear the info's logs for "myLog" in my console appender
private static final Logger loggerBatch = LogManager.getLogger("my.test");
loggerBatch.info("test log");
Config:
<?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] %-5level %logger{36} - %msg%n" />
</Console>
<RollingFile name="myLog" fileName="C:\\Workarea\\my.log"
filePattern="C:\\Workarea\\myLog-%d{dd-MM-yyyy}-%i.log"
append="true">
<PatternLayout>
<Pattern>%d %p %c [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5MB"/>
</Policies>
<DefaultRolloverStrategy max="5"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="my.test">
<AppenderRef ref="myLog" level ="info"/>
</Logger>
<Root level="debug">
<AppenderRef ref="Console" />
</Root>
<Root level="error">
<AppenderRef ref="Console" />
</Root>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
In addition to Andrey's answer you have declared 3 root loggers. There can only be one. Which one of the definitions will win is undefined.
Check your additivity settings. This will fix the issue
<Logger name="my.test" additivity="false">