How to disable request-level logging with the AWS Java SDK? - java

I'm trying to disable request level logging by the AWS Java SDK with Tomcat 8 on Linux. It's a fresh installation of tomcat 8 and my test servlet (which works) just prints to standard out, which by default goes to /var/log/tomcat8/catalina.out.
I'd like to disable the request level logging like - Sending Request... by the AWS SDK, so I've tried adding the following to my logging config at /usr/share/tomcat8/conf/logging.properties:
log4j.logger.com.amazonaws = WARN
log4j.logger.org.apache.http.wire = WARN
log4j.logger.com.amazonaws.request = WARN
...like the docs say here, but it's still doing the verbose logging. My tomcat startup information shows that the logging.properties file is being used:
org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/share/tomcat8/conf/logging.properties
Is there anything else I need to do to?

If you are using Logback, instead of Log4J or Java14 logging, put the following in logback.xml:
<configuration>
...
<logger name="org.apache.http.wire" level="WARN"/>
<logger name="com.amazonaws" level="WARN"/>
...
To specify an external logback.xml and using Spring Boot
-Dlogging.config="C:\logback\logback.xml"
or if you are not
-Dlogback.configurationFile=file:///C:/logback/logback.xml
Logback configuration via jvm argument

I had the same issue, none of the above helped actually.
Creating a logback.xml and putting it on classpath with below config fixed it:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<logger name="org.apache" level="ERROR" />
<logger name="httpclient" level="ERROR" />
</configuration>
Hope it helps the others.

"logging.properties" is the configuration file for Java Util Logging (JUL), witch is a different framework then Log4J. You can try to create a Log4J config file "log4j.properties" in the root of your classpath and insert the code from above: "log4j.logger.com.amazonaws = WARN" .

By Mark
Perhaps I should have been clearer: log4j is not required to control logging in the SDK. The SDK uses Apache Commons Logging, which is, as I already mentioned, an industry standard. Commons Logging is just a dispatch layer to an underlying logging implementation, so that customers can plug in any compliant logging framework and have it work. I used log4j in my examples because it's the one most commonly used, and therefore the most likely to be helpful in this public forum.
If your logging system works with Commons Logging, then you already know how to configure it. If it doesn't, then feel free to turn off Commons Logging altogether by passing the appropriate command-line argument to your JVM, as in my above response. If for some reason you can't change the java command line for your application, then you can use the following snippet in a Main class, to the same effect:
static {
System.setProperty("org.apache.commons.logging.Log",
"org.apache.commons.logging.impl.NoOpLog");
}
Again, to be absolutely clear: the SDK doesn't need log4j. Unfortunately, because all the underlying log implementations are configured differently, this means that we can't tell you exactly how to configure the logging unless we know which implementation your application uses. For that reason, we often use log4j syntax in our examples.
For more information about how Apache Commons Logging works and how to configure it, please read:
http://commons.apache.org/logging/guide.html

Related

How to format the property "sun.net.www.protocol.http.HttpURLConnection.level = ALL" to log4j.xml?

