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.
Related
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.
Generally, for internationalisation, we use .properties files to get the en/es/fr data.
Because for my specific use case, the content of the dictionary is not static, but dynamically changed at runtime, I am wondering if there is an API in Spring Boot, to implement this requirement.
I had the same problem and because i want dynamic i18n message. I decided to store my message in database and via Spring localeResolver bean get local and request database to get related message. So I can add message to database in runtime , in multiple language.
You can refer to this :https://vkuzel.com/spring-boot-internationalisation-with-database-stored-messages-and-ibm-icu
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.
We use Java DSL to configure our routes. All configurations for routes are in a db table and can be configured via a GUI.
How is it possible to ensure that the camelContext starts up even if a route is misconfigured (e.g. .to(invalidurl or typo) in a route or simply a bug in a route)?
Is there a possibilty to validate the routes before starting or maybe better some parameters/options which can be set on the context itself?
You can configure the routes with .autoStartup(false), and then start the routes manually when CamelContext has been started up.
To validate its really depending on what kind of component it is. If its some database component you can write some code that does a SQL query to see if the is valid user login or something.
To validate that an endpoint uri is misconfigured, then that is harder as they have a ton of options. But this is getting improved from Camel 2.16 onwards where we have during build time some tooling that generates a json schema file with the options, then we could potentially leverage that during parsing the routes to check for invalid configuration before attempting to create the endpoints which could detect errors sooner, and even also with IDE plugins or other 3rd party tooling.
Can you just before adding every route to the context, add it to a separate "test" context individually, and see if it spins up or fails; then based on that add it to your real context?
I've got a Spring Web MVC application (and also a BlazeDS application, though not as relevant) where files are dynamically generated based on certain client actions.
I'd like to just map a certain directory on the file system to Spring MVC (or the app server) url and let it serve the files in that directory (with streaming and standard last-modified header support). Ideally, the mapped directory would be configured via the spring config, since I already have support per-machine for setting that up.
So, how can I do this? The best I can find so far is to write a controller that reads the file manually and streams it byte-by-byte. However, that seems far less than ideal. Is support for something like this already baked into Spring MVC or the standard application server spec?
Thanks!
If your processing model supports it, why not cut the middleman of the filesystem out of the picture completely and just stream the files back through the response stream as they are generated? Take a look at the AbstractExcelView and AbstractPDFView classes of Spring MVC to see some examples of how this is done.
or the standard application server spec?
Yes, there is. As you didn't mention which one you're using, I'll give a Tomcat-targeted answer. All you basically need to do is to add a Context element for /path/to/your/resources in /conf/server.xml:
<Context docBase="/path/to/your/resources" path="/resources" />
This way they'll be accessible through http://example.com/resources/...
Ideal for this is using an lightweight proxying server in front of your appserver, like a nginx or lighthttpd. You can configure it for serving static content, without calling your app.
If directory and files so dynamic, you can prepare real path to file at your controller and give this filepath to the frontend server, using headers. For example for nginx it's a X-Accel-Redirect header. Read more about this (and follow links for other http servers) there