How direct all internal Springboot logging to log4j? - spring

I would like to have all the internal Springboot log messages directed to log4j so that the messages follow my log4j properties (i.e. they go to the console and my log4j file using my layout).
I attempted to configure Springboot to use only log4j logging per this guide: http://spring.io/blog/2009/12/04/logging-dependencies-in-spring/
I have successfully setup log4j to log to the console and to file when using a log4j logger object.
However, it appears that Springboot is still using slf4j when it logs internally. I tried removing the slf4j dependencies, but Springboot fails to startup with a missing class exception.
How can I direct all the internal Springboot logging to log4j?

Have you tried this from the official docs?
http://docs.spring.io/spring-boot/docs/1.2.2.RELEASE/reference/htmlsingle/#howto-configure-log4j-for-logging
Basically you have to exclude the logback stuff that comes by default with the starter poms.
Quoted here for reference:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>

If you are using spring-boot, I recommend following the spring documentation, which I found easier than your reference.
I have log4j working, and it prints messages like:
[2015-03-06 15:34:54 INFO ] [main] [tomcat.TomcatEmbeddedServletContainer] Tomcat initialized with port(s): 8080 (http)
Not sure if that's what you mean by internal messages.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>1.1.4.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- add slf4j interfaces to classpath this is for log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
<scope>runtime</scope>
</dependency>
<!-- add log4j to classpath -->
<!-- does the logging -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
Add log4j.properties under resources
log4j.rootLogger=INFO, Console, File
# Direct log messages to stdout
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %m%n
log4j.appender.File=org.apache.log4j.RollingFileAppender
log4j.appender.File.append=true
log4j.appender.File.file=/tmp/projectdir/logfile-ws.log
log4j.appender.File.threshold=INFO
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %m%n
log4j.appender.File.MaxFileSize=100MB
log4j.appender.File.MaxBackupIndex=1

Related

How to send entire logs to jeager span in Quarkus?

Is there any way to put the std logs provided by the application and the errors to a span?
I wanted to put slf4j logs provided in quarkus and it should show up in jeager-Ui
Quarkus log information
application.properties
quarkus.http.port=8200
quarkus.swagger-ui.always-include=true
quarkus.jaeger.service-name=myservice
quarkus.jaeger.reporter-log-spans=true
quarkus.jaeger.log-trace-context=true
quarkus.jaeger.propagation=jaeger
quarkus.jaeger.sampler-type=const
quarkus.jaeger.sampler-param=1
quarkus.log.console.format=%d{HH:mm:ss} %-5p traceId=%X{traceId}, spanId=%X{spanId}, sampled=%X{sampled} [%c{2.}] (%t) %s%e%n
quarkus.log.handler.gelf.enabled=true
quarkus.log.handler.gelf.host=localhost
quarkus.log.handler.gelf.port=12201
Below are dependencies which I am using to achieve centralized log management(ELK).For that I am using guides provided by quarkus quarkus-centralized-log-managemt guide
pom.xml
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-opentracing</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-openapi</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-logging-gelf</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-metrics</artifactId>
</dependency>
</dependencies>
You can #Inject Tracer and add your own stuff in it.
But I thought that errors would be automatically logged in Jaeger, but I could be wrong.
After configuring your logs to accept Jaeger attributes like for ex :
quarkus.log.console.format=%d{HH:mm:ss} %-5p [%c{2.}] (%t)
[traceId=%X{traceId},spanId=%X{spanId},%X{sampled}] %s%e%n
Then you need to propagate Jaeger context to your logs through the below properties :
quarkus.jaeger.log-trace-context=true
quarkus.jaeger.propagation=jaeger
I had added a new gelf configuration property in my application.properties.By this we can send logs with traceId and spanId to ELK stack.
application.properties
quarkus.http.port=8200
quarkus.swagger-ui.always-include=true
quarkus.jaeger.service-name=myservice
quarkus.jaeger.reporter-log-spans=true //This can be excluded
quarkus.jaeger.propagation=jaeger //This can be excluded
quarkus.jaeger.sampler-type=const
quarkus.jaeger.sampler-param=1
quarkus.log.console.format=%d{HH:mm:ss} %-5p traceId=%X{traceId}, spanId=%X{spanId}, sampled=%X{sampled} [%c{2.}] (%t) %s%e%n
quarkus.log.handler.gelf.enabled=true
quarkus.log.handler.gelf.host=localhost
quarkus.log.handler.gelf.port=12201
quarkus.log.handler.gelf.include-full-mdc=true //newly added property

