configuration of log4j2.xml - java

I'm using log4j2. I would like to have 3 exits for my logs: console and 2 rolling files: the first for info log (FileRoot) and the second for errors (FileErrors). After running the project both of files are created, logs are displayed on console and FileErrors, but nothing in FileRoot. What's wrong with my configuration? Here is it:
<?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] %-10level %logger{36} - %msg%n" />
</Console>
<RollingFile name="FileRoot" fileName="master/logs/app.log" filePattern="master/logs/app-%d{yyyy-MM-dd-HH}.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
<RollingFile name="FileErrors" fileName="master/logs/appErrors.log" filePattern="master/logs/appErrors-%d{yyyy-MM-dd-HH}.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.yyyy" level="ALL" additivity="false">
<AppenderRef ref="Console" />
</Logger>
<Logger name="com.yyyy" level ="INFO" additivity="false">
<AppenderRef ref="FileRoot" />
</Logger>
<Logger name="com.yyyy" level="ERROR" additivity="false">
<AppenderRef ref="FileErrors" />
</Logger>
<Root level="info">
<AppenderRef ref="Console"></AppenderRef>
<AppenderRef ref="FileRoot" />
<AppenderRef ref="FileErrors"></AppenderRef>
</Root>
</Loggers>
</Configuration>``
And here are dependencies in pom.xml:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
And I use
if( LOG.isInfoEnabled() )
{
LOG.info( "app starting" );
}

Related

Log4j2: not write into file

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>

Own log file for a class, thread java log4j2

There is a class Task. Inherited from Thread.
How do I configure the log4j2 logger to create and write Task-1.log for each thread Task?
My xml:
<?xml version="1.0" encoding="UTF-8"?>
<Properties>
<Property name="pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} [%level] [thread-id %T] %l - %msg%n</Property>
</Properties>
<Appenders>
<RollingFile name="fileLogger" fileName="${sys:logFolder}/test.log" filePattern="${sys:logFolder}/test-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>${pattern}</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="${pattern}" />
</Console>
</Appenders>
<Loggers>
<Root level="info" additivity="false">
<appender-ref ref="console" />
<appender-ref ref="fileLogger" />
</Root>
<Logger name="com.test.*" level="info">
<appender-ref ref="fileLogger" level="info" />
</Logger>
</Loggers>

Unable to log messages in console using Log4j2 but they get logged in the file

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>

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.

log4j vs log4j2: log to different file

Using log4j I can log to different files invoking this method:
Logger.getLogger("test")
where test is an appender defined in log4j.properties. How can I log to different files with log4j2? This is my configuration file and I would choose programmatically where to log:
<Properties>
<Property name="log-path">C:/logs</Property>
</Properties>
<Loggers>
<Logger name="it.mypackage" level="debug" additivity="false">
<appender-ref ref="file" level="debug" />
<appender-ref ref="file2" level="error" />
</Logger>
</Loggers>
<Appenders>
<!-- file.log -->
<RollingFile name="file" fileName="${log-path}/file.log" filePattern="${log-path}/file-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
<!-- file2.log -->
<RollingFile name="file2" fileName="${log-path}/file2.log" filePattern="${log-path}/file2-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
</Appenders>
[SOLUTION]
Oh Yes, I'm missing a logger:
<Properties>
<Property name="log-path">C:/logs</Property>
</Properties>
<Loggers>
<Logger name="logger1" level="debug" additivity="false">
<appender-ref ref="file" level="debug" />
</Logger>
<Logger name="logger2" level="debug" additivity="false">
<appender-ref ref="file" level="debug" />
<appender-ref ref="file2" level="error" />
</Logger>
</Loggers>
<Appenders>
<!-- file.log -->
<RollingFile name="file" fileName="${log-path}/file.log" filePattern="${log-path}/file-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
<!-- file2.log -->
<RollingFile name="file2" fileName="${log-path}/file2.log" filePattern="${log-path}/file2-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
</Appenders>
I can choose logger in this way:
private static Logger logger = LogManager.getLogger("logger1");
If you keep the rest of the configuration the same, but modify the Loggers section:
<Loggers>
<Logger name="logger1" level="debug" additivity="false">
<appender-ref ref="file" level="debug" />
</Logger>
<Logger name="logger2" level="debug" additivity="false">
<appender-ref ref="file2" level="error" />
</Logger>
</Loggers>
Now, you can select the appender in your code by getting the logger by name:
Logger logger1 = LogManager.getLogger("logger1");

Categories