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.
Related
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.
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
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.
I am specifying the filename to log4j2 logger in the XML as below. All works fine but i get the error
2017-09-06 16:54:33,496 main ERROR Unknown object "RandomAccessFile" of type org.apache.logging.log4j.core.appender.RandomAccessFileAppender is ignored: try nesting it inside one of: ["Appenders", "Loggers", "Properties", "Scripts", "CustomLevels"].
<?xml version="1.0" encoding="UTF-8"?>
<Configuration package="log4j.test"
status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{dd/MM/yyyy HH:mm:ss} %c %m%n"/>
</Console>
</Appenders>
<RandomAccessFile name="FILE" fileName="${sys:log4j.saveDirectory}/CMSAutomation.${date:yyyy-MM-dd_hh-mm-ss}.log" append="true" immediateFlush="false">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-6p %C{1}.%t:%L >>> %m%n"/>
</RandomAccessFile >
<Loggers>
<Logger name="log4j.test.Log4jTest" level="debug">
<AppenderRef ref="Console"/>
</Logger>
<Root level="trace">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
This error is coming because RandomAccessFile appender is not defined within <Appenders> XML element.
Below is the correct way of defining any appender including RandomAccessFile appender -
<?xml version="1.0" encoding="UTF-8"?>
<Configuration package="log4j.test"
status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{dd/MM/yyyy HH:mm:ss} %c %m%n"/>
</Console>
<RandomAccessFile name="FILE" fileName="${sys:log4j.saveDirectory}/CMSAutomation.${date:yyyy-MM-dd_hh-mm-ss}.log" append="true" immediateFlush="false">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-6p %C{1}.%t:%L >>> %m%n"/>
</RandomAccessFile>
</Appenders>
<Loggers>
<Logger name="log4j.test.Log4jTest" level="debug">
<AppenderRef ref="Console"/>
</Logger>
<Root level="trace">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
You can check the same in log4j2 documentation here.
Additional one point, you are not using RandomAccessFile appender in any logger. If you will not define it in any logger, it wouldn't work.
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.