log4j2 config file not logging to console/file - log4j2

I have the following log4j2.xml config that I am using.
I have 2 issues :-
When my application runs up, I have nothing logged to either the
console or to mylog.log file by either of apache or springframework or
hibernate classes.
Classes within the package "com.foo.bar", I am able change the log
LEVEL via "level" attribute but the messages are only logged to
mylog.log file but nothing to the console
Any suggestions to above would be appreciated ?
thank you
Pete
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{DEFAULT} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<RollingFile name="File" fileName="mylog.log"
filePattern="mylog-%d{yyyy-MM-dd}.%i.log">
<PatternLayout pattern="%d{DEFAULT} [%t] %-5level %logger{36} - %msg%n" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="30 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.foo.bar" level="info" additivity="false">
<AppenderRef ref="File" />
<AppenderRef ref="Console" />
</Logger>
<Logger name="org.apache" level="debug" >
<AppenderRef ref="File" />
<AppenderRef ref="Console" />
</Logger>
<Logger name="org.hibernate" level="debug" additivity="false">
<AppenderRef ref="File" />
</Logger>
<Logger name="org.springframework" level="debug" additivity="false">
<AppenderRef ref="File" />
</Logger>
<Root level="all">
<AppenderRef ref="Console" />
<AppenderRef ref="File" />
</Root>
</Loggers>

Please try replacing
<Root level="all">
With
<Root level="trace">

It sounds like you have a misconfigured log.
Can you debug remotely and where you find use the logger to log something place a breakpoint. Inspect the logger and then you will find a location of where its picking up the log file from.
Also try providing a VM argument for log4j configuration (-Dlog4j.configuration)on start-up of your application.

Related

log4j:WARN No appenders could be found for logger (org.apache.hc.client5.http.impl.classic.InternalHttpClient)

I've implemented a simple Java app that uses httpclient 5.1.3. I'm also using log4j2 (2.1.9) and have configured it emit log entries at the wire level. I know the log4j2 config is loaded because I can see my file appender working when I emit log messages from my app.
I don't understand why httpclient logging reports no appenders for org.apache.hc.client5.http.impl.classic.InternalHttpClient or for the asynch class if I change my app to use asynch protocol. Please help.
My log4j2 config:
<Appenders>
<Console name="Console">
<PatternLayout pattern="%d %-5level [%logger] %msg%n%xThrowable" />
</Console>
<File name="MyFile" fileName="c:/a/app.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Logger name="org.apache.hc.client5.http" level="DEBUG">
<AppenderRef ref="Console"/>
</Logger>
<Logger name="org.apache.hc.client5.http.wire" level="DEBUG">
<AppenderRef ref="Console"/>
</Logger>
<Logger name="org.apache.hc.client5.http.impl.classic.InternalHttpClient" level="DEBUG">
<AppenderRef ref="Console"/>
</Logger>
<Root level="INFO">
<AppenderRef ref="MyFile" />
</Root>
</Loggers>
</Configuration>
I could sure use some help. Thanks.

Server Logs not showing with log4j2

When I migrate from log4j to log4j2 Server Logs not showing.
Log4j2.xml exist in classpath.
here is my log4j2.xml file
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO" packages="com.vermeg.maskpolicy.customLog" monitorInterval="5">
<Appenders>
<RollingFile name="FILE" fileName="${soliam.fo.logdir}/log/server.log" append="true"
filePattern="${soliam.fo.logdir}/log/$${date:yyyy-MM}/server-%d{yyyy-MM-dd}.log">
<PatternLayout pattern="%d %-5p %X{userName}#%X{remoteAddress} [%c] %m%n"/>
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
<CronTriggeringPolicy schedule="0 0 0 * * ?"/>
</RollingFile>
<Console name="CONSOLE" target="SYSTEM_OUT">
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d{ABSOLUTE} %-5p %X{userName}#%X{remoteAddress} [%c{1}] %m%n"/>
</Console>
<Rewrite name="rewrite">
<LogInterceptor/>
<AppenderRef ref="CONSOLE"/>
<AppenderRef ref="FILE"/>
</Rewrite>
</Appenders>
<Loggers>
<Logger name="com.bsb.soliam.fo.aspectj.AspectJLogger" level="${log4j.level}">
<AppenderRef ref="rewrite"/>
</Logger>
<Logger name="org.jgroups" level="WARN">
<AppenderRef ref="rewrite"/>
</Logger>
<Logger name="org.apache.cxf.phase.PhaseInterceptorChain" level="ERROR">
<AppenderRef ref="rewrite"/>
</Logger>
<Logger name="org.springframework.flex.servlet.MessageBrokerHandlerAdapter" level="WARN">
</Logger>
<Logger name="org.apache.commons.javaflow" level="INFO">
</Logger>
<Logger name="org.apache.commons.beanutils.locale" level="INFO">
</Logger>
<Logger name="com.bsb.soliam.fo.core.filepoller.JmsJobScheduler" additivity="false" level="${log4j.level}">
<AppenderRef ref="rewrite"/>
</Logger>
<Root level="${log4j.root.level}">
<AppenderRef ref="rewrite"/>
</Root>
</Loggers>
</Configuration>
log before migration
enter image description here
log after migration just show hibernate log.
I don't know the main cause of this problem ,
can anyone help please

