Can't print to console with log4j - java

My problem is that I want to know how many connections are opened to the oracledb in an oracle datasource pool (oracle.jdbc.pool.OracleDataSource) and I want to print this information to the console using log4j.
I defined my pool in a spring configuration file:
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource" >
<property name="dataSourceName" value="ds" />
<property name="URL" value="jdbc:oracle:thin:#something" />
<property name="user" value="user" />
<property name="password" value="password" />
</bean>
I would pass somehow the log's informations in my ojdbc6 or ojdbc14 jars to the console with log4j as I said but my log4j doesn't print anything.
In my log4j.properties I have:
log4j.logger.oracle.jdbc.pool=ALL, jdbc
log4j.appender.jdbc=org.apache.log4j.ConsoleAppender
log4j.appender.jdbc.layout=org.apache.log4j.PatternLayout
log4j.appender.jdbc.layout.ConversionPattern=JDBC | %5p | %d{HH:mm:ss,SSS} > %m - [%l]%n
I don't know if there are these kind of informations in ojdbc bundles, ok and if you know how can I have you're welcome, but my problem is that I can't see any logs. Maybe there aren't any logs at all...
Thank you for your attention.

there is no guarantee oracle uses log4j, you should try to configure java util logging settings. This link seems to suggest oracle uses java util logging. Alternatively you could build a proxy class or extend this class and write your own log4j messages before/after delegating back to the oracle connection pool.

Check this page to initialize the configurator. If you don't initialize it properly log4j wont log messages. Most importantly try to use log.debug, log.warn in the business logic which makes use of oracle objects. Log4j is a just a logging library which you can use through java. Not sure if oracle dumps it via that.
There are other tracing and logging tools if you think its not working fine. My first pointer would be try a simple app in your application to see if your console gets the log4j messages when you run them after using the configurator i mentioned previously.

I found the answer, only some ojdbc bundles are able to print logs.
I have ojdbc6 and ojdbc14 jars and they haven't this ability.
From this link.
When full logging is enabled, it is almost guaranteed that all sensitive information will be exposed in the log files. This is intrinsic to the logging feature. However, only certain JDBC JAR files include the JDBC logging feature. The following JAR files include full logging and should not be used in a sensitive environment:
ojdbc5_g.jar
ojdbc5dms_g.jar
ojdbc6_g.jar
ojdbc6dms_g.jar
The following JAR files include a limited logging capability:
ojdbc5dms.jar
ojdbc6dms.jar
I'm done :P

Related

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

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

JmxResource published with simplejmx does not appear in JConsole

I am using simplejmx to publish my JMX Resources.
I have got jmx-config.xml
<bean id="beanPublisher" class="com.j256.simplejmx.spring.BeanPublisher">
<property name="jmxServer" ref="jmxServer" />
</bean>
<bean id="jmxServer" class="com.j256.simplejmx.server.JmxServer"
init-method="start" destroy-method="stop">
<property name="registryPort" value="8123" />
</bean>
I am starting my JBoss application, everything is ok:
15:20:11,860 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] (MSC service thread 1-8) Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory#1be30160: defining beans [...,beanPublisher,jmxServer,...]; root of factory hierarchy
I created a simply class.
package com.mypckg.jmx;
import com.j256.simplejmx.common.JmxAttributeField;
import com.j256.simplejmx.common.JmxResource;
#JmxResource(description = "Blah1", domainName = "Blah2", beanName = "Blah3")
public class DummyJMX {
#JmxAttributeField(description = "Blah4")
private int var = 3;
}
I am starting JConsole, I am choosing JBoss application and I am going to MBeans. That is what I see:
*
Probably, my DummyJMX class has not been published (or I just cannot find it).
About which step I forgot?
Thank you in advance
EDIT :
EDIT :
#Andrei Stefan
An error which I got using your link:
#Gray
An error which I got using localhost:8123:
Try the following url in JConsole, with Remote Process option: service:jmx:rmi:///jndi/rmi://localhost:8123/jmxrmi
It's a bit different than what I provided in the comments.
Finally, I am connected to my JMX Beans using JConsole.
Probably, I did something wrong in the beginning of my work with simplejmx.
I have not changed a lot of things. I kept jmx-config file and I still use version 1.8 of simplejmx.
I can easily connect to this bean locally - I have no idea why I was not able to do that earlier. Could you tell me, why in your opinion it should not be a local process?
Below, you can see that my JMX Bean appears in JConsole:
Probably, my DummyJMX class has not been published (or I just cannot find it).
When you are using the registryPort configuration for JmxServer then it will not show up in the "Local Process" list under Jconsole. It will be able to be accessed as a "Remote Process" with localhost:8123. If you are on a Linux box, you might use netstat -an | grep LISTEN to see what ports your application is listening on. If you don't see 8123 in the list then maybe it already has a RMI server configured?
If you want to use the platform mbean-server which does show up as a local process then use the new setter or constructor in version 1.9 which was released recently (4/2014). Unfortunately, SimpleJMX cannot programmatically register itself so it shows up in the process list -- that's not code that the JVM exports.
<bean id="jmxServer" class="com.j256.simplejmx.server.JmxServer"
init-method="start" destroy-method="stop">
<property name="usePlatformMBeanServer" value="true" />
</bean>

