Issue in log4j2 Custom Filter - java

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.

Related

Same log4j2.xml for different java processes with separate log level

Problem
I want to standardize my log4j2.xml setup for several Java processes that I run on different linux servers. My hard condition: a single log4j2.xml for all servers.
Sometimes it is needed that I change my loglevel from INFO to DEBUG while the Java process is running. You can do this by changing the logLevel in the log4j2.xml, while you have monitorInterval configured.
However, this changes the loglevel of ALL processes that are running on that server! I need a solution that I can change the logLevel for a single process.
My first try
I thought that I would move the logLevel to the properties. Suppose I have the log4j2.xml (as an example):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Configuration monitorInterval="30">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %t %-5p %-32c{1} - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="${bundle:logger:logLevel}">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Next to this, I also have the file logger.properties on the classpath:
logLevel=INFO
Now when I change my log4j2.xml (by just adding a space somewhere) and updating this property file, the logging changes. However, as mentioned it changes the logLevel of ALL processes.
What I want
I want to be able to have a logger.properties that looks like this:
process1LogLevel=INFO
process2LogLevel=DEBUG
And then something in the log4j2.xml that configures this property dynamically:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Configuration monitorInterval="30">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %t %-5p %-32c{1} - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="${bundle:logger:<something based on the process name>}">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
However, this "flexible" property doesn't really work. Do you have any idea for a solution?
Note
If you have a different idea that looks a little bit different but achieves my goals, that is also fine! Any out-of-the-box solution is always appreciated :)
Found it. You can nest properties. If you specify -Dname=process1 for example, you can use it like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Configuration monitorInterval="1">
<Properties>
<Property name="process1">DEBUG</Property>
<Property name="process2">INFO</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %t %-5p %-32c{1} - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="${${sys:name}}">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

No console output using log4j2 and slf4j

am I missing something using this configuration? I thought that root logger would log all events from error level to the bottom one. If I try to log event at info level, there is no output. This config gives me only error leveled logs. I am using slf4j to create logger objects through Logger-factory class.
<?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>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
For info level logs, change the configuration, in particular Root level to info as show below
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
By changing level to info, the program will start logging both info, error logs in the console.

How to configure java application having 2 log4j2.xml files so that both configurations work fine

We are running a Java file named Calc.java and for this application, we are using log4j2.xml file for logging to a file named LogCalc.txt. Our Calc.java is using a .jar file named Addition.Jar, which is composed of Add.java, which also uses log4j2.xml file for its own logging to a file named LogAdd.txt.
The question here is, when we run Calc.java and access a method from Addition.Jar, logging is only happening in LogCalc.txt, the configurations in log4j2.xml of Calc.java is taken into account and log4j2.xml of LogAdd.txt is not taken into account. Due to this I am not able to get logs from Addition.jar, LogAdd.txt is empty.
How can we change our configurations such that we can see both logs from Calc.java in LogCalc.txt as well as from Add.java in LogAdd.txt, i.e., both XML configurations must work fine in our application and both of them logging to different files with their own configurations taken from respective XML files).
Our aim here is to make a project where main app uses log4j2.xml and also the included .jar in this project is using log4j2.xml, but we should get logs from both of them to their respective appenders separately without any problem.
Our Xml files look like this. log4j2.xml used in Addition.jar is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<File name="A1" fileName="A1.log" append="false">
<PatternLayout pattern="%t %-5p %c{2} - %m%n"/>
</File>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="org.apache.log4j.xml" level="debug">
<AppenderRef ref="A1"/>
</Logger>
<Root level="warn">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
The log4j2.xml file used with Calc.java is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<File name="A1" fileName="A1.log" append="false">
<PatternLayout pattern="%t %-5p %c{2} - %m%n"/>
</File>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="org.apache.log4j.xml" level="debug">
<AppenderRef ref="A1"/>
</Logger>
<Root level="info">
<AppenderRef ref="A1"/>
</Root>
</Loggers>
</Configuration>
Log4j will locate the first log4j2.xml that it finds on the classpath and use that for configuration. It does not currently support multiple configurations, although work is in progress to allow it to. Even if it did it would not work the way your configuration won't work because the loggers are the same.
I suggest you review the log4j guide online. Log4j does not log on the basis of jar files but by comparing the names of the loggers you are using against what is configured. If you want everything in one jar to go to one file then use a logger name such as Calc for the first jar and a logger name of LogCalc for the second jar. Then have your configuration route the events for each of the loggers to the appropriate files.

How to write logs to two different loggers?

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.

Log4j2 Silence specific package

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

Categories

Resources