Can I keep rolling file in log4j2 even though the log is empty

I have a program in java to write log using log4j2 that configured in xml configuration file.
Can I keep rolling file in log4j2 even though the log is empty. I need to use the archived log generated by log4j2. Now the archived log is only created if the log is not empty, if there is no log then the archived is not created. But I want log4j2 to keep generated the log even there is empty log. Any advice?
Here is my code:
<?xml version="1.0" encoding="UTF-8"?>
<Appenders>
<RollingFile
fileName="logs/app-%d{yyyyMMdd}.log"
filePattern="logs/$${date:yyyy-MM-dd}/app-%d{yyyyMMdd}-%i.log.gz"
name="app_file">
<PatternLayout>
<Pattern>%d %p %m %ex%n
</Pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy
size="100 MB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
<Console name="stdout" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %highlight{%class{1.}:%L} [%c] [%p] %m%n" />
</Console>
</Appenders>
<Loggers>
<Logger additivity="false" includeLocation="true"
name="app">
<AppenderRef ref="app_file" level="trace"/>
<AppenderRef ref="stdout" level="trace"/>
</Logger>
<Logger level="error" name="com.ulisesbocchio.jasyptspringboot">
</Logger>
<Logger level="error" name="org.springframework">
</Logger>
<Root includeLocation="true">
<AppenderRef ref="stdout" />
</Root>
</Loggers>
I also added some code below in my java code
System.setProperty("Log4jContextSelector",
"org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
Seems that the file only roll over when something is written into the file. I think that basically you should do some customization, here are 2 options:
Set a timer, log empty string periodic to trigger the roll over.
Override the RollingFile and roll over file whatever you want.

Log4j2 - Seperate logs for different levels - How do I print just the INFO levels?

How do I print just the INFO levels?
My current log is printing all INFO, WARN, ERROR and FATAL levels to console - I understand why this is happening. (because they are all above INFO)
Is there a way to just specify that just and only just, INFO levels get sent to the console appender and not everything above it?
Currently have the following log4j2 xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="App" packages="">
<Appenders>
<File name="logFile" fileName="C:\Users\user\workspace\logs\log.out">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="com.App" level="info" additivity="false">
<AppenderRef ref="logFile" level="error"/>
<AppenderRef ref="Console" level="info"/>
</Logger>
<Root level="error">
<AppenderRef ref="logFile" />
</Root>
</Loggers>
Need to use a ThresholdFilter;
<ThresholdFilter level="WARN" onMatch="DENY" onMismatch="ACCEPT" />
This blocks the WARN level and everything above it. So now it only accepts info.

Log4j 2 console output stop working

I've been using log4j 2 (2.7) and it worked very well until I had to reinstall the IDE (Eclipse Neon J2EE edition).
Now I only works loggingto a log file but not to the console.
This is my log4j2.xml file:
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="log" fileName="target/test.log" append="true">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M %msg%xEx%n"/>
</File>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
<Root level="TRACE">
<AppenderRef ref="log" />
</Root>
</Loggers>
The output to the log works perfectly, as I said.
Try declaring Root only once.
Example:
<Loggers>
<Root>
<AppenderRef ref="Console" level="error"/>
<AppenderRef ref="log" level="trace"/>
</Root>
</Loggers>
I agree with Sazzad 's answer: you can only have one root logger. I would suggest specifying a level on the root logger and override the level only on the AppenderRef that need it. So:
<Loggers>
<Root level="trace">
<AppenderRef ref="Console" level="error"/>
<AppenderRef ref="log"/>
</Root>
</Loggers>
Also, not sure if this was omitted just from the question or from the actual configuration file, but the configuration really should start with a <Configuration> and end with a </Configuration> element.

Resources