Spring 启动,Tomcat 访问日志和打包应用程序中的 Logback 配置文件
Spring Boot, Tomcat Access Log with Logback config file in a packaged application
如何在 EmbeddedServletContainerCustomizer-Bean 中正确配置我的 LogbackValve 以读取 class 路径上的文件?
现在我有以下配置,它在我的本地主机上完美运行(logging.accessLogConfig: src/main/resources/logback_access_dev.xml
在 application.yml 中设置):
@Configuration
public class TomcatConfiguration {
@Value("${logging.accessLogConfig}")
private String accessLogConfig;
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return new EmbeddedServletContainerCustomizer() {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
if (container instanceof TomcatEmbeddedServletContainerFactory) {
TomcatEmbeddedServletContainerFactory containerFactory =
(TomcatEmbeddedServletContainerFactory) container;
LogbackValve logbackValve = new LogbackValve();
logbackValve.setFilename(accessLogConfig);
containerFactory.addContextValves(logbackValve);
}
}
};
}
}
但是一旦我将它作为打包应用程序部署到我的服务器上,就再也找不到该文件了:-WARN in ch.qos.logback.access.tomcat.LogbackValve[] - [src/main/resources/logback_access_dev.xml] does not exist
我也在尝试更改 logging.accesslogConfig: classpath:logbook_access_dev.xml
,并将内容读取到临时文件,但这也不起作用:
@Configuration
public class TomcatConfiguration {
private static final Logger logger = LoggerFactory.getLogger(TomcatConfiguration.class);
@Value("${logging.accessLogConfig}")
private Resource accessLogConfig;
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return new EmbeddedServletContainerCustomizer() {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
if (container instanceof TomcatEmbeddedServletContainerFactory) {
TomcatEmbeddedServletContainerFactory containerFactory =
(TomcatEmbeddedServletContainerFactory) container;
try {
InputStream configuration = accessLogConfig.getInputStream();
File configFile = File.createTempFile(accessLogConfig.getFilename(), "tmp");
FileUtils.copyInputStreamToFile(configuration, configFile);
LogbackValve logbackValve = new LogbackValve();
logbackValve.setFilename(configFile.getAbsolutePath());
containerFactory.addContextValves(logbackValve);
}
catch (IOException e) {
logger.warn("could not read access log configuration {}", accessLogConfig);
}
}
}
};
}
}
我正在使用 Spring Boot 1.2.7.RELEASE 和 logback-access 1.1.3.
非常感谢任何有关如何获得 运行 的帮助:)
现在通过创建我自己的 SpringLogbackValve 来解决问题,它接受 InputStream 而不是文件名。它基本上是 ch.qos.logback.access.tomcat.LogbackValve 的克隆,只是用 Inputstream inputStream
而不是 String fileName
和以下方法:
@Override
public void startInternal() throws LifecycleException {
executorService = ExecutorServiceUtil.newExecutorService();
if (inputStream != null) {
try {
JoranConfigurator jc = new JoranConfigurator();
jc.setContext(this);
jc.doConfigure(inputStream);
}
catch (JoranException e) {
logger.warn("failed to configure {}", inputStream);
}
}
else {
getStatusManager().add(
new WarnStatus("[" + inputStream + "] does not exist", this));
}
if (!quiet) {
StatusPrinter.print(getStatusManager());
}
started = true;
setState(LifecycleState.STARTING);
}
如何在 EmbeddedServletContainerCustomizer-Bean 中正确配置我的 LogbackValve 以读取 class 路径上的文件?
现在我有以下配置,它在我的本地主机上完美运行(logging.accessLogConfig: src/main/resources/logback_access_dev.xml
在 application.yml 中设置):
@Configuration
public class TomcatConfiguration {
@Value("${logging.accessLogConfig}")
private String accessLogConfig;
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return new EmbeddedServletContainerCustomizer() {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
if (container instanceof TomcatEmbeddedServletContainerFactory) {
TomcatEmbeddedServletContainerFactory containerFactory =
(TomcatEmbeddedServletContainerFactory) container;
LogbackValve logbackValve = new LogbackValve();
logbackValve.setFilename(accessLogConfig);
containerFactory.addContextValves(logbackValve);
}
}
};
}
}
但是一旦我将它作为打包应用程序部署到我的服务器上,就再也找不到该文件了:-WARN in ch.qos.logback.access.tomcat.LogbackValve[] - [src/main/resources/logback_access_dev.xml] does not exist
我也在尝试更改 logging.accesslogConfig: classpath:logbook_access_dev.xml
,并将内容读取到临时文件,但这也不起作用:
@Configuration
public class TomcatConfiguration {
private static final Logger logger = LoggerFactory.getLogger(TomcatConfiguration.class);
@Value("${logging.accessLogConfig}")
private Resource accessLogConfig;
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return new EmbeddedServletContainerCustomizer() {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
if (container instanceof TomcatEmbeddedServletContainerFactory) {
TomcatEmbeddedServletContainerFactory containerFactory =
(TomcatEmbeddedServletContainerFactory) container;
try {
InputStream configuration = accessLogConfig.getInputStream();
File configFile = File.createTempFile(accessLogConfig.getFilename(), "tmp");
FileUtils.copyInputStreamToFile(configuration, configFile);
LogbackValve logbackValve = new LogbackValve();
logbackValve.setFilename(configFile.getAbsolutePath());
containerFactory.addContextValves(logbackValve);
}
catch (IOException e) {
logger.warn("could not read access log configuration {}", accessLogConfig);
}
}
}
};
}
}
我正在使用 Spring Boot 1.2.7.RELEASE 和 logback-access 1.1.3.
非常感谢任何有关如何获得 运行 的帮助:)
现在通过创建我自己的 SpringLogbackValve 来解决问题,它接受 InputStream 而不是文件名。它基本上是 ch.qos.logback.access.tomcat.LogbackValve 的克隆,只是用 Inputstream inputStream
而不是 String fileName
和以下方法:
@Override
public void startInternal() throws LifecycleException {
executorService = ExecutorServiceUtil.newExecutorService();
if (inputStream != null) {
try {
JoranConfigurator jc = new JoranConfigurator();
jc.setContext(this);
jc.doConfigure(inputStream);
}
catch (JoranException e) {
logger.warn("failed to configure {}", inputStream);
}
}
else {
getStatusManager().add(
new WarnStatus("[" + inputStream + "] does not exist", this));
}
if (!quiet) {
StatusPrinter.print(getStatusManager());
}
started = true;
setState(LifecycleState.STARTING);
}