public class LogbackConfigListener extends Object implements javax.servlet.ServletContextListener
ServletContextListener that can be used in web applications to define the location of the
logback configuration and optionally to inject the context path into the properties of logback
Should be the first listener to configure logback before using it. Location is defined in the
logbackConfigLocation context param. Snippet from web.xml:
<!-- mandatory, sets the custom config location -->
<context-param>
<param-name>logbackConfigLocation</param-name>
<param-value>/WEB-INF/log-sc.xml</param-value>
</context-param>
<!-- optional, needed if config wants to use the context path -->
<context-param>
<param-name>logbackConfigContextPathKey</param-name>
<param-value>contextPath</param-value>
</context-param>
<!-- mandatory, applies custom config location based on the value of
logbackConfigLocation -->
<listener>
<listener-class>org.wicketstuff.logback.LogbackConfigListener</listener-class>
</listener>
The above means that logback will be configured using the /WEB-INF/log-sc.xml
servlet context resource and ${contextPath} can be used in the logback config as a
placeholder for the webapp's context path.
Placeholders (ex: ${user.home}) in logbackConfigLocation are supported. Location
examples:
/WEB-INF/log-sc.xml (starts with '/') -> loaded from servlet context
classpath:foo/log-cp.xml (starts with "classpath:") -> loaded from classpath
file:/D:/log-absfile.xml (is a valid url) -> loaded as url
D:/log-absfile.xml (is an absolute file path) -> loaded as absolute file
log-relfile.xml (is a relative file path) -> loaded as file relative to the servlet
container working directory
| Modifier and Type | Field and Description |
|---|---|
static String |
CONFIG_CONTEXT_PATH_KEY_PARAM
Context param name for logback context path property name.
|
static String |
CONFIG_LOCATION_PARAM
Context param name for location.
|
static String |
CONTEXT_PATH_ROOT_VAL
Injected name for root context path ("ROOT").
|
static String |
LOCATION_PREFIX_CLASSPATH
Prefix for classpath urls.
|
| Constructor and Description |
|---|
LogbackConfigListener() |
| Modifier and Type | Method and Description |
|---|---|
protected void |
applyConfigLocation(javax.servlet.ServletContext sc,
ch.qos.logback.classic.LoggerContext lc)
Configures logback with the custom config location specified in the web.xml.
|
protected void |
configureConfig(javax.servlet.ServletContext sc,
ch.qos.logback.classic.LoggerContext lc,
URL location,
String contextPathKey)
Reconfigures logback to use the config from the specified url.
|
void |
contextDestroyed(javax.servlet.ServletContextEvent sce) |
void |
contextInitialized(javax.servlet.ServletContextEvent sce) |
protected String |
getContextPath(javax.servlet.ServletContext sc)
Returns a normalized context path for the webapp.
|
protected URL |
toUrl(javax.servlet.ServletContext sc,
String location)
Converts the passed in location String to an URL.
|
public static final String CONFIG_LOCATION_PARAM
public static final String CONFIG_CONTEXT_PATH_KEY_PARAM
public static final String CONTEXT_PATH_ROOT_VAL
public static final String LOCATION_PREFIX_CLASSPATH
public void contextInitialized(javax.servlet.ServletContextEvent sce)
contextInitialized in interface javax.servlet.ServletContextListenerprotected void applyConfigLocation(javax.servlet.ServletContext sc,
ch.qos.logback.classic.LoggerContext lc)
sc - represents the webapp, not nulllc - logback context to be configured, not nullprotected void configureConfig(javax.servlet.ServletContext sc,
ch.qos.logback.classic.LoggerContext lc,
URL location,
String contextPathKey)
sc - represents the webapp, not nulllc - logback context to be configured, not nulllocation - point to the logback config, not nullcontextPathKey - nullable, a not null value is the property name under which the webapp context
path is storedprotected String getContextPath(javax.servlet.ServletContext sc)
CONTEXT_PATH_ROOT_VAL is returned.sc - represents the webapp, not nullprotected URL toUrl(javax.servlet.ServletContext sc, String location)
sc - represents the webapp, not nulllocation - holding the config resource location as string, not nullpublic void contextDestroyed(javax.servlet.ServletContextEvent sce)
contextDestroyed in interface javax.servlet.ServletContextListenerCopyright © 2015. All rights reserved.