log4j2 doesnt write log in files - java

following XML configuration is my log4j config, I see the log messages in console but the log file is empty, whats wrong in it ?
I am using log4j-api-2.1
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<RollingFile name="file-log" fileName="${log-path}/logtile.log/"
filePattern="${log-path}/logtile-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>%d{HH:mm:ss.SSS} [%t] %-5level - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1"
modulate="true" />
<SizeBasedTriggeringPolicy size="500" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="mylogger" level="info" additivity="false">
<appender-ref ref="file-log" level="info" />
<appender-ref ref="Console" level="info" />
</Logger>
<Root level="info">
<appender-ref ref="Console" />
</Root>
</Loggers>

How does your code obtain Logger instances? Assuming your code looks something like this:
Logger log = LogManager.getLogger(com.mycompany.MyClass.class);
log.info("Where is my message?");
Note that the fully qualified name of your logger is "com.mycompany.MyClass".
This logger will send messages to the root logger, and all named loggers whose name matches "com.mycompany.MyClass". The following configuration names would match this fully qualified class:
<Logger name="com.mycompany.MyClass" ...>
<Logger name="com.mycompany" ...>
<Logger name="com" ...>
However, the named logger in your configuration only matches "mylogger", so this logger will not receive the "Where is my message?" log event. The root logger will get the event and send it to its appender, the console.

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>

Log4j2 doesn't log into logfile

I changed the path of log4j2.xml configuration file and now the log files will create but application doesn't log into the files. I get no error messages.
My order structur:
Log4j2.xml:
<Properties>
<Property name="LOG_PATTERN">
%d{yyyy-MM-dd HH:mm:ss.SSS} %5level ${hostName} --- [%15.15thread] %-100.100class : %message%n%exception
</Property>
</Properties>
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${LOG_PATTERN}"/>
</Console>
<RollingFile name="RootAppender" fileName="./log/log.log" filePattern="./log/log-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="100MB" />
<TimeBasedTriggeringPolicy interval="1"/>
</Policies>
<DefaultRolloverStrategy max="7"/>
</RollingFile>
<RollingFile name="CurlAppender" fileName="./log/curl.log" filePattern="./log/curl-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<Pattern>${LOG_PATTERN}</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="500MB" />
<TimeBasedTriggeringPolicy interval="1"/>
</Policies>
<DefaultRolloverStrategy max="7"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="RootAppender"/>
</Root>
<Logger name="CurlLogger" level="trace" additivity="false">
<AppenderRef ref="CurlAppender"/>
</Logger>
</Loggers>
I've found the issue.
In the application.properties file there must be set the "logger.config" attribute.
After setting this attribute, the file logging works.
I think there is some issue in Loggers tag.
In the Logger name please add your package name and also change your AppenderRef in Logger.
Please change your xml code to below:
<Loggers>
<Root level="info">
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="RootAppender"/>
</Root>
<Logger name="com.example.springbackend.controller" level="trace" additivity="false">
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="RootAppender"/>
</Logger>
</Loggers>
Here <Logger name="com.example.springbackend.controller" level="trace" additivity="false"> Logger name is project package name.
I think the issue is in root level.
Please change the root level logger to debug or trace.

log4j2 logging to console only

I have a maven web project which requires logging.
I decided to use log4j2 for that purpose and added the required entries in the pom.xml file
Then I created the log4j2.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<RollingFile name="RollingFile" fileName="${logs.path}/text.log"
filePattern="${logs.path}/%d{YYYYMMdd}-text.%i.log">
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
<Policies>
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} %-5p %c{1}:%L - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="root" level="info" additivity="false">
<appender-ref ref="RollingFile" level="info" />
<appender-ref ref="Console" level="info" />
</Logger>
<Root level="info" additivity="false">
<AppenderRef ref="RollingFile" />
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
I'm starting tomcat with -Dlogs.path="C:\mylogs", where C:\mylogs exists and has public read/write access.
The console logger is working fine, I can see the correct output in my console, the issue is that the file isn't being created, so I have no logfile, everything gets logged to catalina.out only, and the tomcat startup logs don't show errors for log4j.
What am I missing? is there some additional configuration I need to do in order to log to file?
I went through the documentation. You must refer to system properties with sys:, and it seems that tomcat properties are seen as system properties, so I replaced ${logs.path} with ${sys:logs.path} and it worked.
To create a log file with the log4j2 config you first need to define your appender. Here's an example:
<RollingFile name="MGMT"
fileName="${logdir}/mgmt.log"
filePattern="${logdir}/mgmt.%d{yyyy-MM-dd}.log">
<PatternLayout pattern="${layout}"/>
<CronTriggeringPolicy schedule="0 0 0 * * ?"/>
<DefaultRolloverStrategy>
<Delete basePath="${logdir}" maxDepth="1">
<IfFileName glob="mgmt.*.log" />
<IfAccumulatedFileCount exceeds="10" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
Afterwards you just need to define a logger that will use it:
<Logger name="the package or name that is displayed in the log line"
level="ALL"
additivity="false">
<AppenderRef ref="MGMT"/>
</Logger>
If you have a log that has com.company.test as package set that package as name for the logger. It's important that they match.
The field additivity will define if you want to pass the catched log to its parent ( true ) or just let this logger handle it ( false )
EDIT:
your config file might need this:
<Configuration status="info">
<Properties>
<Property name="logdir">${sys:catalina.base}/logs</Property>
<Property name="layout">%d [%t] %-5p %c- %m%n</Property>
</Properties>
<Appenders>
If you don't want to use this you have to define the path static like <RollingFile name="MGMT"
fileName="C:\path\file.log"

Apache Log4j2 package specific logging using log4j2.xml

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

Log4j 2 logging in the wrong appender

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">

Categories