I need to add to my logging.properties the line:
sun.net.www.protocol.http.HttpURLConnection.level = ALL
But this file is generated using log4j.xml, which is in xml format. And I'm not finding how to correctly format this property to xml to generate this line.
using log4j-1.2.8, JRE and JDK 1.8_181, Wildfly 10.1.0.Final
At first glance, you will not be able to configure the logging level for sun.net.www.protocol.http.HttpURLConnection using Log4j, no matter if you use properties or xml configuration files.
As you can see in the source code of the HttpURLConnection class, its logging is based in PlatformLogger, which in turn uses java.util.logging.
The PlatformLogger javadoc describes how it should be configured:
Platform logger provides an API for the JRE components to log
messages. This enables the runtime components to eliminate the
static dependency of the logging facility and also defers the
java.util.logging initialization until it is enabled.
In addition, the PlatformLogger API can be used if the logging
module does not exist.
If the logging facility is not enabled, the platform loggers
will output log messages per the default logging configuration
(see below). In this implementation, it does not log the
the stack frame information issuing the log message.
When the logging facility is enabled (at startup or runtime),
the java.util.logging.Logger will be created for each platform
logger and all log messages will be forwarded to the Logger
to handle.
Logging facility is "enabled" when one of the following
conditions is met:
a system property "java.util.logging.config.class" or
"java.util.logging.config.file" is set
java.util.logging.LogManager or java.util.logging.Logger
is referenced that will trigger the logging initialization.
Default logging configuration:
global logging level = INFO
handlers = java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
Basically, you need to configure the system property java.util.logging.config.file pointing to an appropriate properties file with the required properties:
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=INFO
sun.net.www.protocol.http.HttpURLConnection.level=ALL
Please, consider review this related SO question, it provides different examples and further information about how to do it.
As suggested in that question, you can use the system property javax.net.debug as well:
-Djavax.net.debug=all
If you want to integrate Log4j and java.util.logging, you can try using libraries like SLF4J and the corresponding bridges. This related SO question provides more information.
Remark: Since Log4j 1.2.8 is almost 20 years old, declared end-of-life 8 years ago and with several outstanding vulnerabilities, I don't believe you want to use it.
As you can see in jccampanero's answer, the HttpURLConnection class uses PlatformLogger for logging, which in Java 7 or 8 delegates all logging to java.util.logging.LogManager.
Several alternative implementations of java.util.logging.LogManager exist, but WildFly uses its own implementation called JBoss LogManager, which is the easiest to use. To modify your logging configuration, you just need to edit your server's configuration and add:
<subsystem xmlns="urn:jboss:domain:logging:3.0">
...
<logger category="sun.net.www.protocol.http.HttpURLConnection">
<level name="DEBUG"/>
</logger>
</subsystem>
(cf. documentation).

separate log4j2 files for web applications in single weblogic container

We have following Set up:
Application Server : Weblogic 10.3.6
We have 2 Managed Servers and we have 3 web applications wars deployed on both the managed servers.
All log4j2 jars are kept in server lib and loaded on server start up.
Now we want each web application to have its own logj2 configuration as we want to have control on logs levels.
we made separate log4j2 configuration file for each web application using tried initialising same using log4j2-web.jar in web.xml but we observed that when we load first web application, the log4j2 configuration file get loaded but when we try to load 2nd and 3rd web application,with their separate log4j config xmls,it doesnt load the new config file but sets log4j2 configurations of first web applications which got deployed first.
I tried to debug same and got to the point that as log4j2 jars are present in server class loader so they are common to all web application and log4j2 uses class loader to load logger context.
Now i want each application to have its separate log4j2 implementation which will not be common to other web application.so logging will be different for each application.
Kindly note, i cannot copy log4j2 libraries in WEB-INF/lib of each web applications as we already have size constrain for war.
Kindly suggest how can i implement separate log4j2 configuration for each web application in single weblogic container.
I tried to use JNDIContextSelector but its not working in Weblogic or i dont know how to make it work in weblogic as after doing all the required changes mentioned in log4j2 website,it was not able to find loggers.
Kindly help as i m stuck in same issue from last week.
Unfortunately by far the easiest solution that I am aware of is to include the log4j2 libraries in WEB-INF/lib of each web application...
If the log4j2 jar files are in the shared library directory, they are loaded by the shared classloader, and the configuration is shared between all web applications. This is how web containers and log4j2 are designed...
I would focus on this size constraint you mention. Why is there a size constraint on your war file? Perhaps there is another way to get around this size constraint? Would buying extra hard disk or a bigger server solve the issue? (Hardware is much much cheaper than the time you and perhaps your dev team are spending on this...)
If you are really desperate you can try to have one configuration with differently named loggers that log to different files or filter on different levels. When your application calls LogManager.getLogger, the string you pass becomes the name of the logger. This string can be used in the configuration to control filtering and the target appender. One thing you can do in your application, is giving loggers application-unique names like this:
// App.getPrefix() returns "app1." or "app2." etc: a unique name for each web app
Logger logger = LogManager.getLogger(App.getPrefix() + getClass().getName());
Then have different loggers and appenders in your configuration:
<Configuration status="debug">
<Appenders>
<File name="app1File" ...
<File name="app2File" ...
</Appenders>
<Loggers>
<Logger name="app1" level="TRACE">
<AppenderRef ref="app1File"/>
</Logger>
<Logger name="app2" level="TRACE">
<AppenderRef ref="app2File"/>
</Logger>
</Loggers>
</Configuration>
This is obviously a bit of a hack, and won't work for the classes outside your dev team's control...
You can also ask on the log4j-user mailing list to see if any of the other log4j2 team members have any ideas.
I have the same problem but using Tomcat9. I resolved this by explicitly providing a configuration location to each application when it is initializing logger context through a listener.
URL configLocation = Class.forName("com.example.MyAppListener").getResource("/log4j2.xml");
LoggerContext context = (LoggerContext) LogManager.getContext(false);
context.setConfigLocation(configLocation.toURI());
if(!context.isInitialized()) {
context.initialize();
}else {
context.reconfigure();
}

