Spring Webservice Logging - Remove Binary content - java

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.

Related

Is it possible to create custom fields in a Kibana dashboard?

I am using a Java micro-service architecture in my application and generating separate log files for each micro-service.
I am using ELK stack approach to visualize the logs in Kibana, but the problem is whatever the fields that I'm getting from Elastic Search that are related to server logs fields. some example fields are #timestamp,#version,#path,#version.keyword,#host.
i want to customize this fields by adding some fields like customerId,txn-Id,mobile no so that we can analyze the data easily.
I'm using org.apache.logging.log4j2 to write the logs. Can I set above fields (customerId,txn-Id,mobile) to log files? And then Elastic will store these fields with the above default fields and then these custom fields should available in a Kibana dashboard. Is this possible?
It's definitely possible to do that. I've not done it with the log4j2 stack (I have with slf4j/logback), but the basic approach is:
set those fields in the Mapped Diagnostic Context (I'm fairly sure log4j2 supports that)
use a log appender which logs to logstash-structured JSON
configure filebeat to ship the JSON logs
if filebeat is shipping to logstash, you'll need to configure logstash to pass those preformatted JSON logs directly to elasticsearch
It is definitely possible. I am doing that now with my applications. However, the output looks a bit different from yours. The basic guide for doing this can be found at Logging in the Cloud on the Log4j2 web site.
The "normal" log view looks very similar to what you would see when logging to a file.
However, if you select a message you can see the individual fieds.
The Log4j2 configuration uses a TCP Socket appender that is configured to write to a cluster of Logstash servers that use a single DNS entry and to use the Gelf layout.
You can also use MapMessages to capture individual data elements and log them. While this currently works it is slightly cumbersome so I have recently committed improvements that will be available in Log4j 2.15.0.
It is important to note that the Logging in the Cloud page briefly mentions storing your logging configuration in Spring Cloud Config. If you want to have a common base configuration while allowing apps to do some customization this works very, very well. However, The Gelf, Json Template Layout and TCP Appender are all independent from that and can be used without Spring Boot.

Log fields/variables to file in web application

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);

How to intercept all exceptions in a Spring MVC web application and how to store a lot of informations?

In a web application (Spring 3.1.2 MVC), I need to intercept all exceptions to store informations in a database. It's like a logger (it's already configure in the log4j.xml file) but the idea it's to keep informations in a specific database table.
So how can I handle all exception by aspect? by filter? by interceptor? Not by #ControllerAdvice (since Spring 3.2) because the project is build with Spring 3.1.
And in a second time, when I catch the exception, how can I handle that, how can I retrieve a lot of informations: request url, referer url, class, line, objects states (class + values)?
Some frameworks exists for that (intercept and store exceptions)?
I'm a big fan of Logback which provides an appender for the database out-out-of-the-box. The DBAppender basically writes all logging information to the database, the stack trace, the message and the MDC (when set).
To add additional attributes to your logging you can register the [MDCInsertingServletFilter] which will add things like URL, remote ip, username (if available) to the MDC (and as such will be logged).
For the other information you can specify a pattern to include the desired information.
To log all exceptions you can create an after throwing advice and have that applied to your classes. One tricky thing is to make sure your exceptions are being logged only once instead of over and over.

Is it possible to get the root log level of Jboss Server, if custom loggers are used instead of logging service provided by the Server?

Following are the requirements,
multiple modules deployed on JBoss AS 7 with individual logging configuration using logback.xml.
all of them request exclusion of default logging-service provided by the Server, using jboss-deployment-structure.xml.
Following are observation,
log.debug statements get printed as INFO on Server log(server.log)
it's because root level of custom-logger(logback.xml) is set to DEBUG
Now questions,
How can I make, DEBUG statements generated by custom logging statement gets printed with appropriate log level?
Conversely, is it possible to get log level of Server without using it's logging service?
In other words, is it possible to achieve uniform log-level configuration across multiple modules that use custom-loggers?

Getting XML from Spring Web Service Request

We need to log all incoming SOAP requests, preferably by persisting to a DB as we have identifying properties that we'd like to associate with it. Is there any way of getting the raw XML data in Spring?
I suggest you take a look at the source for SoapEnvelopeLoggingInterceptor and/or PayloadLoggingInterceptor. You can probably modify this to include what you want.
Another solution could be to put a servlet Filter in front of everything that puts the identifying properties into the MDC (assuming you are using SLF4J and/or Log4j/Logback) that way you could configure a jdbc backed Appender which logs to the database.

Categories

Resources