separating database tasks from spring petclinic app

I want to turn off the spring petclinic's automatic recreation and repopulation of its underlying MySQL database every time the app restarts. Can anyone show me how to do this?
The web.xml for the app can be found at this link. And the other xml config files can be found at this link.
I prefer to run database scripts separately from the application, using the MySQL command line client.
In the file datasource-config.xml locate the following configuration
<jdbc:initialize-database data-source="dataSource">
<jdbc:script location="${jdbc.initLocation}"/>
<jdbc:script location="${jdbc.dataLocation}"/>
</jdbc:initialize-database>
Just comment out this code, and you should be good to go.

db.properties vs persistence.xml which one is better?

Recently I started a maven project to build an application for integrating
Spring, JPA, JSF
But in the automatically generated folder structure I can see one file named as
db.properties
and also I have one
persistence.xml
Now my question is that Database connection can be defined in either of these files, Can anybody tell me
1. Which way is better and why ?
2. Why there is db.properties file automatically generated while I already have persistence.xml ?
db.properties file is like messages.properties which is used to define key value pair. And after that we will use keys in expression language. So configurations will only be done in
persistence.xml or dataSource.xml
whichever is preferred choice but the values we will take from db.properties in the form of expression language eg.
driverClassName=com.mysql.jdbc.Driver
this is an entry in your db.properties. and you will use it in persistence.xml as follows.
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driverClassName}" />
I assume, from the fact that you mention JSF, that you are building a web application for deployment to an application server. I also caveat this question in that I don't know about db.properties or where it comes from.
When deploying to an application server, it is always best to configure your database connections in the container and expose them to the application via JNDI. This allows the container to manage connection pooling and credentials and keeps this information out of your WAR/EAR files. It also ensures that your WAR/EAR files are agnostic to the particular database instance, so can be deployed to a container in any environment without modification.
Therefore, I recommend against configuring your datasource in persistence.xml.
See also Difference between configuring data source in persistence.xml and in spring configuration files which is a similar question- the accepted answer there expresses the solution in more detail.

Custom configuration for JBoss applications?

I've built a simple alert monitor to display the health of various applications. This is configured by XML, as each monitor instance needs to show different metrics. An example configuration may be:
<machine>
<monitors>
<check type="connectivity" name="Production Server">
<property key="host" value="ops01.corp" />
<alarm />
</check>
</monitors>
</machine>
Currently, I'm storing this in the root of the C:\ drive of the server. What would be nice is if I could put it in the deploy directory of the JBoss server, and could somehow get a reference to it. Is this possible? I looked at MBeans but it didn't seem to support complex XML structures.
Robert
Try JOPR - http://www.jboss.org/jopr .
For custom metrics, you can write your own plug-in.
You can get an input stream for any file in the classpath by using the ClassLoader#getResourceAsStream(String name) method. Just pass the location of the file relative to the classpath.

Categories

Resources