Get spring framework logging in wildfly - spring

I was trying to get Spring 4 to log in Wildfly 8.2. I wanted spring to use the wildfly logging configuration.
All the examples I could find were trying to complicate this process by adding additional logging frameworks and configuration.
So here is how I did it, for prosperity.

To get spring 4 to log in wildfly 8, I had to add the following to the logging subsystem config in the standalone.xml file.
<add-logging-api-dependencies value="false"/>
<use-deployment-logging-config value="false"/>
Additionally to get debug logging
<logger category="org.springframework">
<level name="DEBUG"/>
</logger>
Here is the full subsystem config:
<subsystem xmlns="urn:jboss:domain:logging:2.0">
<add-logging-api-dependencies value="false"/>
<use-deployment-logging-config value="false"/>
<console-handler name="CONSOLE">
<level name="DEBUG"/>
<formatter>
<named-formatter name="COLOR-PATTERN"/>
</formatter>
</console-handler>
<periodic-rotating-file-handler name="FILE" autoflush="true">
<formatter>
<named-formatter name="PATTERN"/>
</formatter>
<file relative-to="jboss.server.log.dir" path="server.log"/>
<suffix value=".yyyy-MM-dd"/>
<append value="true"/>
</periodic-rotating-file-handler>
<logger category="com.arjuna">
<level name="WARN"/>
</logger>
<logger category="org.apache.tomcat.util.modeler">
<level name="WARN"/>
</logger>
<logger category="org.springframework">
<level name="DEBUG"/>
</logger>
<logger category="org.jboss.as.config">
<level name="DEBUG"/>
</logger>
<logger category="sun.rmi">
<level name="WARN"/>
</logger>
<logger category="jacorb">
<level name="WARN"/>
</logger>
<logger category="jacorb.config">
<level name="ERROR"/>
</logger>
<root-logger>
<level name="INFO"/>
<handlers>
<handler name="CONSOLE"/>
<handler name="FILE"/>
</handlers>
</root-logger>
<formatter name="PATTERN">
<pattern-formatter pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
</formatter>
<formatter name="COLOR-PATTERN">
<pattern-formatter pattern="%K{level}%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
</formatter>
</subsystem>

The answer depends on what causes the deployment to break in your deployment.
If you have any of the following logging classes in your classpath that can cause logging to break: logging.properties, jboss-logging.properties, log4j.properties, log4j.xml, jboss-log4j.xml
So for example just run this code (either in your app or in a debugger) and it'll show you if you have the file in the classpath
getClass().getResource("/logging.properties")
Repeat for each of the log files specifed above, if any of those return non-null then you have found your culprit.
At that stage you can either remove the problem logging file, or alternatively use Rian's suggestion of adding <use-deployment-logging-config value="false"/> (you don't need to use add-logging-api-dependencies in that scenario thou).
Another potential issue if you have multiple logging jar files. Keep in mind that wildfly will automatically provide several of those unless you use <add-logging-api-dependencies value="false"/>

I had this similar problem and I got it working thanks to the first answer posted by Rian. Here is my working system:
Server: jBoss EAP 7.1
Framework: Spring 5
Configuration via: yml
Modules: each module has its own independent war deployed
Requirement: Each module must log in a separated file
In the yml file I added the following lines to set the log output to a different file than the standar server.log:
logging:
file: ${SPF_LOGGING_PATH:app-logs/}log-#project.artifactId#-app.log
pattern:
console: "%d %-5level %logger : %msg%n"
file: "%d %-5level [%thread] %logger : %msg%n"
Using this configuration, a file in the output dir is created but no text is been written. So, adding this lines to the logging subsystem section in standalone.xml file, it started to write text from each module:
<add-logging-api-dependencies value="false"/>
<use-deployment-logging-config value="false"/>
Hope this may help anyone facing the same issue.
Cheers!

Related

JAVA_TOOL_OPTIONS variables in Logback configuration

