How to turn off Spring 3 debug logging? - spring

I would like to turn off log4j logging for Spring 3.1 while leaving things on debug for my own code.
I tried sticking this line into my log4j.properties:
log4j.category.org.springframework = WARN
To get this:
# Root Logger Setup: Includes the level of reporting and appenders -> where
# log messages get sent
log4j.rootLogger = DEBUG,ca,fa
log4j.category.org.springframework = WARN
#ca - Console Appender - Send messages to the console
log4j.appender.ca = org.apache.log4j.ConsoleAppender
log4j.appender.ca.layout = org.apache.log4j.PatternLayout
log4j.appender.ca.layout.ConversionPattern = [acme]: [%-5p] - %d{yyyy-MMM-dd HH:mm:ss} - %c{1}:%M(): %m%n
#fa - File Appender - Send messages to a log file
log4j.appender.fa = org.apache.log4j.RollingFileAppender
log4j.appender.fa.File = acme.log
log4j.appender.fa.MaxFileSize = 100KB
log4j.appender.fa.MaxBackupIndex = 10
log4j.appender.fa.Threshold = DEBUG
log4j.appender.fa.layout = org.apache.log4j.PatternLayout
log4j.appender.fa.layout.ConversionPattern = [%-5p] - %d{yyyy-MMM-dd HH:mm:ss} - %c{2}:%M(): %m%n
No luck in shutting off the debug output from Spring though.
Thanks in advance for any help
Steve

Are all your dependencies in place?
1.3.2.3 Using Log4J
Many people use Log4j as a logging framework for configuration and management purposes. It's efficient and well-established, and in fact it's what we use at runtime when we build and test Spring. Spring also provides some utilities for configuring and initializing Log4j, so it has an optional compile-time dependency on Log4j in some modules.
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). So for Maven users this is your dependency declaration:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.0.RELEASE</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<scope>runtime</scope>
</dependency>
</dependencies>
And here's a sample log4j.properties for logging to the console:
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{ABSOLUTE} %5p %t %c{2}:%L - %m%n
log4j.category.org.springframework.beans.factory=DEBUG

I figured this out.
In my classpath I have a directory C:\Classes for convenience when I am experimenting with things. I had another log4.properties file there, which was superseding the one packaged in my WAR making it look like the technique below wasn't working. I renamed the log4.properties in my C:\Classes and all is well.
Thanks to everyone who took a look and thanks to the people at Spring Source who made doing this necessary. It is good to know that an extensive level of debugging is easily available to me when I want it instead of just getting a black box.

Related

How can I expose camel mbean in spring boot?

I am migrating the application from 2.23.2 to 3.1.0 version.
The application works fine having 2 routes.
But a mbean is not exposed in jconsole.
There are basic mbeans in jconsole.
- JMImplementation
- com.sun.management
- com.zaxxer.hikari
- connector
- java.lang
- java.nio
- java.util.logging
- javax.management.remote.rmi
- org.apache.logging.log4j2
- org.springframework.boot
How can I handle this issue?
You need to add a dependency to your project for camel-management. For example:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-management</artifactId>
<version>3.1.0</version>
</dependency>
There's a note about this in the Camel 3.x upgrade guide:
https://camel.apache.org/manual/latest/camel-3x-upgrade-guide.html#_spring_boot_jmx

Seeking a log4j2.properties appender, but not the rolling appender?

Question 1:
I have been looking everywhere for an example of a log4j2.properties file that uses an appender to write to a file - but not the rolling appender.
I already roll the files through syslog and I don't understand why I have to define an entire separate rolling strategy just to write to a single file.
Is there such a beast?
Question 2:
If I can't get an answer for Question 1, then I have been looking for a comprehensive listing of all the appenders possible in a log4j2.properties file. I see plenty of examples of all the different rolling appenders, but I was looking for appenders that are not specific either to rolling or console.
All I've been able to find so far are properties files with rolling appenders, or non-rolling XML files.
(summary)
Either the answer to Question 1 or Question 2 would be amazing. The bottom line is that I'm looking for something that doesn't involve rolling, that would be put in a .properties file (specifically for Elasticsearch).
Thank you!
Question 1: I have an very simple log file with just log everything to a file - not rolling file.
name=PropertiesConfig
property.filename = logs
appenders = console, file
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{dd} [%t] %c{1} - %msg%n
appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=${filename}/propertieslogs.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
loggers=file
logger.file.name=com.aavn.viking.feedback360
logger.file.level = info
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE
rootLogger.level = info
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
About the rolling file strategy I think it is used for delete the log file when it meet specific business like over 30 days, the accumulated file over 100 MB, or something else.
Question 2: There are some appenders that you can put on log4j2.properties file except console, rolling file and file such as: CassandraAppender,FailoverAppender, FlumeAppender, JDBCAppender, JMS Appender, HttpAppender etc. The link below is for more information about another appenders.
http://logging.apache.org/log4j/2.x/manual/appenders.html
P/s: If you want run my log4j2.properties, you must addlogging.config=src/main/resources/log4j2.properties to the application.properties
and add log4j2 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-log4j2</artifactId>
</dependency>

