I have a Spring MVC web app that uses Spring Security 3.2.
It's configured to authenticate users against a MySQL database but users cannot log in. I suspect Spring Security is not connecting to the MySQL database, but there are no log messages displayed.
What do I add to my log4j.properties file to make the Spring Security logs appear?
I tried adding this to the file ...
log4j.category.org.springframework=DEBUG
But that didn't work. Here's my current log4j.properties file ...
status = error
name = PropertiesConfig
filters = threshold
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
appenders = console
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
rootLogger.level = info
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
log4j.category.org.springframework=DEBUG <<<--- adding this didn't work
Any ideas?
Try this :)
log4j.logger.org.springframework=DEBUG
Related
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);
};
}
Thought of sharing the below work as I struggled to find this info:
Requirement is to put the business correlation id(AccCode and Mac) in every log statement. This can be done by using Mapped Diagnostic Context(MDC)..
No need to understand how LOGBack appender->Encoder->Layout work.
Spring boot log4j.properties has below pattern:
LOG_LEVEL_PATTERN=%5p
LOG_PATTERN=[%d{yyyy-MM-dd HH:mm:ss.SSS}] boot%X{context} - ${PID} ${LOG_LEVEL_PATTERN} [%t] --- %c{1}: %m%n
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=${LOG_PATTERN}
Override LOG_LEVEL_PATTERN from application.properties with custom fields:
logging.pattern.level=AccCode:%X{AccCode} Mac:%X{Mac} %5p
Please note, now we have two extra fields defined into the layout. AccCode and Mac. Value for these new fields need to be provided using the MDC:
MDC.put("AccCode", "46014" );
MDC.put("Mac", "2025b9bcf");
Then all the log statements would have AccCode and Mac as below:
2016-09-12 09:11:37.574 **AccCode:46014** **Mac:2025b9bcf** DEBUG 9724 --- [main] c.c.p.sph.http.client.SphApAdminClient : Loging out from Sph
I am working on developing mapreduce using eclipse , and trying to test it using hadoop 2.6.0 windows standalone mode.
But getting the below error for log4j,
How to fix the below appender problem,
No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory)
same problem as hadoop log4j not working but no answer yet
Thanks ,
1- Create the file log4j.properties and put it in the location of
src/main/resources
the content of the log4j.properties
hadoop.root.logger=DEBUG, console
log4j.rootLogger = DEBUG, console
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{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n
it happens because of the hadoop logging framework
Create a log4j.properties file with something like the following:
hadoop.root.logger=DEBUG, console
log4j.rootLogger = DEBUG, console
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{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n
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>
I need simple path setting to use log4j to be used for logging FINEST level of logging. But all forums and discussions show path setting for log4j.properties file either under eclipse or in some dev env. For a non development user, i need to set log4j.properties file, i tried in C:\apache-tomcat-7.0.27\conf path, but dosent help. stdout logs show default logs, note I have deleted default ogging.properties from \conf folder.
Where do i put log4j.properties file in windows tomcat folder ?
well, as official docs say (http://tomcat.apache.org/tomcat-7.0-doc/logging.html), you should have not deleted, the default logging.properites file, present on:
${catalina.base}/conf/logging.properties
,but rather add there what you need:
org.apache.catalina.level=FINEST
and
You would need to ensure the ConsoleHandler's (or FileHandler's') level is also set to collect this threshold, so FINEST or ALL should be set.
Or is there any strict requirement for you to use log4j (as per default JULI is used by tomcat7)?
UPDATE:
OK, as you mentioned, you're interested in the log4j case only. Let me point you to the official documentation: http://tomcat.apache.org/tomcat-7.0-doc/logging.html#Using_Log4j
Please follow the steps present there. As they claim to be working :)
For the explicit points you mentioned, the location of the log4j.properties file:
Create a file called log4j.properties with the following content and save it into $CATALINA_BASE/lib
log4j.rootLogger=FINEST, 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
Compared to the official docs, I just changed the 1.st line of the config to: log4j.rootLogger=FINEST, CATALINA
(But make sure you do the rest of the steps as well - like copying of the jar files,...)