I have a small AWS Lambda function written in Java with a Logback attached to it for sending out error log emails. The variables needed for the Logback appender (basic SMTP details) are loaded into the Logback.xml configuration appender SMTPAppender in the usual format of "${smtpPort}" etc. The variables are maintained through the Lambda Function Environment variable: key=JAVA_TOOL_OPTIONS, value=-DsmtpPort=587 -DsmtpHost=xyz.
Here is the logback.xml configuration:
<configuration debug="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{ISO8601} %-5p [%t] %c - %m%n</pattern>
</encoder>
</appender>
<appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
<smtpPort>${smtpPort}</smtpPort>
<STARTTLS>${startTls}</STARTTLS>
<smtpHost>${smtpHost}</smtpHost>
<username>${smtpUser}</username>
<password>${smtpPassword}</password>
<to>${logEmailTo}</to>
<from>xxxx#xxxxxxxx.net</from>
<subject>Error log [Scheduled Task Runner]</subject>
<layout class="ch.qos.logback.classic.html.HTMLLayout">
<pattern>%date%thread%level%logger%msg</pattern>
<cssBuilder class="LambdaHandlers.util.ErrorEmailCssBuilder"/>
</layout>
<cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTracker">
<!-- send just 10 log entries per email -->
<bufferSize>10</bufferSize>
</cyclicBufferTracker>
</appender>
<logger name="org.apache.commons.httpclient" level="OFF"/>
<logger name="httpclient.wire.header" level="OFF"/>
<logger name="httpclient.wire.content" level="OFF"/>
<logger name="httpclient.wire" level="OFF"/>
<logger name="org.apache.http" level="OFF"/>
<logger name="io.micrometer" level="INFO"/>
<logger name="software.amazon.awssdk" level="OFF"/>
<logger name="io.netty" level="OFF"/>
<logger name="org.apache.catalina.realm" level="DEBUG"/>
<logger name="org.apache.axis" level="INFO"/>
<root level="debug">
<appender-ref ref="STDOUT"/>
<appender-ref ref="EMAIL"/>
</root>
</configuration>
When the values are hardcoded in the logback.xml the email sending works as expected, however; after moving these to the JAVA_TOOL_OPTIONS bucket, the email stop working. The Lambda environment variables are correctly loaded on Lambda startup and can be accessed through the System.getProperty() in the Lambda context, providing the values from the environment variable.

Logs in Spring Boot not showing in terminal

I have a project in Spring Boot where I am adding logs with the #Slf4j annotation, but I can't get them to show up in the terminal when starting the project.
This is the log: log.info("totalVacations: " + totalVacations);
When I run the command mvn spring-boot: run, in terminal I can't see the message.
Very likely you are missing the configuration of the appender, if you are also using LOG4J and you don't already have it, you can create a file named: log4j2.xml within the application's class path, and then create some basic configuration like:
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />
</Console>
<RollingFile name="RollingFile"
fileName="./logs/spring-boot-logger-log4j2.log"
filePattern="./logs/$${date:yyyy-MM}/spring-boot-logger-log4j2-%d{-dd-MMMM-yyyy}-%i.log.gz">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
<Policies>
<!-- rollover on startup, daily and when the file reaches
10 MegaBytes -->
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy
size="10 MB" />
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<!-- LOG everything at INFO level -->
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
</Root>
<!-- LOG "com.baeldung*" at TRACE level -->
<Logger name="com.baeldung" level="trace"></Logger>
</Loggers>
</Configuration>
The key here is to set an appender to the console so that the logs are pushed there, and then at the logger section establish which level are you trying to get at the console appender, according to your example that should be INFO
If you are using just plain SLF4J with Logback go to src/main/resources/application.properties and add the following line:
logging.level.com=INFO
The ".com" is referencing the package to where this will apply, in this case is everything inside src/main/java/com.
and at the logback-spring.xml try this basic config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="your.name" level="INFO" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</logger>
</configuration>
You can find a very nice tutorial with more info here:
https://www.baeldung.com/spring-boot-logging
https://springframework.guru/using-logback-spring-boot/
Check whether #Slf4J import is from Lombok or some other library. I'm sure lombok #Slf4J default provides lowercase log and not LOG.

Removing Log4j2.xml from Tranisitive Dependecies and to use Log4j2-spring.xml instead in springboot project

