Log4j2 doesn't log into logfile - java

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.

Related

Log4j2 : Changing log level for a specific user and specific loggers : not working as expected

Requirement is to change the log level for a specific user . Using DynamicThresholdFilter at the context level does work but applies to all the loggers . My requirement is to only apply it for specific loggers and not impact the others. With this configuration file all the appenders are logging at debug levell.
This is how my file looks like . Any recommendations on applying the same filter or any other filter which can enable debug logging for specific loggers ?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO" monitorInterval="30">
<Properties>
---------------------
</Properties>
<DynamicThresholdFilter
key="customLogging-enabled" onMatch="ACCEPT" onMismatch="DENY">
<KeyValuePair key="true" value="DEBUG" />
</DynamicThresholdFilter>
<Appenders>
<Console name="consoleAppender" target="SYSTEM_OUT"
follow="true">
<PatternLayout pattern="${CONSOLE_LOG_PATTERN}" />
</Console>
<RollingFile name="customLoggingAppender"
fileName="${sys:log_dir}/customLogging.log"
filePattern="customLogging-%d{yyyy-MM-dd}-%i.log" append="true">
<ThreadContextMapFilter onMatch="DENY"
onMismatch="NEUTRAL">
<KeyValuePair key="customLogging-enabled" value="true" />
</ThreadContextMapFilter>
<PatternLayout>
.......................
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5000KB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
<RollingFile name="standardAppender"
fileName="${sys:log_dir}/standard.log"
filePattern="standard-%d{yyyy-MM-dd}-%i.log" append="true">
<PatternLayout>
....................
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5000KB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
.......More appenders here
</Appenders>
<Loggers>
<Root level="ERROR">
<AppenderRef ref="consoleAppender" />
<AppenderRef ref="standardAppender" />
</Root>
<!-- DEFAULTED TO ROOT LOGGER - START -->
<Logger name="com.abc" level="ERROR">
<AppenderRef ref="customLoggingAppender" />
</Logger>
<Logger name="com.abc.helpers" level="ERROR">
<AppenderRef ref="customLoggingAppender" />
</Logger>
<Logger name="com.abc.business.persist.utils" level="ERROR" />
<Logger
name="com.abc.business.persist.ManagerFactory"
level="ERROR" />
<Logger name="com.da.handlers" level="ERROR" additivity="false">
<AppenderRef ref="abcAppender" />
<AppenderRef ref="customLoggingAppender" />
</Logger>
<Logger name="com.handler.audit" level="ERROR"
additivity="false">
<AppenderRef ref="xyzAppender" />
</Logger>
</Loggers>
</Configuration>
I tried applying the same filter at the appender level but it did not work
<RollingFile name="customLoggingAppender" fileName="${sys:log_dir}/customLogging.log" filePattern="customLogging-%d{yyyy-MM-dd}-%i.log" append="true">
<DynamicThresholdFilter key="customLogging-enabled" onMatch="ACCEPT" onMismatch="DENY">
<KeyValuePair key="true" value="DEBUG"/>
</DynamicThresholdFilter>
<PatternLayout>
....
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5000KB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
My expectation is that ONLY the loggers referring to this appender will be logging at debug level

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>

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>

log4j2 doesnt write log in files

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.

log4j2 file not writing to file

I've been searching the web for days now but somehow nothing seems to help.
I try to migrate from log4j 1.x to log4j 2 but something is not right here and I hope one of you can help.
I have a JSF application running on Websphere 8 and this is is my configuration so far:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Filter type="ThresholdFilter" level="trace"/>
<Appenders>
<RollingFile name="DEBUG" filePattern="C:/Files/varm_debug.%d{yyyy-MM-dd}.log" fileName="C:/Files/varm_debug.log" append="true">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} ;%-5p; [%c{1}.%M]; %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="1500KB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
<ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
<RollingFile name="INFO" filePattern="C:/Files/varm_info.%d{yyyy-MM-dd}.log" fileName="C:/Files/varm_info.log" append="true">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} ;%-5p; [%c{1}.%M]; %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="1500KB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
<RollingFile name="WARN" filePattern="C:/Files/varm_warn.%d{yyyy-MM-dd}.log" fileName="C:/Files/varm_warn.log" append="true">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} ;%-5p; [%c{1}.%M]; %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="500KB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
<ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
<RollingFile name="ERROR" filePattern="C:/Files/varm_error.%d{yyyy-MM-dd}.log" fileName="C:/Files/varm_error.log" append="true">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} ;%-5p; [%c{1}.%M]; %m%n"/>
<Policies>
<SizeBasedTriggeringPolicy size="500KB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
<File name="comparisonLogAppender" fileName="C:/Files/VARM-output.log" append="true">
<PatternLayout pattern="%d{ISO8601}; %m%n"/>
</File>
</Appenders>
<Loggers>
<!-- Logger to compare results -->
<!-- Logger to log infos/errors throughout comparison -->
<Logger name="comparisonLogger" level="trace" additivity="true" immediateFlush="true">
<AppenderRef ref="comparisonLogAppender"/>
</Logger>
<Logger name="de.saltsolutions.varm" level="info" >
<AppenderRef ref="DEBUG" level="debug"/>
<AppenderRef ref="INFO" level="info"/>
<AppenderRef ref="WARN" level="warn"/>
<AppenderRef ref="ERROR" level="error"/>
</Logger>
<Logger name="org.hibernate" level="warn">
<AppenderRef ref="DEBUG" level="debug"/>
<AppenderRef ref="INFO" level="info"/>
<AppenderRef ref="WARN" level="warn"/>
<AppenderRef ref="ERROR" level="error"/>
</Logger>
<Root level="debug">
<AppenderRef ref="DEBUG" level="debug"/>
</Root>
<Root level="info">
<AppenderRef ref="INFO" level="info"/>
</Root>
<Root level="warn">
<AppenderRef ref="WARN" level="warn"/>
</Root>
<Root level="error">
<AppenderRef ref="ERROR" level="error"/>
</Root>
</Loggers>
</Configuration>
All the files are generated okay but they stay empty.
My main problem is the comparisonLogger. If I use it (LogManager.getLogger("comparisonLogger") in my main class, it writes the log file perfectly.
But when I try using the exact same logger in my ManagedBean (ViewScoped) nothing gets logged.
Does anybody know why it doesn't work in my ManagedBean? Does this have anything to do with the Websphere?
I hope anybody can help because I have absolutely no idea what I could possibly do to make this work.
Thanks so much in advance
I finally figured out my problems.
I use Log4j2 in a web application which means that I needed to add some stuff in my web.xml (further information here: http://logging.apache.org/log4j/2.x/manual/webapp.html)
You can only configure ONE root logger
Once I adjusted that, everything worked just as expected.
My Log4j2 Jar-Files are now in my client and my ear and my XML config is in my client but it also works if you split the config files (one part in my web project the other for the client on C:\temp or some other directory)
You may need to put the log4j2 jar files and log4j2.xml config in a place so that they are always in the classpath.

Categories