How can I get AWS Lambda and Spring Boot 2.1.5 to use Log4j2.xml and log to Cloudwatch?

In order to log to cloudwatch in a lambda, one must use this log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration packages="com.amazonaws.services.lambda.runtime.log4j2">
<Appenders>
<Lambda name="Lambda">
<PatternLayout>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %X{AWSRequestId} %-5p %c{1}:%L - %m%n</pattern>
</PatternLayout>
</Lambda>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Lambda" />
</Root>
</Loggers>
</Configuration>
Also the following items must be in the pom.xml:
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.1.5.RELEASE</version>
</parent>
...
<dependency>
<artifactId>aws-lambda-java-log4j2</artifactId>
<groupId>com.amazonaws</groupId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.2</version>
</dependency>
Of course none of this works for me. It seems Spring uses Logback and I have tried shutting that off using these instructions. Nothing seems to work.
Any tips?
Got it! I read a post/rant where a guy noticed that AWS used a "5 year old jar" for the Cloudwatch/Lambda logging (can't find it now). So I found a Log4j2 jar that is compatible with 1.2 (log4j-1.2-api) and it worked. I had to shut off Logback in Spring and activate Log4j2. Below should take you all the way.
<dependency>
<artifactId>spring-boot-starter</artifactId>
<groupId>org.springframework.boot</groupId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<artifactId>aws-lambda-java-log4j2</artifactId>
<groupId>com.amazonaws</groupId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>2.11.2</version>
</dependency>
...
PS. You might need to fix this as well: log4j2 ERROR StatusLogger Unrecognized conversion specifier

Externalize logback.xml in Spring MVC

I use SL4J for logging in my Java Spring MVC Web Application. I have the logback.xml file under /src/main/resources. In my pom.xml file, I have the following related to logging:
<properties>
<org.slf4j-version>1.6.4</org.slf4j-version>
<logback.version>1.0.1</logback.version>
</properties>
And the following dependencies:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<!-- logback -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
</dependencies>
Now logging is working fine for me. But now I am trying to move the logback.xml file outside the war file so that I do not have to change the war file to change the logging level. I was trying to move the file to ${catalina.base}/conf/. I was unable to find a way to specify the path of the file which would be ${catalina.base}/conf/logback.xml. I have found a couple of similar questions, but I couldint find a solution that I could use:
Externalized the location of logback.xml in Spring Boot using application.properties and
logback externalization. Is there any way I can move the file under the conf folder under tomcat and specify its path so that I can keep this logback.xml ouside the war file
It is not good idea to put logging properties in tomcat config folder. Better to put it inside application folder/war.
But there is a way to locate logback.xml from external directory by setting a property in your spring application properties file.
For example in application.properties add
logging.config=file:/home/user/tomcat/conf/logback.xml

How to output logger name in JGroups and Spring Boot?

I'm using Spring Boot 1.3.2, with Logback, and using EhCache as the Hibernate second-level cache with JGroups 3.6.8.Final for distribution. All of the log messages for org.jgroups loggers are coming out as unknown.jul.logger. This originates in the SLF4JBridgeHandler that Spring Boot installs to bridge java.util.logging loggers over to SLF4J.
Is there something I can do to have the logger name propagated over to the SLF4J logger? When I was using JGroups 3.1.0.Final it was working fine, but after the upgrade all I get is the unknown logger.
Since jgroups 3.6.x I switched to the following configuration (slf4j + log4j2).
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.6</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.5</version>
</dependency>

Spring always print logs at DEBUG level when running Maven

I am using slf4j, logback for logging in my Spring application. I have configured to exclude common-logging and add the jcl-over-slf4j bridge.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.3</version>
</dependency>
My logback.xml looks like the following:
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
<logger name="com.my" level="DEBUG" />
The problem is when running maven builds, classes from spring always prints in DEBUG level which pollutes the console with verbose information. This doesn't happen when running the app with Tomcat though.
Also, if I extend a class of Spring, e.g. HttpSessionRequestCache, logging statements from the Spring classes are executed, as if it get the level from the child class! This does happen when running with Tomcat.
How to make Maven honor the logback.xml configuration?

Resources