How does my spring web app capture errors that I didn't catch and log? Is this a result of apache commons? - spring

I'm confused as to how the errors are logged without me implicitly catching them and logging out the error. All that I've done is put a log4j.xml file in my project defining appenders and now the logs catch and log everything from the frameworks.
If I say, try to query in Hibernate and the query fails, or I try to open a file that doesn't exist, or I get a null pointer exception, if the log4j.xml file defines a log file, and the error level is set correctly, then the error will be captured there?
How does my spring web app capture errors that I didn't catch and log? Is this a result of apache commons logging?
Or is this some magic that log4j knows how to deal with - catch stream to the console etc?
Any info appreciated.

From spring official documentation:
The nice thing about commons-logging is that you don't need anything else to make your application work. It has a runtime discovery algorithm that looks for other logging frameworks in well known places on the classpath and uses one that it thinks is appropriate (or you can tell it which one if you need to). If nothing else is available you get pretty nice looking logs just from the JDK (java.util.logging or JUL for short). You should find that your Spring application works and logs happily to the console out of the box in most situations, and that's important.
To make Log4j work with the default JCL dependency (commons-logging)
all you need to do is put Log4j on the classpath, and provide it with
a configuration file (log4j.properties or log4j.xml in the root of the
classpath).
Take a look for a complete explanation: http://static.springsource.org/spring/docs/3.0.x/reference/overview.html#d0e743

Related

suppress logging in spring batch 3

I am writing a spring-batch application, based on an example I found. When I run it in Eclipse, it produces some annoying INFO logs from deep inside the framework. I'd like to suppress that, so only my System.out statements show - for clarity.
I figured the logging is from the inherited commons-logging1.1.1.jar, that's in my classpath, because when I take out that reference I get errors about Logger.
Question1: Which framework is used for logging in my sample application and how can I change the default INFO to WARN?
Question2: will adopting Log4j just override this default behavior?
I've create two property files under /resources: commons-logging.properties, with this: org.apache.commons.logging.Log=org.apache.commons.logging.im‌​pl.SimpleLog, and simplelog.properties, with this: org.apache.commons.logging.simplelog.defaultlog=warn It worked. Thanks - –

Spring Boot Audit Logging by Example

Almost every aspect of Spring Boot's documentation have proven to be treasure troves of copious amounts of information. That is until I get to Chapter 50: Auditing.
I am trying to understand the 2 paragraphs that make up this entire chapter. If I'm reading it correctly, then when I run my Spring Boot app in "production mode" (that is, as a built/packaged uberjar via java -jar path/to/myapp.jar) then every time an access event (auth attempt/success/fail) occurs, that event will get logged/recorded somewhere.
I haven't done any config whatsoever. I run my app in "prod mode" and log in. I expect to see some console/log output indicating the auth event, but I don't see any. I log out, same deal (no console output). I try to log in with a bad username, and again, nothing in the console output.
Is Spring Boot recording access events somewhere else, besides console/log output? If so, where and why?
Do I need to define any #Beans and register them with some kind of event listener? If so, can someone please provide a succinct code example?
Basically I'm just looking to get Spring Boot's default audit logging pumping events to STDOUT (console). Any ideas?

log4j.properties not being picked in Maven based Application

I have searched enough for the below question but could not find the appropriate answer. I would be glad if you can provide me a link if the question already exists.
I am new to Maven and I have created a Maven based application integrated with Hibernate.
Question:
I have a log4j.properties file in "src/java/resources" I have created a Logger instance in one of my classes and tried to print logs however the logs do not get printed on to the console.
Note: I have given the stdout as logs output location.
src/java/resources/ sounds suspicious. The correct location for classpath resources is src/main/resources/. Put the log4j.properties there.
Additionally, it depends on the version you're using. log4j.properties is no longer supported in newer version. In case you use log4j 2, migrate to log4j2.xml.

Spring Boot Logging Configuration

Is there a way to disable Spring boot logs and print only the logs which i give in the program using logger.info or logger.debug statements. I mean i want only the log statements which i had given in the program to be logged and nothing else. I use the default logging system given by spring boot. If required i can change to log4j or log4j2 as well. Using spring-boot version 1.2.7.
In other way, putting forward , like logging.level.org.springframework can be used to log spring related logs ,is there a way like logging.level.applicationlevel to get application(Java logger statement) logs alone
You can configure your logging in your application.properties like below:
logging.level.com.myapp.packagename=INFO
logging.level.org.springframework=ERROR
INFO means it will print logging of classes in your package and all sub package at INFO/ERROR/WARN level, while for spring related classes only print if there are ERROR level logging.
You can configure logging properties as follows:
logging.level.* = ERROR
logging.level.package_name=DEBUG
Example:
logging.level.com.example.controller=DEBUG
So classes under com.example.controller package will be logged, others only on error will be logged.
I hope this helped!!

Websphere Configuration to Avoid Unwated Warnings in SystemOut.log File

I have a problem about logging in SystemOut.log with Websphere. Can someone help me?
I’m getting some unwanted warning written in the SystemOut.log file with Websphere Application Server (WAS). All of the warnings are being generated by OGNL (ognl.NoSuchPropertyException). These exceptions don’t affect the code flow. Hence I need to turn off the logging of these warnings.
Also I have configuration in log4j.properties file to control the filtration of the log messages to error.
It would be a great help if anyone can help what configuration I should do in WAS to avoid logging these warnings.
Regards
You can change the log level from WAS admin console. Servers->WAS->servername->troubleshooting->change log details level
Follow the path and you will see options to filter warning. For example to filer message from com.ibm.ws.*=WARN will filter all but WARN.
Hope this helps.
First, you have to determine what running component, inside your WAS instance, is generating these warning messages. Is it WebSphere itself, logging these warnings internally? or is it your code?
If the source is WebSphere, then perhaps, before setting the logging level to "error" or "severe", you may want to open a PMR with IBM. I never encountered OGNL warnings generated by WebSphere itself. These warnings, then, can be indicative of a problem in your WebSphere installation.
If the source is your application, then the way to cope with this situation depends on how OGNL, internally, is generating these messages:
If OGNL is simply writing log lines to System.out, then there's nothing you can do to suppress these lines.
If OGNL is logging through Log4J, then you should be able to set the log level of the OGNL logger(s) through your log4j.properties. If your log4j.properties changes aren't reflected, then it means that you have a classloading problem of some sort (the log4j.properties file being loaded by a different classloader than the one used to load your web application).
If OGNL is using a different logging framework (such as SLF4J or Commons Logging), then you'll have to read through the documentation of these frameworks to learn how to tune the logging level.

Resources