[ 
https://issues.apache.org/jira/browse/LOG4J2-293?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13754049#comment-13754049
 ] 

Abhinav Shah edited comment on LOG4J2-293 at 8/29/13 8:58 PM:
--------------------------------------------------------------

h3. I found a work around to this problem. 
h4. You would need spring-core-3.2.0 jar for this solution to work.

{code}
import java.io.FileNotFoundException;
import java.net.URL;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.logging.log4j.core.web.Log4jServletContextListener;
import org.springframework.util.ResourceUtils;
import org.springframework.util.SystemPropertyUtils;
import org.springframework.web.util.WebUtils;

public class MyLog4jServletContextListener implements ServletContextListener {
        private ServletContext servletContext;
        private Log4jServletContextListener log4jServletContextListener;

        @Override
        public void contextInitialized(ServletContextEvent sce) {
                this.servletContext = sce.getServletContext();
                String location = 
servletContext.getInitParameter("log4jConfig");

                // Perform actual log4j initialization; else rely on log4j's 
default
                // initialization.
                try {
                        // Resolve system property placeholders before 
potentially
                        // resolving a real path.
                        location = 
SystemPropertyUtils.resolvePlaceholders(location);

                        // Leave a URL (e.g. "classpath:" or "file:") as-is.
                        if (!ResourceUtils.isUrl(location)) {
                                // Consider a plain file path as relative to 
the web
                                // application root directory.
                                location = WebUtils.getRealPath(servletContext, 
location);
                        }

                        URL url = ResourceUtils.getURL(location);
                        if (url == null) {
                                throw new FileNotFoundException(location
                                                + " cannot be resolved to 
absolute file path "
                                                + "because it does not reside 
in the file system");
                        }
                        
sce.getServletContext().setInitParameter("log4jConfiguration",
                                        url.toString());
                } catch (FileNotFoundException ex) {
                        throw new IllegalArgumentException(
                                        "Invalid 'log4jConfigLocation' 
parameter: "
                                                        + ex.getMessage());
                }
                log4jServletContextListener = new Log4jServletContextListener();
                log4jServletContextListener.contextInitialized(sce);
        }

        @Override
        public void contextDestroyed(ServletContextEvent sce) {
                log4jServletContextListener.contextDestroyed(sce);
        }
{code}

h4. web.xml
{code:xml}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
        xmlns="http://java.sun.com/xml/ns/javaee";
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd";
        id="WebApp_ID" version="3.0" metadata-complete="true">
        <absolute-ordering />
        <display-name>pec-service</display-name>
        <context-param>
                <param-name>log4jConfig</param-name>
                
<param-value>classpath:log4j/${com.labcorp.app.env}.log4j.xml</param-value>
        </context-param>
         
        <listener>
                
<listener-class>com.eag.util.web.MyLog4jServletContextListener</listener-class>
        </listener>     
         
        <filter>
                <filter-name>log4jServletFilter</filter-name>
                
<filter-class>org.apache.logging.log4j.core.web.Log4jServletFilter</filter-class>
        </filter>
        <filter-mapping>
                <filter-name>log4jServletFilter</filter-name>
                <url-pattern>/*</url-pattern>
                <dispatcher>REQUEST</dispatcher>
                <dispatcher>FORWARD</dispatcher>
                <dispatcher>INCLUDE</dispatcher>
                <dispatcher>ERROR</dispatcher>
        </filter-mapping>
        
        
</web-app>
{code}
                
      was (Author: abhinav.s...@gmail.com):
    h3. I found a work around to this problem. 
h2. You would need spring-core-3.2.0 jar for this solution to work.

{code}
import java.io.FileNotFoundException;
import java.net.URL;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.logging.log4j.core.web.Log4jServletContextListener;
import org.springframework.util.ResourceUtils;
import org.springframework.util.SystemPropertyUtils;
import org.springframework.web.util.WebUtils;

public class MyLog4jServletContextListener implements ServletContextListener {
        private ServletContext servletContext;
        private Log4jServletContextListener log4jServletContextListener;

        @Override
        public void contextInitialized(ServletContextEvent sce) {
                this.servletContext = sce.getServletContext();
                String location = 
servletContext.getInitParameter("log4jConfig");

                // Perform actual log4j initialization; else rely on log4j's 
default
                // initialization.
                try {
                        // Resolve system property placeholders before 
potentially
                        // resolving a real path.
                        location = 
SystemPropertyUtils.resolvePlaceholders(location);

                        // Leave a URL (e.g. "classpath:" or "file:") as-is.
                        if (!ResourceUtils.isUrl(location)) {
                                // Consider a plain file path as relative to 
the web
                                // application root directory.
                                location = WebUtils.getRealPath(servletContext, 
location);
                        }

                        URL url = ResourceUtils.getURL(location);
                        if (url == null) {
                                throw new FileNotFoundException(location
                                                + " cannot be resolved to 
absolute file path "
                                                + "because it does not reside 
in the file system");
                        }
                        
sce.getServletContext().setInitParameter("log4jConfiguration",
                                        url.toString());
                } catch (FileNotFoundException ex) {
                        throw new IllegalArgumentException(
                                        "Invalid 'log4jConfigLocation' 
parameter: "
                                                        + ex.getMessage());
                }
                log4jServletContextListener = new Log4jServletContextListener();
                log4jServletContextListener.contextInitialized(sce);
        }

        @Override
        public void contextDestroyed(ServletContextEvent sce) {
                log4jServletContextListener.contextDestroyed(sce);
        }
{code}

h4. web.xml
{code:xml}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
        xmlns="http://java.sun.com/xml/ns/javaee";
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd";
        id="WebApp_ID" version="3.0" metadata-complete="true">
        <absolute-ordering />
        <display-name>pec-service</display-name>
        <context-param>
                <param-name>log4jConfig</param-name>
                
<param-value>classpath:log4j/${com.labcorp.app.env}.log4j.xml</param-value>
        </context-param>
         
        <listener>
                
<listener-class>com.eag.util.web.MyLog4jServletContextListener</listener-class>
        </listener>     
         
        <filter>
                <filter-name>log4jServletFilter</filter-name>
                
<filter-class>org.apache.logging.log4j.core.web.Log4jServletFilter</filter-class>
        </filter>
        <filter-mapping>
                <filter-name>log4jServletFilter</filter-name>
                <url-pattern>/*</url-pattern>
                <dispatcher>REQUEST</dispatcher>
                <dispatcher>FORWARD</dispatcher>
                <dispatcher>INCLUDE</dispatcher>
                <dispatcher>ERROR</dispatcher>
        </filter-mapping>
        
        
</web-app>
{code}
                  
> classloader URI scheme broken or insufficient when using Log4jContextListener
> -----------------------------------------------------------------------------
>
>                 Key: LOG4J2-293
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-293
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: Configurators
>    Affects Versions: 2.0-beta7
>            Reporter: Neale Upstone
>            Assignee: Nick Williams
>              Labels: documentation
>
> I'm trying to migrate to Log4j2, and things looked promising when I spotted 
> Log4jContextListener.
> However, there are too many holes.
> Firstly, I tried using classpath: as a scheme, and nothing blew up, so I 
> assumed I'd got it right.
> Then I *looked at the code* (which shouldn't be how we find out) and 
> eventually discovered some code relating to a 'classloader' scheme.
> Still silent failure.  It seems that the classpath is not being searched, 
> perhaps just the WAR classloader, not the JARs in WEB-INF/lib.
> Next I tried omitting the / (i.e. using classloader:log4j2.xml) and got a 
> NullPointerException.
> Can you please document what schemes are supported and what you expect them 
> to do, and *not fail silently* when a configuration file is specified, but 
> nothing happens.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscr...@logging.apache.org
For additional commands, e-mail: log4j-dev-h...@logging.apache.org

Reply via email to