Springboot structured logging using : Slf4j fluent API + logstash-logback-encoder - spring

I am trying to use fluent api implementation: https://www.slf4j.org/manual.html#fluent in a spring boot project with logstash-logback-encoder.
implementation ('org.springframework.boot:spring-boot-starter-web')
implementation('net.logstash.logback:logstash-logback-encoder:7.2')
I tried to add slf4j like this.
implementation('org.slf4j:slf4j-api:2.0.3')
Getting the following error:
SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See https://www.slf4j.org/codes.html#noProviders for further details.
SLF4J: Class path contains SLF4J bindings targeting slf4j-api versions 1.7.x or earlier.
SLF4J: Ignoring binding found at [jar:file:/home/prchowdh/.gradle/caches/modules-2/files-2.1/ch.qos.logback/logback-classic/1.2.7/3e89a85545181f1a3a9efc9516ca92658502505b/logback-classic-1.2.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See https://www.slf4j.org/codes.html#ignoredBindings for an explanation.
I understand, Spring has an internal dependency on logback and logback has a dependency on slf4j.
Any guidance will be helpful.

Related

How to exclude SLF4J binding from gradle-api-4.10.jar?

I use logback as dependency in my project and get the following output:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:~/.gradle/caches/4.10/generated-gradle-jars/gradle-api-4.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:~/.gradle/caches/modules-2/files-2.1/ch.qos.logback/logback-classic/1.2.3/7c4f3c474fb2c041d8028740440937705ebb473a/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.gradle.internal.logging.slf4j.OutputEventListenerBackedLoggerContext]
I read a lot of similar questions and answers. Most of them advice to add corresponding exclude to extra dependency. But I can't imagine which of these are relevant to gradle-api-4.10.jar. Looking through dependency tree also yielded no result.

Spring and hibernate integration

I am doing spring and hibernate integration example getting the below exception. Please advise me how to solve it.
log4j:WARN No appenders could be found for logger (org.springframework.beans.factory.xml.XmlBeanDefinitionReader).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'd' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:509)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1041)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:273)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
at com.javatpoint.InsertTest.main(InsertTest.java:14)
You will have to share some code snippets before someone can actually help you here!
Looking at the exception it seems you have created a Class property "d" and marked it as #Autowired.
Spring is trying to find a class of type d or name d registered in its context.
You register a class by using #Service or #Component etc annotation.
Update:(after the OP's comment).
Please ensure you are creating d bean using below code in application context.
<bean id="d" class="com.javatpoint.EmployeeDao">
<property name="template" ref="template"></property>
</bean>

setting spring boot logger to debug only for AOP related info

I use the following configuration
logging.level.org.springframework=DEBUG
to investigate in AOP related issues...is there any way to show in the log file only log entries related to AOP the same way as
logging.level.org.springframework.web=DEBUG
but for AOP?

How to remove NullPointerException from JSR 330 Spring application

I am implementing Spring+JSF application following the guidlines of http://www.mkyong.com/jsf2/jsf-2-0-spring-integration-example/ but I am using the latest Spring version (i.e. 3.x, it already contains the JSR 330 implementation, according to the documentation, so this should not be an issue) and my own classes, that is the difference from the mentioned example.
I am stuck with NullPointerException that indicates that the bean (that acts as JSF managed) bean has not received injection of the instance of Spring bean. All the beans use JSR 330 annotations at appropriate places (#Named, #Inject) and the interfaces of the beans and the variables (under #Inject annotation) follow the usual naming patterns...
So - how to debug this situation. E.g. is there way to see all the beans that sit into the Spring application context. E.g. so we can determine that the Spring context is not initialized appropriately. Maybe there are other methods how to see what is happening in app (debug injection)?
Spring supports JSR 330 annotations for dependency injection provided you have the relevant jars in your classpath.
You need to add the following jar to your application.
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
And you might get good info by enabling logging for org.springframework
Just add log4j jar and drop in this log4j.properties file in the root of the classpath.
Log4j.properties:
log4j.rootCategory=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %-5p %c %M - %m\n
log4j.category.org.springframework=DEBUG
See also:
Documentation
beans-standard-annotations
overview-logging-log4j

Log4j works for one class and doesn't work for the other class

I am developing a Spring MVC 3.0.4 webapp and Logger factroy doesn't work for the Controller classes other than HomeController.java. I initilize the Logger factory like:
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
private static final Logger logger = LoggerFactory.getLogger(RequestDataController.class);
and
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
is added for both classes
What configuration is missing?
It seems SLF4J is missconfigured. You use it for you own logging and that's a right choice! But Spring has JCL logging framework hardcoded. It's an abstarction layer that is typically configured with Log4J implementation. Please, visit this link where some JCL-to-SLF4J drop-in replacements described. If you want to leave both Log4J and SLF4j in your project than add proper logging preferences into log4j.properties or log4j.xml for core spring packages.

Resources