I need to add file Appender in existing log4j2.xml of my existing spring-boot project. i did that and its generating two logs files for every logger specified i.e, i have only added Root level logger then also two files are getting generated.
When i searched for it i found that in spring projects log4j2-spring.xml should be used instead of Log4j2.xml which may be the cause of duplicates log files getting generated.
But when i am trying to run to change my log4j2.xml to log4j2-spring.xml its not even considering that file and is picking any other log4j2.xml that is available via classpath as i have added some external jar as dependency in this project the log4j2.xml of these exteranl dependemcies are being considered rather then my log4j2-spring.xml. Is there a way of telling it to use my log4j2-spring.xml instead of any log4j2.xml or may be is there a way to remove the usage of log4j2.xml from any dependency.
I tried doing the same with a simple spring-boot project and it worked as expected but when i am trying to do the same in my project which is multi-module maven project it does not works.
Can someone let me know what would be the way of doing above said asks.
Here is my pom.
`
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info" monitorInterval="60">
<Properties>
<Property name="log-path">${sys:app.log.path}</Property>
<Property name="archive">${log-path}/archive</Property>
<Property name="appName">${sys:spring.application.name}</Property>
<Property name="envName">${sys:app.env.name}</Property>
</Properties>
<Appenders>
<Console name="Console-Appender" target="SYSTEM_OUT">
<PatternLayout>
<pattern>
%d{yyyy-MM-dd HH:mm:ss.SSS}{GMT} [%-5level] [%t] %c{1} - %msg%n
</pattern>
<charset>US-ASCII</charset>
</PatternLayout>
</Console>
<File name="File" fileName="${log-path}/${appName}-${envName}--${date:yyyy-MM-dd-HH-mm-ss-SSS}.log" append="true">
<PatternLayout pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<Loggers>
<Root level="INFO" additivity="false">
<AppenderRef ref="Console-Appender" level="WARN" />
<AppenderRef ref="File" level="INFO" />
</Root>
<Logger name="com.tmo.eus" level="INFO" additivity="false">
<Appender-ref ref="Console-Appender" />
</Logger>
</Loggers>
</Configuration>
`

MongoDbUtils debug logging

How can I disable the following logging from org.springframework.data.mongodb.core.MongoDbUtils:
DEBUG o.s.data.mongodb.core.MongoDbUtils - Getting Mongo Database name=[erepprod]
I have tried the following in my log4j.properties file:
log4j.category.org.springframework.data.mongodb.core.MongoDbUtils=ERROR,console
log4j.logger.org.springframework.data.mongodb.core.MongoDbUtils=ERROR,console
But the message is still being logged.
Any help would be appreciated as I have spent two days trying to get rid of this message.
Probably a bit late, but for anyone else having the problem,
I know from using Spring Boot that they seem to prefer now SL4J + Logback
So by adding a logback.xml that looked like below.. solved it:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d %5p | %t | %-55logger{55} | %m %n</pattern>
</encoder>
</appender>
<logger name="org.springframework.data.mongodb">
<level value="INFO" />
</logger>
<root>
<level value="INFO" />
<appender-ref ref="CONSOLE" />
</root>
</configuration>

hibernate logback sql

I want to see the actual parameters of my SQL queries when I use Hibernate. I add this to my logback.xml to see the queries (with question marks):
<logger name="org.hibernate.type" level="TRACE" />
but to no effect.
Is there any special configuration necessary?
OnConsoleStatusListener shows me the correct configuration
23:48:15,246 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.hibernate.type] to TRACE
but no output from org.hibernate.type package.
I'm using Spring with Jpa.
Things you have to make sure:
Are you sure that SLF4J + LogBack is working in your app?
Is your logger pointing to any appender?
Have you configured an appended?
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<!-- "application-name" is a variable -->
<File>c:/logs/${application-name}.log</File>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d %p %t %c - %m%n</Pattern>
</layout>
</appender>
<root level="debug">
<appender-ref ref="FILE"/>
</root>
</configuration>
I'm using this configuration, and it works for me:
<logger name="org.hibernate.type" level="trace" additivity="false">
<appender-ref ref="consoleAppender" />
</logger>
The logger that works for me is the following:
<logger name="org.hibernate.type" level="TRACE" />

Resources