我使用spring boot嵌入式tomcat和spring boot 1.5.9, 我也使用Log4j2。
最近我在加载过程中遇到了问题,所以我想更好地理解tomcat日志[不是访问日志],我试过(在application.properties中):
logging.level.org.apache.tomcat: INFO
logging.level.org.apache.catalina: INFO
但以上都没有奏效。有没有其他方法来实现它?
答案 0 :(得分:7)
发现它!!您现在可以通过3个简单步骤在App的Log4j日志文件中查看嵌入式Tomcat的内部日志:
1]加入你的pom:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jul</artifactId>
</dependency>
2]为你的运行arg添加一个新的JVM参数,例如:
java -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager -jar target/demo-0.0.1-SNAPSHOT.jar
3]添加到您的application.properties:
logging.level.org.apache=DEBUG
享受生活! :)
<强>阐释:强> 问题是因为Log4j日志级别没有传播到JUL(这是嵌入式tomcat使用的实际日志记录方式)所以上面的内容实现了与JUL和Log4j日志级别的连接。
<强>参考:强> 在阅读了Spring boot 1.5.10发行说明(解决方案不需要)后,我看到了新的文档,阐明了如何实现它并解释它:
https://github.com/spring-projects/spring-boot/issues/2923#issuecomment-358451260
答案 1 :(得分:1)
我经常苦苦挣扎,并没有找到任何帮助。最初,我在Spring启动应用程序中构建了“WAR”。将它部署到tomcat实例并且 按照以下步骤操作,将所有内部tomcat日志(JULI)日志重定向到我的应用程序日志文件。
删除现有的JULI库(CATALINA_HOME / bin / tomcat-juli.jar文件)和现有的Tomcat Java Logging配置文件(CATALINA_HOME / conf / logging.properties)。
从Tomcat下载'Extras部分(http://tomcat.apache.org/download-70.cgi)下载JULI Log4j Tomcat库(tomcat-juli.jar)。将下载的文件放在CATALINA_HOME / bin目录中。
从Tomcat下载的Extras部分下载Tomcat JULI适配器库(tomcat-juli-adapters.jar)。将此文件放在CATALINA_HOME / lib目录中。
下载Log4j(版本1.2或更高版本),并将下载的库文件放入CATALINA_HOME / lib目录。
在以下位置创建Log4j配置文件:CATALINA_HOME / lib / log4j.properties。检查下面的log4j配置是否与默认的Java Logging配置匹配。
重启Tomcat。
Log4j配置文件匹配默认Tomcat日志记录设置:
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
您还可以检查GIT上可用的适配器 @ link
在Spring启动应用程序中,您可以进行更改,例如添加和删除嵌入式Tomcat服务器中的jar文件夹,甚至可以使用Spring引导的 TomcatEmbeddedServletContainerFactory.class 向其添加自定义配置文件。
答案 2 :(得分:0)
请参阅此url,其中包含常见的应用程序属性,其中还包括应用程序日志记录属性和tomcat级别日志记录属性。无论您使用yaml还是属性文件,spring boot都使用此配置来引导应用程序。搜索以下配置项。
# LOGGING
logging.config= # Location of the logging configuration file. For instance `classpath:logback.xml` for Logback
logging.exception-conversion-word=%wEx # Conversion word used when logging exceptions.
logging.file= # Log file name. For instance `myapp.log`
logging.level.*= # Log levels severity mapping. For instance `logging.level.org.springframework=DEBUG`
logging.path= # Location of the log file. For instance `/var/log`
logging.pattern.console= # Appender pattern for output to the console. Only supported with the default logback setup.
logging.pattern.file= # Appender pattern for output to the file. Only supported with the default logback setup.
logging.pattern.level= # Appender pattern for log level (default %5p). Only supported with the default logback setup.
logging.register-shutdown-hook=false # Register a shutdown hook for the logging system when it is initialized.
server.tomcat.accept-count= # Maximum queue length for incoming connection requests when all possible request processing threads are in use.
server.tomcat.accesslog.buffered=true # Buffer output such that it is only flushed periodically.
server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be relative to the tomcat base dir or absolute.
server.tomcat.accesslog.enabled=false # Enable access log.
server.tomcat.accesslog.file-date-format=.yyyy-MM-dd # Date format to place in log file name.
server.tomcat.accesslog.pattern=common # Format pattern for access logs.
server.tomcat.accesslog.prefix=access_log # Log file name prefix.
server.tomcat.accesslog.rename-on-rotate=false # Defer inclusion of the date stamp in the file name until rotate time.
server.tomcat.accesslog.request-attributes-enabled=false # Set request attributes for IP address, Hostname, protocol and port used for the request.
server.tomcat.accesslog.rotate=true # Enable access log rotation.
server.tomcat.accesslog.suffix=.log # Log file name suffix.
server.tomcat.additional-tld-skip-patterns= # Comma-separated list of additional patterns that match jars to ignore for TLD scanning.
server.tomcat.background-processor-delay=30 # Delay in seconds between the invocation of backgroundProcess methods.
server.tomcat.basedir= # Tomcat base directory. If not specified a temporary directory will be used.
server.tomcat.internal-proxies=10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\
192\\.168\\.\\d{1,3}\\.\\d{1,3}|\\
169\\.254\\.\\d{1,3}\\.\\d{1,3}|\\
127\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\
172\\.1[6-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\
172\\.2[0-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\
172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3} # regular expression matching trusted IP addresses.
server.tomcat.max-connections= # Maximum number of connections that the server will accept and process at any given time.
server.tomcat.max-http-post-size=0 # Maximum size in bytes of the HTTP post content.
server.tomcat.max-threads=0 # Maximum amount of worker threads.
server.tomcat.min-spare-threads=0 # Minimum amount of worker threads.
server.tomcat.port-header=X-Forwarded-Port # Name of the HTTP header used to override the original port value.
server.tomcat.protocol-header= # Header that holds the incoming protocol, usually named "X-Forwarded-Proto".
server.tomcat.protocol-header-https-value=https # Value of the protocol header that indicates that the incoming request uses SSL.
server.tomcat.redirect-context-root= # Whether requests to the context root should be redirected by appending a / to the path.
server.tomcat.remote-ip-header= # Name of the http header from which the remote ip is extracted. For instance `X-FORWARDED-FOR`
server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI.
答案 3 :(得分:0)
为Java Util Logging,Log4J,Log4J2和Logback提供了默认配置。在每种情况下,记录器都预先配置为使用控制台输出,并且可选的文件输出也可用
请参阅此链接:https://stackoverflow.com/questions/31939849/spring-boot-default-log-location/31939886
spring boot中的嵌入式tomcat在内部将日志回显给控制台。 默认日志配置将在写入时将消息回显给控制台。因此,在您按照描述明确指定文件之前,它将保留在控制台中。
您可以根据需要保留日志记录。
答案 4 :(得分:0)
嵌入式tomcat的包是org.springframework.boot.context.embedded.tomcat
因此,将其添加到 application.properties 文件
logging.level.org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer=INFO
答案 5 :(得分:0)
对于slf4j和Spring Boot 2,可以从Tomcat中隐藏异常并自行处理:
添加到pom:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
添加到配置:
@PostConstruct
void postConstruct() {
SLF4JBridgeHandler.install();
}
添加到application.yaml
logging:
level:
org.apache.catalina: off
处理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)));
}
}
答案 6 :(得分:0)
由org.apache.catalina.valves.AccessLogValve
生成的日志文件通常可以像这样localhost_access_log
进行配置:
@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;
}
}