How to use JBoss logging brought by Hibernate?

I am writing standalone java application which is using Hibernate. Maven brought jboss-logging library for me. I am not using JBoss. The question is: can I log with this library only, or I need to download some logging implementation like log4j?
JBoss Logging is just a logging facade. To configure your loggers, e.g. use/add handlers, you need a log manager like JBoss Log Manager, the J.U.L. log manager, logback or log4j.
JBoss Logging will attempt to discover which log manager is being used. You can specify which log manager you'd like to use with the org.jboss.logging.provider system property. The allowed values for `org.jboss.logging.provider' are:
jboss - for JBoss Log Manager
jdk - For the J.U.L. log manager
log4j - For the log4j log manager
slf4j - For logback with slf4j
Hibernate uses JBoss Logging for it's i18n capabilities, it's vararg logging methods and the ability to not be tied to a log manager.
Of course you can absolutely use JBoss Logging in your project. If you want to configure logging handlers you'd also have to use a log manager as well.
afaik, jboss-logging is more a extra layer on top of normal logging api, to provide more sophisticated feature like i18n etc.
JBoss-logging can use other logging library (e.g. SLF4J) as the underlying handler for log.
I believe if you are writing a simple standalone Java app, you do not need to use JBoss-logging (unless you know you really want and need to do so).
Using SLF4J (with LogBack or Log4J binding) will be a good choice. Visit http://slf4j.org for more information
Make sure you have jboss-logging and your logger implementation in your classpath and set the system property org.jboss.logging.provider to log4j, jdk, slf4j or jboss depending on what you want. In theory autodetection may also work.
https://github.com/jboss-logging/jboss-logging/blob/master/src/main/java/org/jboss/logging/LoggerProviders.java#L29

Filtering out log4j messages from third-party frameworks?

