Stop log4j debug printing from third party Jars - java

I'm using classes from other Jars which use classes from other Jars that use log4j. I cannot change any of these Jars.
A lot of these classes are printing debug to the console at startup when debugging in IntelliJ.
I'm looking for ways to stop this as it's irritating.
I've tried creating a log4j.properties file with "error" log level and had it load in the main() of my application as well as setting the default logger to "error" level. I also checked and all the Jars have a properties inside but non are specfying what log level (they just say what version the Jar is). I've tried creating my own properties files for the classes to use which works but they've already printed their debug before they load the file.
Here's some of the debug chatter being produced:
12:54:17.660 [main] DEBUG org.apache.commons.configuration.PropertiesConfiguration - FileName set to xxx.properties
12:54:17.665 [main] DEBUG org.apache.commons.configuration.ConfigurationUtils - ConfigurationUtils.locate(): base is null, name is xxx.properties
12:54:17.665 [main] DEBUG org.apache.commons.configuration.DefaultFileSystem - Could not locate file xxx.properties at null: no protocol: xxx.properties
12:54:17.668 [main] DEBUG org.apache.commons.configuration.ConfigurationUtils - Loading configuration from the path xxx.properties
12:54:17.668 [main] DEBUG org.apache.commons.configuration.PropertiesConfiguration - Base path set to file:///C:/myProgram/xxx.properties
12:54:17.680 [main] DEBUG org.apache.commons.configuration.PropertiesConfiguration - FileName set to yyy.properties
12:54:17.680 [main] DEBUG org.apache.commons.configuration.ConfigurationUtils - ConfigurationUtils.locate(): base is null, name is yyy.properties
12:54:17.681 [main] DEBUG org.apache.commons.configuration.DefaultFileSystem - Could not locate file yyy.properties at null: no protocol: yyy.properties
12:54:17.681 [main] DEBUG org.apache.commons.configuration.ConfigurationUtils - Loading configuration from the path yyy.properties
12:54:17.681 [main] DEBUG org.apache.commons.configuration.PropertiesConfiguration - Base path set to file:///C:/myProgram/yyy.properties
Here are my .properties files:
---log4j.properties---
log4j.rootLogger=error
---xxx.properties---
log4j.rootLogger=error
---yyy.properties---
log4j.rootLogger=error
my main method starts like this:
public static void main(String[] args) {
PropertyConfigurator.configure("log4j.properties");
Logger.getRootLogger().setLevel(Level.ERROR);
The log4j.properties file is being loaded fine with no issue, and is happy with "log4j.rootLogger=error". (if I add ', R' or ', stdout' like I've seen in other SO questions it complains but I suspect that is due to me being on Windows ).
If this isn't possible, let me know and I'll just accept it, live with it and stop looking.
This project is run by a large audience using windows, mac and linux. (the project is a test harness for developers who need to run the collection of Jars do develop new ones for the collection)
--- edit ----
I've investigated the suggestion of putting a log4j.xml in my project's src/main/resource folder but can't get it to work.
Everything I read indicates the log4j.xml should work but I'm running out of things to try. As a last ditch attempt I tried to google:
"org.apache.commons.configuration" debug stop
and google sent me to my own question haha
Here's my log4j.xml which doesn't seem to be stopping the org.apache.commons.configuration from each jar debug printing every time those classes are initilised (which happens a few hundred times a second).
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender name="CA" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n"/>
</layout>
</appender>
<logger name="org.apache.commons.configuration">
<level value="error"/>
</logger>
<root>
<priority value="off"/>
<appender-ref ref="CA"/>
</root>
</log4j:configuration>
I've rebuilt my project and repackaged my jar. I'm hoping I don't have to put the log4j.xml into each Jar's resources folder? :/
Is there a way to see if it's loading the log4j.xml file?
Is there something I need to do to make all the Jars which are presumably instantiting their own instances of log4j use this log4j.xml?
I tried this now for my main method:
public static void main(String[] args) {
PropertyConfigurator.configure("log4j.xml");
Logger.getRootLogger().setLevel(Level.ERROR);
but I am still seeing the console spammed with DEBUG messages from org.apache.commons.configuration
-- edit 2 --
Still stuck. I tried simplying to a really basic example and DEBUG output is still appearing in the console.
Here is my simple main method:
public static void main(String[] args) throws ConfigurationException {
PropertyConfigurator.configure("log4j.xml");
Logger.getRootLogger().setLevel(Level.ERROR);
PropertiesConfiguration config = new PropertiesConfiguration("log4j.xml");
}
Here are the imports:
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
3rd line causes this output to appear:
10:04:08.526 [main] DEBUG org.apache.commons.configuration.PropertiesConfiguration - FileName set to log4j.xml
10:04:08.536 [main] DEBUG org.apache.commons.configuration.ConfigurationUtils - ConfigurationUtils.locate(): base is null, name is log4j.xml
10:04:08.536 [main] DEBUG org.apache.commons.configuration.DefaultFileSystem - Could not locate file log4j.xml at null: no protocol: log4j.xml
10:04:08.546 [main] DEBUG org.apache.commons.configuration.ConfigurationUtils - Loading configuration from the path log4j.xml
Here is the log4j.xml which I confirmed is being loaded by renaming it and seeing a load file error.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<root>
<level value="error"/>
<priority value="off"/>
</root>
<logger name="org.springframework">
<level value="error"/>
<priority value="off"/>
</logger>
<logger name="org.apache.commons.configuration">
<level value="error"/>
<priority value="off"/>
</logger>
<logger name="org.apache.commons.configuration.PropertiesConfiguration">
<level value="error"/>
<priority value="off"/>
</logger>
<logger name="org.apache.commons">
<level value="error"/>
<priority value="off"/>
</logger>
<logger name="org.apache">
<level value="error"/>
<priority value="off"/>
</logger>
</log4j:configuration>

I created a file called "logback-test.xml" and tried placing it in every possible folder I could think of, finally found if I put it into here "Log4JTest\out\production\Log4JTest" (where Log4JTest is the name of my test IntelliJ project) then it is found.
The loopback-test.xml has the following contents:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<logger name="org.apache" level="OFF"/>
</configuration>
and it seems to have done the job!
Here's my Main.java
package com.company;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
public class Main {
public static void main(String[] args) throws ConfigurationException {
PropertiesConfiguration config = new PropertiesConfiguration("log4j.xml");
}
}
and here's the output console:
Connected to the target VM, address: '127.0.0.1:50725', transport: 'socket'
Disconnected from the target VM, address: '127.0.0.1:50725', transport: 'socket'
Process finished with exit code 0
Now to test on the full project... will mark this as 'solved' if it solves the original problem.

Related

Configure Wiremock to use log4j with a Custom Transformer

I'm running Wiremock as a Standalone process (v2.5.1). I've created a Java custom transformer by extending: com.github.tomakehurst.wiremock.extension.ResponseTransformer
My custom transformer then uses some other common code that uses Log4J for logging. With code like:
import org.apache.log4j.Logger;
private static Logger logger = Logger.getLogger(CommonCode.class);
...
logger.error("This is some error");
Is there anyway I can configure Wiremock to output this custom logging? I've tried putting a log4j.xml and log4j.properties file in the classpath. Here's an example of a properties file:
log4j.appender.CUSTOMAPPENDER=org.apache.log4j.RollingFileAppender
log4j.appender.CUSTOMAPPENDER.File=c:/WireMock/logs/custom.log
log4j.appender.CUSTOMAPPENDER.layout=org.apache.log4j.PatternLayout
log4j.logger.com.myorg=DEBUG, CUSTOMAPPENDER
The equivalent log4j.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="CUSTOMAPPENDER" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="C:/WireMock/logs/custom.log"/>
<param name="datePattern" value="'.'yyyy-MM-dd"/>
<param name="append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM dd HH:mm:ss}] [%r] %5p [%t] (%F:%L) - %m%n"/>
</layout>
</appender>
<logger name="com.myorg" additivity="false">
<level value="DEBUG"/>
<appender-ref ref="CUSTOMAPPENDER"/>
</logger>
</log4j:configuration>
I also have the following JARs in my classpath:
log4j-1.2.17.jar
slf4j-api-1.7.2.jar
slf4j-log4j12-1.7.2.jar
I'd like this custom logging to go to a separate log file from the Default Wiremock verbose logging. Any help would be appreciated.
Once I realised it was simply that log4j could not find the log4j.properties or log4j.xml file I was able to resolve the issue. Basically the log4j properties file must be in the classpath for the Wiremock Java process.
I was running the Wiremock Server via a batch script which looked like this:
"C:/Java/jdk1.8.0_102/bin/java" -Dfile.encoding=UTF-8 -cp "C:/WireMock/lib/wiremock-standalone-2.5.1.jar;C:/WireMock/lib/*" com.github.tomakehurst.wiremock.standalone.WireMockServerRunner --port="9091" --extensions "com.myorg.CustomTransformer" --root-dir="C:/WireMock" --verbose > "C:/WireMock/logs/wiremock.log"
My log4j.xml file was in my C:/WireMock/lib directory. I assumed that because I had a classpath entry of cp "C:/WireMock/lib/wiremock-standalone-2.5.1.jar;C:/WireMock/lib/*" which included C:/WireMock/lib/* that it would pick up my log4j properties. I also explicitly added the properties file to this classpath. Neither of these worked (hence this question). My Custom transformer class com.myorg.CustomTransformer is present in a jar also in the lib directory (which is picked up).
However after some research I found 2 ways to get the log4j properties file to be picked up.
Specify the log4j properties location as another -D parameter in the batch script. If I added -Dlog4j.configuration=file://c:/WireMock/lib/log4j.xml
Update my custom jar build script to include the log4j.xml file itself in the jar.

Connection to database in a managed thread, Debug values doesn't appear in Tomcat console or logs

I am facing this trouble for a long time now without accessing the debug logs of the managed Threads in my Spring boot application when run on Tomcat. All the logs appear when run on the Eclipse/STS.
In Tomcat logs, I can only see the main Tread Logs.
I am connection to a database through JDBC and this is happening in a separate thread. I tried to follow the log configuration documentation but none of them helps to get the debug logs of these threads. So I do not actually see the exact problem of what is causing the connection to fail.
Here is what I tried so far:
I tried with the following logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<Target>System.out</Target> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p
%c{1}:%L - %m%n</pattern> </encoder> </appender> <logger name="com.biscoind"
additivity="false" level="TRACE"> <appender-ref ref="stdout" /> </logger>
<root level="debug"> <appender-ref ref="stdout" /> </root> -->
<include resource="org/springframework/boot/logging/logback/base.xml" />
<logger name="org.springframework.web" level="DEBUG" />
</configuration>
When that did not resolve the issue I removed this file and see if by default, if it logs all the treads. But it did not.
So, I added the following configurations to the application.properties
logging.level.org.springframework.web:TRACE
logging.level.org.hibernate:ERROR
Then It seemed to me that this is only logging out the above namespaces, I again added
debug=true
logging.level.org.springframework.web:DEBUG
logging.level.org.hibernate:DEBUG
Tried and it did not work.
I added my namespaces also and tried as follwing,
debug=true
logging.level.com.mydomain:DEBUG
logging.level.org.springframework.web:DEBUG
logging.level.org.hibernate:DEBUG
That did not work also, I am now confused on the what should I do with the config relative to logging to make the logs to appear for the tread executions.
Irrespective of the treads, because of the property spring.jpa.show-sql=true it logs the queries that are made.
It was not a problem with the threads at all. The application was working correctly in the development environment. The problem was in the deployment environment.
It turned out to be a Java version miss-match with the jar files and the JVM version. The jars were build using Java 8 and it was running on Java 7 JVM.
When the JMV was changed to Java-8. It worked fine. So Next time I will be more careful with the version mismatch.

Disable highly verbose logging

I am getting this highly verbose logging and I want to get rid of it.
1:39:20.187 [main] INFO c.v.c.c.ConfigManager - End XML Read
11:39:20.187 [main] INFO c.v.c.c.ConfigManager - The content of ConfigCfg_pdMetering.xml is ConfigCfg [xxx=yyy]
11:39:37.335 [Thread-1] DEBUG o.a.h.c.p.RequestProxyAuthentication - Proxy auth state: UNCHALLENGED
11:39:37.335 [Thread-1] DEBUG o.a.h.i.c.DefaultHttpClient - Attempt 1 to execute request
The logging comes from a jar I have imported in the project. I modified the logback.xml and log4j.properties in the jar using "jar uf"
logback.xml now looks like:
<?xml version="1.0"?>
<configuration debug="false">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are by default assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<logger name="com.hccl" level="ERROR"/>
<logger name="org.apache.http.wire" level="ERROR"/>
<root level="debug">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
and log4j.properties:
log4j.rootCategory=INFO, stdout
log4j.logger.org.springframework.ws.client.MessageTracing.sent=ERROR
log4j.logger.org.springframework.ws.client.MessageTracing.received=ERROR
log4j.logger.org.springframework.ws.server.MessageTracing=ERROR
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%p [%c{3}] %m%n
log4j.logger.httpclient.wire=ERROR
I imported this modified jar and imported it to my project. This has not affected the logging.
Also I am getting the following in the console:
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
I do not know why I am seeing this warning.
Muting Apache http
The packages you want to mute are c.v.c.c. and o.a.h. Those are abbreviations. Also, you specified <logger name="org.apache.http.wire" level="ERROR"/> which doesn't match the above (take the first letters of each package name).
You might have more luck with
<logger name="org.apache.http" level="ERROR"/>
to shut down all org apache http logging (which is probably o.a.h.*).
Muting c.v.c.c
To mute the first two lines beginning with c.v.c.c.ConfigManager, you need to find out the classes. You can do this either by replacing %logger{36} with just %logger to have the full package and class name printed. Or perhaps it's your own class. In this case you can just putt a corresponding logger entry into your file by yourself.
Warning for slf4j-Binding
If slf4j outputs warnings about "actual bindings", you have probably more than one slf4j binding in your classpath. As I read from your question, you try to use both logback and log4j, which won't work - slf4j decides to use logback only. Remove log4j and the log4j.properties. If they get included by maven dependencies, specify exclude tags for those dependencies.

Log4j log append to file name dependent on day - config

I'm fairly new to log4j and I would like to set up automatic logging of anything output to the console, like errors, or info messages for a web app running on my local server.
How would I set up my xml and/or properties file to do this? What I really am looking for is that each day a new log file be created in a directory (Ex: /mylogs/app-log-01-08-2014.log)
I've begun like this:
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- general application log -->
<appender name="BarLogFile" class="org.apache.log4j.FileAppender">
<param name="File" value="my-changing-file-name.log" /> ->>> how does this change
<param name="Threshold" value="INFO" /> ->>> should INFO be Console here?
</appender>
<logger name="what-goes-here?">
<appender-ref ref="something-here"/>
</logger>
<root>
<level value="INFO"/>
</root>
Also, where in a web project does the xml file go? WEB-INF?
Any help is appreciated.
You are looking for what is known as "daily rolling log files", configuration is shown here. As for the web project, see this answer.
On a side node, you might want to learn about Logback as alternative.

Excluding only one level of Log4j Logger

I'm using Log4j in a application in which I also use Axis2 and Jetty web server.
I configured the Log4J property file to exclude these classes from logging when in debug priority. But when I did this, the other priority messages also began to be excluded from the main logger.
Is there a way that I can tell Log4j that I just want to log INFO logs from these classes while logging debug logs from my classes?
Here's what I have done:
#Jetty Server and Axis2
log4j.category.org.apache.axiom=DEBUG
log4j.additivity.org.apache.axiom=false
log4j.category.org.apache.axis2=DEBUG
log4j.additivity.org.apache.axis2=false
################# MAIN LOGGER #################
log4j.rootCategory=DEBUG, mainLogger
#File configuration
But as I said this configuration also exclude INFO messages from the main logger.
No, set the Root level to DEBUG, and
log4j.category.org.apache.axiom=INFO
log4j.category.org.apache.axis2=INFO
Also, do not set the additivity to false.
When you're starting from scratch, you might want to use the more modern XML config format right away. There, it would look like this:
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
...
</appender>
<category name="com.apache.axiom">
<priority value="INFO" />
</category>
<root>
<priority value ="DEBUG" />
<appender-ref ref="FILE" />
</root>
</log4j:configuration>
Set the rootCategory to INFO, set an apropriate Logger of your classes (e.g. org.myemployer.myapp) to DEBUG
Using this you can set your log level:
import org.apache.log4j.Level;
public static Logger logger = null;
logger.setLevel(Level.DEBUG);
and
Logger.getRootLogger().setLevel(Level.INFO);

Categories

Resources