Spring Boot Audit Logging by Example - spring-boot

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?

Related

How to know what log levels used in production spring boot application?

I am wondering if there is a way to know what are the logging levels set in spring boot application, which is running on production. I know I can check some logging level statements in application.yml. But I am not sure if it is edited after starting the application.
So, I want to confirm what logging levels are used in the application without stopping or restarting.
Can you provide inputs on this?
If actuators are enabled on your project you can call the /actuator/loggers endpoint:
$curl 'http://localhost:8080/actuator/loggers' -i -X GET
For more reference

Can we log per spring Integration flow?

Spring integration really helps us a lot during application integration, it make us more focus on flow design.
However we have an requirement to have logs per integration flow, any suggestions how to achieve this? Currently we use <int:message-history/> for logging.
in our case, each integration flow is one spring-context.xml, and we load one spring-context.xml with a separated ApplicationContext, actually we want each ApplicationContext has its own log file, this will greatly help us for issue tracking
As we have many integration flows, so currently all logs are in one log file, so it is not easy for us to find the log we want, and it is difficult for us to trace the message history.
Thanks.

SpringBoot-- I can't get message in the console logged in ApplicationListener that interested in ApplicationStartedEvent

I can't get message in the console logged in ApplicationListener that interested in ApplicationStartedEvent. I have find the reason. But I don't understand why spring does it like that. I find there is a LoggingApplicationListener in which called this.loggingSystem.beforeInitialize().In beforeInitialize() a turboFilter has been added into logback,the turboFilter return a deny in his decide().
I know a little english , please help me
Spring Boot fires some events very early, before the ApplicationContext is even created or the LoggingSystem is initialized. The reason for the latter is that you can configure logging using keys from application.properties.
Spring Boot needs to read the Environment first and then use those keys to let the logging system configures itself. During that period, no logging is enabled so if you use it in your own listener, nothing will happen.
We gave a university session last November on those topics and there is a section on application events.
Careful with ApplicationStartedEvent. The name is misleading, it's actually the very first event we fire (we should have named this ApplicationStartingEvent and we've done that recently). If you want to check that the application has (fully) started, you may want to use ApplicationReadyEvent. The logging system is initialized at that point so you can safely use loggers.

Can someone explain the flow of execution of spring boot application?

I am working on a spring boot application.
I wanted to know what happens when the application started running and before it becomes ready for user interaction.
I tried going through the console logs but I am still unsure as to what happens when.
I believe you should elaborate a bit more your question. That's because you can build different types of applications using Spring Boot. In a nutshell, during the start up the application will basically try to load the "beans" defined in the related context(s), pre-configured components, define the active profile, properties files, etc. Also some Spring and application events are generated during the start up.
A good way to understand what's going on behind the scenes is running the application in DEBUG mode. By default, the log level of the application is set as INFO.
Have a look at this link for further details:
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-spring-application
I hope this can help you as start point.

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

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

Resources