-
Notifications
You must be signed in to change notification settings - Fork 40.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add logback-access-spring-boot-starter to 3rd party starter list #2609
Comments
Hey, I'm trying to get logback-access + tomcat working with spring boot. Has anyone been able to get this working out-of-the-box? Or is there some necessary plumbing to set up? The primary issue we hit is that the LogbackValve (https://github.com/qos-ch/logback/blob/master/logback-access/src/main/java/ch/qos/logback/access/tomcat/LogbackValve.java) takes in a filename that it assumes is accessible from the filesystem. Our log configuration file is bundled with the JAR so doesn't seem to fit that use case. |
@dave-r12 I guess this is a sample project you asked for: https://github.com/izeye/samples-spring-boot-branches/tree/rest-with-tee-filter |
+1 |
@izeye that does not work in a spring boot app right? Does it work in a standard war deploy? |
@dahankzter Have you tried it? IIRC, I checked it's working with an embedded Tomcat at that time. |
@izeye I get stuff like this: Caused by: java.io.FileNotFoundException: class path resource [logback-access.xml] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/apps/..... |
@dahankzter Well, it works for me with both IntelliJ and repackaged jar. In
|
Does it work when you move the jar somewhere where the source is not available? |
@dahankzter Oops. Sorry I didn't check that. You're right. |
As a workaround, you can copy the access xml from the class path to the filesystem and run it there as part of your configuration class
where |
Awesome @mattreyuk ! |
Here's a code snippet if it helps: @Autowired
private ResourceLoader resourceLoader;
// class code, etc.
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
if (container instanceof TomcatEmbeddedServletContainerFactory) {
TomcatEmbeddedServletContainerFactory containerFactory = (TomcatEmbeddedServletContainerFactory) container;
final LogbackValve logbackValve = new LogbackValve();
// load from classpath or if it doesn't work, try the filesystem
try {
final File xmlFile = resourceLoader.getResource(ResourceLoader.CLASSPATH_URL_PREFIX + "logback-access.xml").getFile();
logbackValve.setFilename(xmlFile.getCanonicalPath());
} catch (IOException e) {
log.warn("Couldn't setup logback-acccess with classpath. Falling back to file");
logbackValve.setFilename("logback-access.xml");
}
containerFactory.addContextValves(logbackValve);
}
} |
There is already a project providing that feature: https://github.com/akihyro/spring-boot-ext-logback-access |
@mattreyuk this solution worked, but I am hopping something like spring-boot-ext-logback-access that @adammichalik mentioned is added to spring boot. |
Since logback 1.1.6 there is no need of any workarounds in order to load the logback-access configuration file as a resource. Reference: http://jira.qos.ch/browse/LOGBACK-1069 All you have to do is: |
@Autowired
private ResourceLoader resourceLoader;
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
if (container instanceof JettyEmbeddedServletContainerFactory) {
final JettyEmbeddedServletContainerFactory containerFactory = (JettyEmbeddedServletContainerFactory) container;
containerFactory.addServerCustomizers(new JettyServerCustomizer() {
@Override
public void customize(Server server) {
// set-up request log handler
final RequestLogImpl requestLog = new RequestLogImpl();
// load from classpath or if it doesn't work, try the filesystem
try {
final File logbackFile = resourceLoader.getResource(ResourceLoader.CLASSPATH_URL_PREFIX + "logback-access.xml").getFile();
if (logbackFile != null) {
requestLog.setFileName(logbackFile.getCanonicalPath());
}
} catch (IOException e) {
requestLog.setFileName("logback-access.xml");
}
final RequestLogHandler requestLogHandler = new RequestLogHandler();
requestLogHandler.setHandler(server.getHandler());
requestLogHandler.setRequestLog(requestLog);
server.setHandler(requestLogHandler);
}
});
}
} |
Just to mention that when customising the container to create the logback Valve does not work with management endpoints when they are specified on a different port. (when management.port is different than server.port) |
It should be possible using a |
Even when configuring the LogbackValve manually using a costumizer, the Also adding support for MDC fields will help out a lot during tracing events in the log When can we expect this feature? @wilkinsona Thanks for keeping our dev engine running! |
I've re-purposed this issue to add a link to the 3rd-party starter. |
Nope, that's the same one. Thanks, @izeye! |
No description provided.
The text was updated successfully, but these errors were encountered: