Log fields/variables to file in web application - java

I have log4j2 configured in my web app which logs debug/info messages to console. I want to use a logger to write specific fields which are received in each http request to a new file. I understand that adding a file appender to existing log4j2.xml file would write all the log messages where logging is configured, but I want to use this new logger only to write specific fields.
I guess a rollingfile appender will help, which creates a new file for every request and save some input fields received as part of that request.
Thank you.

You have full control on what you log.
Use the logger to specifically log all the fields you need.
All the fields you do not specifically log, are not logged.
For example the following line will log only SelectedField1 and SelectedFieldN.
All other fields in the application will not be logged.
logger.debug("SelectedField1:"+selectedField1+"SelectedFieldN:"+selectedFieldN);

Related

ThreadLocal guard in Logback appender base classes

ThreadLocal guard is utilized in doAppend method of Logback base appenders to prevent calling back on itself.
Under what circumstances would doAppend call back on itself?
Under what circumstances would doAppend call back on itself?
Anytime the Appender::append creates logging events that feedback into the appender itself. This is any Appender makes method calls against a 3rd party library which in itself uses a logging framework that uses slf4j or can be bridged to logback/slf4j.
Take the SMTPAppender as an example. Per the docs:
The SMTPAppender relies on the JavaMail API.
If the following statements are true then you have a feedback loop:
An ancestor of the JavaMail logger namespace has an attached SMTPAppender, say the root logger for example.
JavaMail session debugging has been enabled which will generate JUL log records on SMTP send.
The sl4fj JUL bridge handler is installed and is redirecting the JUL log records to the already attached SMTPAppender in point 1.
There is a running thread that tries to generate the first log message.
The SMTPAppender attempts to send email message. This action will generate more messages that feedback into logger in point 1.
When all of this is true, your application log message that you wanted to send via email ended up creating X number of log message debug statements from JavaMail. Those X number of message now end up calling SMTPAppender::append and so on.
This same type of example could be applied to the DBAppender which requires a JDBC driver that could be directly logging to slf4j. Same rules for any custom appenders applies here too.
In general, you don't what your application logs to be mixed with this appender generated log information or create some sort of feedback loop that takes down your server.

Spring Webservice Logging - Remove Binary content

Is there a way to exclude the binary content out of the Spring's webservice logging.
We have a system which receives large attachments[MTOM] with webservice response.
Enabling logging level to trace
log4j.logger.org.springframework.ws.client.MessageTracing=TRACE
dumps all the binary file data in the output log. This also slows down while batch processing.
Changing the log level to DEBUG only logs the root webservice response object name and is not useful for debugging.
Looking at the spring code where the response is logged WebserviceTemplate.logResponse, the method is private and cannot be reached/overridden.
Is there a way to add like a custom logger or tweak the way spring logs or any other possible way to remove the binary content from webservice responses in logs?
P.S. I also tried re-generating the webservice client classes with XJC2Task
with the -XToString option, just hoping that if setting the MessageTracing=DEBUG would log the full content of the webservice response root object [by using toString]. But that didnt happen and Spring would only log the object name.

What is the difference between Java Logger and System.out.println

I looked up the api about the logger class(here) and I was looking at the Logger.info method. I was confused when I saw its perimeter as a message displayed as a string public void info(String msg) which is same as System.out.println(). I am wondering what is the different between these two, and why do we use Logger instead of System.out.println when they can print out the same thing.
In Logger.
Logger.info("Hello")
Output:
[INFO ] 2015-08-07 11:18:46.140 [main] ClassName Hello
In System.out.println
`System.out.println("Hello")
Output:
Hello
Usually, because a Logger can be configured to write to a file (and the console). It might also be configured at higher (or lower) granularity as to messaging. For example, you might configure (at runtime) for level of warn. In which case, that logger would not display debug or info messages. It can include information such as the class that is writing, a line number, and a date and time (of the message).
Using a logger allows you to abstract out a lot of details and do a lot more than you could writing to stdout.
You can specify different destinations to write to. Different appenders write to a file, roll the file for given time periods, write to a queue or database, etc.
You can specify a consistent format for log messages instead of having to add it to every line you write to stdout.
You can choose an appender that buffers the output so that multiple threads can log without having the threads contend for the lock on the console object.
You can do a lot with filtering by category (typically package and classname) and log level (trace, debug, info, error, fatal), to make it easy to configure what log messages you want to see and which you want to ignore. With logging you can change the configuration in the logger properties or include a page in your application to change what gets filtered on the fly.
You can mix and match this stuff, for instance, setting up a specific smtp appender to email log messages for logging level of error or higher, in addition to writing the messages to a rolling file or whatever.
The main difference between a Logger and System.out.println is Logger: Prints the text in a file(text file)System.out.println: Prints the output in console
Logger is useful when you are going for any LIVE projects. Because if any project is developed and deployed, then you cannot check the console. At that time Logger will be useful to track the flow of your project also you can find the Error or Exception if you have given the logger in catch{...} block.
Also go through this Logger vs. System.out.println
But when we use any logging mechanism(log4j, slf4j, logback etc) we
configure appenders and corresponding target log files for each
package. By default console appender is turned off unless you
explicitly configure it to log to a destination.
The System.out.println always logs the message to a console appender.
So, it should be used only when we are sure that the console appender
is configured in the logger configuration file. Otherwise we end up
having logs logged on server console which is incorrect. Any log from
inside the application should go to the corresponding application log
and not the server log.
Let me explain with an example.
If we are building an application called Tracker using a logging mechanism to run in a tomcat container, and we configured appenders for application logging with destination log file as tracker-application.log and we did not configure the console appender.
Then if the System.out.println is encountered by the JVM then the log will go to server log of the tomcat server which is wrong because server log should only have information about the server and not the application.
But if we created a console appender with a target log file then it will be logged properly.
Hope I was clear. ;)
For best practices refer Do not use System.out.println in server side code or http://www.vipan.com/htdocs/log4jhelp.html
For differences between both of them please refer
Logger vs. System.out.println

Listening for Log Messages in slf4j/logback

I have a Swing-based application without a console that logs all messages to text files through slf4j with logback underneath.
Whenever a message is logged, I'd also like to add it to a JTextArea for the end user to see on the screen.
These messages should be formatted the sameway as the log files for consistency.
Does slf4j/logback have API that lets you add a listener for log messages so that I can copy them to JTextArea?
Logback Appenders are listeners for log messages. Just implement a custom appender that will log
in a JTextArea.
You can then create, configure and add appenders to loggers programmatically, or using an external config file (logback.xml)

How to redirect java.util.logging to a file?

I have a java program using an external library. The main program uses log4j to log its messages and the library uses java.util.logging.
My problem is that log messages from the external library and the main program are mixed in the console.
I would like to redirect all log messages from the external library to a file. I tried to do that with a logging.properties file:
handlers= java.util.logging.FileHandler
.level= INFO
java.util.logging.FileHandler.pattern = foo.log
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
This file is initialized with:
System.setProperty("java.util.logging.config.file", "logging.properties");
Unfortunately, log messages from the external library keep appearing in the console.
Should I use something like slf4j to intercept log messages from java.util.logging?
Thank you for your time.
Here's some code from one of my programs. This also does automatic rotation. The config class is my own that's read from a properties files. You can just replace that with your own values.
Logger rootLogger = Logger.getLogger("");
logHandler = new FileHandler(config.getLogFile(),
config.getLogRotateSize()*1024*1024,
config.getLogRotateCount(), false);
logHandler.setFormatter(new SimpleFormatter());
logHandler.setLevel(Level.INFO);
rootLogger.removeHandler(rootLogger.getHandlers()[0]);
rootLogger.setLevel(Level.INFO);
rootLogger.addHandler(logHandler);
Note this is for a stand-alone program. Any application server has it's own logging configuration tools. The program can also change the formatter and levels on the fly if a dynamic debug mode is desired.

Categories

Resources