How do I filter log messages from external third party frameworks? I am using Hibernate and Spring framework and I would like to suppress the logs so that only my log4j logs appears.
In my log4j.properties file I set the root logger logging level to ERROR. Then for packages I specifically want to log, like my application code, I set the logging level to INFO or DEBUG.
log4j.rootLogger=ERROR, stdout
log4j.logger.com.initech.tps=DEBUG
log4j.logger.org.hibernate.SQL=INFO
I see co-workers who set root logging low and then end up listing everything they don't want to see, that just seems backward to me. I would rather list what I want to log than all the things I don't want to log.
BTW turning logging off entirely for a third-party component seems like a bad idea to me. For instance, Spring is relatively noisy and uses WARN for things I really don't need to know about, but if it logs an ERROR entry for something I want to see it.
You can do it by changing logger level in log4j.properties/log4j.xml file.
You need to set logger's <level value="off"/> if you want to filter logs from package but keep logger configuration for later use.
You could also set it to highest level to log only in case of error or fatal issue.
Following entries should be added to log4j.xml to turn off logging from hibernate and springframework packages:
<logger name="org.springframework">
<level value="off"/>
</logger>
<logger name="org.hibernate">
<level value="off"/>
</logger>
In log4j.properties you can define individual levels on a per logger basis:
log4j.logger.<name>=FATAL
In log4j.xml the syntax is
<logger name="<name>">
<level value="fatal"/>
</logger>
<name> is often the full qualified classname. You might want to use WARN or ERROR instead of FATAL
Just don't add those packages in your log4j.properties. For instance, you must have this for Spring in your properties file. Take it out if you have some entries like below (anything that starts with org.springframework). Same needs to be done for hibernate.
#Logs the SQL from Spring
log4j.logger.org.springframework.jdbc.core.JdbcTemplate=ERROR
#Logs the SQL parameters from Spring
log4j.logger.org.springframework.jdbc.core.StatementCreatorUtils=ERROR
Also as it was mentioned you should also set the following. I forgot to mention this.
log4j.rootLogger=FATAL
or
log4j.rootLogger=ERROR

How to turn off logging from SLF4J?

It's a third party application generating huge amounts of log entries on our app server. Like this:
[03.03.10 15:21:57:250 CET] 00000180 FtpProtocolHa I org.slf4j.impl.JCLLoggerAdapter info Close connection : 10.227.10.10 - admin
[03.03.10 15:27:35:209 CET] 00000181 MinaFtpProtoc I org.slf4j.impl.JCLLoggerAdapter info [/10.227.10.10] CLOSED
++++
How do I turn this output from SLF4J off? I've looked in the .war file to find some configuration for SLF4J but nothing. Their website didn't help either.
slf4j is just a funnel to the actual log backend (here overriding jakarta commons logging), which is the one you must configure to get rid of a certain kind of messages. For logback this is the appropriate configuration snippet:
<!-- No Tomcat debug logs -->
<configuration>
...
<logger name="org.apache.catalina.core" level="OFF" />
...
</configuration>
For log4j it is very similar.
Alternatively, download http://www.slf4j.org/dist/slf4j-1.6.4.tar.gz, look in there for slf4j-nop-1.6.4.jar (this is the no-operation logger) and include this in your classpath. When the SLF4J classloader sees this (it looks to see what loggers are in the classpath that it can use), it should stop logging (once you've restarted the app).
At least this way you don't need to mess with the log configuration files...
slf4j is a logging facade for various logging frameworks. That output comes from the Apache Commons Loggin framework adapter, that turns to be another facade. Commons Logging Configuration.
Which logging back-end, e.g. logback, log4j, j.u.l., are you using? You need to configure the backend to filter those messages.
Moreover, the fact that the log messages point to "org.slf4j.impl.JCLLoggerAdapter" indicates that caller location inference is not working correctly. (It should mention the actual caller not JCLLoggerAdapter). This can happen if:
you are using an older version of SLF4J
or
the caller is using a slf4j-like wrapper or has its own homegrown logging API which does not infer caller location properly. See also a relevant SLF4J FAQ entry.
As stated by #sparkyspider answer, you can simply add the slf4j-nop library to your application.
If using Maven, add this to your pom.xml file:
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.36</version>
</dependency>
</dependencies>
If using Gradle, add this to your build.gradle[.kts] file:
dependencies {
implementation("org.slf4j:slf4j-nop:1.7.36")
}
Search for the following string: level="DEBUG" using your IDE.
You will find that text in a .xml file.
Go there and use level="INFO" instead of level="DEBUG".
The key value is not case-sensitive.
There can be something like:
<root level="info">
...
</root>

Categories

Resources