I've just updated to Log4. Now i have some small issues configurating my new setup.
The log4j2.xml is quite small and simple, but i'm not sure how i can disable logs (or at least set them to ERROR) for all jersey packages.
Here's my log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d %p %c{1.} [%t] %m%n" />
</Console>
<File name="FILE" fileName="/logs/m2m/error.log">
<PatternLayout pattern="%d %p %c{1.} [%t] %m%n" />
</File>
<Async name="ASYNC">
<AppenderRef ref="FILE" />
<AppenderRef ref="CONSOLE" />
</Async>
</Appenders>
<Loggers>
<Logger name="org.glassfish.jersey.servlet" level="ERROR" additivity="false">
<AppenderRef ref="ASYNC" />
</Logger>
<Root level="DEBUG">
<AppenderRef ref="ASYNC" />
</Root>
</Loggers>
</Configuration>
And here's the log-output i try to silence:
Jan 24, 2014 8:21:12 AM org.glassfish.jersey.servlet.WebComponent filterFormParameters
WARNING: A servlet request to the URI #### contains form parameters in the request body but the request body has been consumed by the servlet or a servlet filter accessing the request parameters. Only resource methods using #FormParam will work as expected. Resource methods consuming the request body by other means will not work as expected.
Thanks
Based on the format of the output I suspect that the log output shown above is actually produced by JUL (java.util.logging) and not by log4j.
It is possible to route calls made to the java.util.logging API to the log4j2 implementation. Your log4j2 configuration (which looks correct) should then filter out WARN-level log events emitted by any class in the org.glassfish.jersey.servlet package.
This involves adding a few more jars to the classpath: see the FAQ.
If you want to do with Logback, here I explain how to do it:
Excessive warning messages from Jersey 2.x
Related
I am trying to implement custom filter as mentioned in post Log4j2 custom filter .My lo4j2.xml is as follows
<?xml version="1.0" encoding="UTF-8"?>
<!-- Logger configuration when running outside of docker -->
<Configuration status="trace" packages=“com.rest.server_common.logging">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c{1}:%L - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="DEBUG">
<com.rest.server_common.logging.MyCustomFilter level="DEBUG"/>
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
When I start my jetty server,I get the error
main ERROR Root contains an invalid element or attribute "com.rest.server_common.logging.MyCustomFilter"
What is the problem here?
I have even tried moving filter line
<com.rest.server_common.logging.MyCustomFilter level="DEBUG"/>
after Configuration element but I still get the error
Just to close.The package was in different module so the filter was not getting recognised.
I'm trying to push logs to an ActiveMqueue using JMS in log4j2.
I have done this in my log4j2.properites
<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="5">
<Appenders>
<RollingFile name="fishLogstash"
fileName="E:/xxx/log/xxx.server/xxxLogstash.log" append="false"
immediateFlush="false" bufferSize="1000"
filePattern="/soft/log/xxx.server/xxxx-%i.log">
<PatternLayout pattern="%-d{yyyy-MM-dd HH:mm:ss.SSS} %5p %c{1} - %m%n" />
<Policies>
<SizeBasedTriggeringPolicy size="100M" />
</Policies>
<DefaultRolloverStrategy max="10" />
</RollingFile>
<JMS name="jmsQueue" destinationBindingName="logQueue"
factoryName="org.apache.activemq.jndi.ActiveMQInitialContextFactory"
factoryBindingName="ConnectionFactory"
providerURL="tcp://localhost:61616">
<PatternLayout pattern="%-d{yyyy-MM-dd HH:mm:ss.SSS} %5p %c{1} - %m%n" />
</JMS>
</Appenders>
<Loggers>
<Root level="warn">
<AppenderRef ref="fish" />
</Root>
</Loggers>
</Configuration>
After this, I wonder how to use the JMS appender to log in my java code? How to retrieve this specific appender, is there somthing like
Logger log = Logger.getAppender("jmsQueue") ?
Thanks in advance.
1.
In your property file, there are xml content. So I am assuming you are trying to use xml style property file. If so, rename your property file to have .xml extension. E.g. log4j2.xml. Remember to write log4j2 supported xml. Examples can be found here.
2.
Mention name value of your defined appenders as AppenderRef in Loggers section of property file. Based on your Appenders section, Loggers section can be
<Loggers>
<Logger name="jmsLogger" level="warn">
<AppenderRef ref="jmsQueue" />
</Logger>
<Root level="warn">
<AppenderRef ref="fishLogstash" />
</Root>
</Loggers>
3.
In your code, get JMS logger as follows:
Logger log = LogManager.getLogger("jmsLogger");
You can log whatever by using log variable. E.g.
log.info("some message");
Hope, this answer would help you.
Im able to create two logs using log4j2 and im able to write the log to a single log file. how to write logs to a two different loggers?
i modified the log4j2.xml file to have two loggers.
Any sample example?
Instead of configuring multiple loggers, you may want to configure multiple appenders. Example:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Appenders>
<File name="MyFile" fileName="logs/app.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
<File name="Other" fileName="logs/other.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="MyFile" level="trace"/>
<AppenderRef ref="Other" level="debug"/>
</Root>
</Loggers>
</Configuration>
Point 1
I guess you're asking to write one log into different destinations, ex. different files. As Remko has mentioned, you could configure different appenders to handle each of the log file.
As I don't have enough reputations for commenting Remko's answer, here're some of my additional examples:
If you use the level of DEBUG for your logs, calling logger.debug("your message") and configure your log4j2.xml like the below, you'll see "your message" logged in both file1.log and file2.log.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<File name="File1" fileName="logs/file1.log">
<PatternLayout>
<Pattern>"Add your pattern here"</Pattern>
</PatternLayout>
</File>
<File name="File2" fileName="logs/file2.log">
<PatternLayout>
<Pattern>"Add your pattern here"</Pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Root level="debug">
<!--This will write the same log into both of the files -->
<AppenderRef ref="File1" level="debug"/>
<AppenderRef ref="File2" level="debug"/>
</Root>
</Loggers>
</Configuration>
Point 2
we should notice that, in Remko's example, all logs go into Other will also be written into MyFile. See more in Log4j2 Documentation, example 6 for explaination. In short, as TRACE is a lower lever than DEBUG, all logs with DEBUG levels could be passed in TRACE and logged.
Point 3
Another possibility of writing into different destinations is through Appender Additivity.
Each enabled logging request for a given logger will be forwarded to
all the apprenders in that Logger's LoggerConfig as well as the
Apprenders of the LoggerConfig's parents.
In log4j I can specify file like this:
log4j.additivity.org.springframework.ws.server.MessageTracing=false
log4j.logger.org.springframework.ws.server.MessageTracing=TRACE, AUDIT
log4j.additivity.org.springframework.ws.client.MessageTracing=false
log4j.logger.org.springframework.ws.client.MessageTracing=TRACE, AUDIT
when I want to log into appender named AUDIT.
How should I rewrite this into log4j2 xml configuration?
UPDATE
I have defined root logger like:
<Root level="error">
<AppenderRef ref="console" />
<AppenderRef ref="syslog" />
</Root>
and some of class I want to log into app file. In log4j:
log4j.logger.org.springframework=WARN, APP
log4j.logger.org.my.project=DEBUG, APP
but in log4j2 I have no idea how to rewrite in in one sentence
Have you tried something like the following (you need to first define your "app" appender):
<Appenders>
<File name="app" fileName="logs/app.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
</Appenders>
Then define your logger's appender:
<logger name="org.my.project" level="debug">
<appender-ref ref="app" />
</logger>
I'd like my application to run at Debug level, except for Spring, which is producing a huge amount of logging statements, which makes it hard to read through the logs. I've currently configured my Log4j2 file like this:
<Configuration status="debug">
<Appenders>
<RollingFile name="systemLog" fileName="C:/test/logs/system.log" includeLocation="true"
filePattern=""C:/test/logs/system-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout pattern="%d{ISO8601} - %-5level [%t] %C %M %m%n" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="15 MB"/>
</Policies>
</RollingFile>
<Async name="systemAsyncAppender" includeLocation="true">
<AppenderRef ref="systemLog" />
</Async>
</Appenders>
<Loggers>
<SpringLogger name="org.springframework.*" level="error" additivity="false">
<AppenderRef ref="systemAsyncAppender" />
</SpringLogger>
<Root level="debug" includeLocation="true">
<AppenderRef ref="systemAsyncAppender" />
</Root>
</Loggers>
</Configuration>
I thought that setting up the SpringLogger would set Spring to only run at ERROR level and, and write to the same log as everything else, but I'm still seeing full Spring DEBUG output. I have vague memories of being able to do this very easily with the old log4j.properties files (I think it was as simple as log4j.category.org.springframework=ERROR), but I'm not sure how to do this with Log4J2.
(Using Log4J2 2.0.2, spring 3.2.11)
I don't think log4j2 has any special configuration for Spring, so <SpringLogger> doesn't seem appropriate here.
Instead, just declare a regular logger
<logger name="org.springframework" level="error" additivity="false">
...
</logger>
Also note the logger name. These are hierarchical. You don't need the * (and I think it actually breaks it).