Spring boot embedded tomcat logs

i'm using spring boot embedded tomcat with spring boot 1.5.9 ,
im also using Log4j2.
recently i exerience problems during load, so i want to understand better the tomcat logs [Not the access Logs] , i tried (in application.properties) :
logging.level.org.apache.tomcat: INFO
logging.level.org.apache.catalina: INFO
but none of the above worked. is there any other way to achieve it ?
Found it !! You are now able to see the internal Logs of Embedded Tomcat in your App's Log4j log file with 3 easy steps:
1] add to your pom:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jul</artifactId>
</dependency>
2] add to your running arg a new JVM param , e.g:
java -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager -jar target/demo-0.0.1-SNAPSHOT.jar
3] add to your application.properties:
logging.level.org.apache=DEBUG
Enjoy Life ! :)
Explaination:
the problem is because Log4j log levels is not propagated into JUL (which is the actual Logging way Embedded tomcat use) so the above achieves this connection with JUL and Log4j log levels.
Reference:
After reading the Spring boot 1.5.10 release notes (which is not required for the solution) i saw the new documentation that shed light how to achive it and explaination about it:
https://github.com/spring-projects/spring-boot/issues/2923#issuecomment-358451260
I struggled a lot,and didnt find anything of my help.Utlimately I had build "WAR" out of my spring boot application.Deploy it to tomcat instance and
followed below steps,which redirected all the internal tomcat logs(JULI) logs to my application log file.
Delete existing JULI library (CATALINA_HOME/bin/tomcat-juli.jar file) and the existing Tomcat Java Logging configuration file (CATALINA_HOME/conf/logging.properties).
Download JULI Log4j Tomcat library (tomcat-juli.jar) from the Tomcat downloads’ Extras section (http://tomcat.apache.org/download-70.cgi). Place the downloaded file to CATALINA_HOME/bin directory.
Download Tomcat JULI adapters library (tomcat-juli-adapters.jar) from the Tomcat downloads’ Extras section. Place this file in the CATALINA_HOME/lib directory.
Download Log4j (version 1.2 or later), and place the downloaded library file to CATALINA_HOME/lib directory.
Create the Log4j configuration file at the following location: CATALINA_HOME/lib/log4j.properties. Check below log4j configuration matching the default Java Logging configuration.
Restart Tomcat.
Log4j configuration File Matching the Default Tomcat Logging Settings:
log4j.rootLogger=INFO, CATALINA
//Define all the appenders log4j.appender.CATALINA=org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA.File=${catalina.base}/logs/catalina.
log4j.appender.CATALINA.Append=true log4j.appender.CATALINA.Encoding=UTF-8
//Roll-over the log once per day
log4j.appender.CATALINA.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout
log4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.LOCALHOST=org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOCALHOST.File=${catalina.base}/logs/localhost.
log4j.appender.LOCALHOST.Append=true log4j.appender.LOCALHOST.Encoding=UTF-8
log4j.appender.LOCALHOST.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.LOCALHOST.layout = org.apache.log4j.PatternLayout
log4j.appender.LOCALHOST.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.MANAGER=org.apache.log4j.DailyRollingFileAppender
log4j.appender.MANAGER.File=${catalina.base}/logs/manager.
log4j.appender.MANAGER.Append=true log4j.appender.MANAGER.Encoding=UTF-8
log4j.appender.MANAGER.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.HOST-MANAGER=org.apache.log4j.DailyRollingFileAppender
log4j.appender.HOST-MANAGER.File=${catalina.base}/logs/host-manager.
log4j.appender.HOST-MANAGER.Append=true log4j.appender.HOST-MANAGER.Encoding=UTF-8
log4j.appender.HOST-MANAGER.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.HOST-MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.HOST-MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Encoding=UTF-8
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
//Configure which loggers log to which appenders
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].
[localhost]=INFO,
LOCALHOST
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].
[localhost].[/manager]=INFO,MANAGER
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].
[localhost].[/host-manager]=
INFO, HOST-
MANAGER
You can also check a adapter avaiable on GIT # link
In your spring boot application,you can make changes like adding and removing jars,folder from embedded Tomcat server Or even adding custom config files to it using TomcatEmbeddedServletContainerFactory.class ,of spring boot.
For slf4j and Spring Boot 2 hide exceptions from Tomcat and handle them by yourself:
Add to pom:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
Add to config:
#PostConstruct
void postConstruct() {
SLF4JBridgeHandler.install();
}
Add to application.yaml
logging:
level:
org.apache.catalina: off
Handle exception in ErrorController
#Controller
#Slf4j
public class ErrorController implements
org.springframework.boot.web.servlet.error.ErrorController {
private static final String ERROR_PATH = "/error";
#Autowired
private ErrorAttributes errorAttributes;
#Override
public String getErrorPath() {
return ERROR_PATH;
}
#RequestMapping(ERROR_PATH)
public ModelAndView error(HttpServletRequest request) {
return processException(errorAttributes.getError(new ServletWebRequest(request)));
}
}
Default configurations are provided for Java Util Logging, Log4J, Log4J2 and Logback. In each case loggers are pre-configured to use console output with optional file output also available
refer this link : https://stackoverflow.com/questions/31939849/spring-boot-default-log-location/31939886
The embedded tomcat in spring boot internally echoes logs to console.
The default log configuration will echo messages to the console as they are written. So until you explicitly specify a file as you described, it stays in the Console.
From the spring boot logging doc.
You can custmize the logging as per your need.
A log file, generated by org.apache.catalina.valves.AccessLogValve, usually named something like localhost_access_log can be configured like this:
#Configuration
public class EmbeddedTomcatConfig {
#Bean
public TomcatEmbeddedServletContainerFactory containerFactory() {
TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory = new TomcatEmbeddedServletContainerFactory();
AccessLogValve accessLogValve = new AccessLogValve();
// set desired properties like
accessLogValve.setDirectory(...);
tomcatEmbeddedServletContainerFactory.addEngineValves(accessLogValve);
return tomcatEmbeddedServletContainerFactory;
}
}
Or, much better with Spring Boot 2:
#Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> customizer() {
return container -> {
AccessLogValve accessLogValve = new AccessLogValve();
// set desired properties like
accessLogValve.setDirectory("...");
container.addEngineValves(accessLogValve);
};
}

Spring Boot and Log4j Issue

my log4j setup is following. When I run as Boot App, the logs are correctly written in console, debug.log and dump.log. Below is what I do in my program to write log in console and debug.log
static final Logger LOG = Logger.getLogger(EnvironmentLoader.class);
LOG.info("blah blah!");
Below is what I do in my program to write log in dump.log
private static final Logger DUMP_LOG = Logger.getLogger("dumpLogger");
DUMP_LOG.info("blah blah!");
Both works fine if I run as Spring Boot App. If I package it as war and run in tomcat, DUMP_LOG writes correctly in dump.log but LOG is not writing in console or debug.log. I wonder why.
log4j.rootLogger=INFO, stdout, debugLog
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %-5p %c.%M:%L - %m%n
log4j.appender.stdout.Target=System.out
log4j.appender.debugLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.debugLog.Append=true
log4j.appender.debugLog.DatePattern='.'dd-MM-yyyy
log4j.appender.debugLog.File=${catalina.base}/logs/debug.log
log4j.appender.debugLog.MaxFileSize=10MB
log4j.appender.debugLog.encoding=UTF-8
log4j.appender.debugLog.layout=org.apache.log4j.PatternLayout
log4j.appender.debugLog.layout.ConversionPattern=%d %-5p %c.%M:%L - %m%n
log4j.category.debugLogger=DEBUG, debugLog
log4j.additivity.debugLogger=false
log4j.appender.dumpLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.dumpLog.Append=true
log4j.appender.dumpLog.DatePattern='.'dd-MM-yyyy
log4j.appender.dumpLog.File=${catalina.base}/logs/dump.log
log4j.appender.dumpLog.MaxFileSize=10MB
log4j.appender.dumpLog.encoding=UTF-8
log4j.appender.dumpLog.layout=org.apache.log4j.PatternLayout
log4j.appender.dumpLog.layout.ConversionPattern=%d - %m%n
log4j.category.dumpLogger=DEBUG, dumpLog
log4j.additivity.dumpLogger=false
The following dependency resolved the issue.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>

Stop log messages from appearing in the command line

I have a maven project with the following dependencies for logging:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
My properties file looks like this :
### Default logger configuration ###
log4j.rootLogger = INFO, fa
log4j.appender.fa = org.apache.log4j.FileAppender
log4j.appender.fa.File = logs/ServerLogs.log
log4j.appender.fa.layout = org.apache.log4j.PatternLayout
log4j.appender.fa.layout.ConversionPattern = %d{ISO8601} %p [%t] %c (%F:%L) - %m%n
### Set Settings for Hibernate Logging ###
log4j.logger.org.hibernate = ERROR
### Set Settings for Spring Framework Logging ###
log4j.logger.org.springframework= ERROR
The problem is that when I execute a statement like the following :
logger.info("Initializing DB connection for the first time");
This log message is written in the file that I specified in the properties file but it is also appended in the command line. How can I disable this behavior ? I want the message to printed only in the file , not in the command line .
Any ideas ?
I found out that this disables the log4j completely:
org.apache.log4j.LogManager.resetConfiguration();
org.apache.log4j.LogManager.getRootLogger().addAppender(new NullAppender());
If you still want the logging but directed to a file you can do this:
LogManager.resetConfiguration();
LogManager.getRootLogger().addAppender(new FileAppender(new PatternLayout(), "log.log"));

Resources