log4j: how to enable it ?

Help
giluka
2013-06-10
2013-06-13
  • giluka
    giluka
    2013-06-10

    Hi all,
    how can i enable log4j on OX ?
    In /properties/log4j.properties i've a configuration properties file that i don't understand how to initialize ...

    Thanks.

     
  • Granjero Moe
    Granjero Moe
    2013-06-12

    Hi giluka.

    I added log4j to my OX project in this way:

    Add these lines to your web.xml file:

    <!-- Servlet para inicializar la configuración de Log4J -->
    <servlet>
        <servlet-name>log4j-init</servlet-name>
        <servlet-class>project.web.servlet.Log4jInit</servlet-class>
        <init-param>
            <param-name>log4j-init-file</param-name>
            <param-value>WEB-INF/classes/log4j.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    

    Add the servlet Log4jInit

    package project.web.servlet;
    
    import javax.servlet.http.HttpServlet;
    
    import org.apache.log4j.xml.DOMConfigurator;
    
    public class Log4jInit extends HttpServlet {
    
        @Override
        public void init() {
            String prefix = getServletContext().getRealPath("/");
            String file = getInitParameter("log4j-init-file");
            // if the log4j-init-file is not set, then no point in trying
            if (file != null) {
                DOMConfigurator.configure(prefix + file);
            }
        }
    
    }
    

    And add the config log4j.xml file. This is an example:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE log4j:configuration SYSTEM "dtds/log4j.dtd" >
    
    <log4j:configuration>
    
        <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
            <param name="Target" value="System.out" />
            <layout class="org.apache.log4j.PatternLayout">
                <!-- 
                <param name="ConversionPattern"
                    value="[%d{ISO8601}] %-5p [%c] %m %n" />
                -->
                <param name="ConversionPattern" value="%-5p [%c] %m %n" />
            </layout>
        </appender>
    
        <appender name="STDERR" class="org.apache.log4j.ConsoleAppender">
            <param name="Target" value="System.err" />
            <layout class="org.apache.log4j.PatternLayout">
                <param name="ConversionPattern"
                    value="[%d{ISO8601}] %-5p [%c] %m %n" />
            </layout>
            <filter class="org.apache.log4j.varia.LevelRangeFilter">
                <param name="LevelMin" value="ERROR" />
                <param name="LevelMax" value="FATAL" />
            </filter>
        </appender>
    
        <appender name="SW_ROLLING_FILE"
            class="org.apache.log4j.RollingFileAppender">
            <param name="File" value="${catalina.home}/logs/project_log4j.log" />
            <param name="Append" value="true" />
            <param name="MaxFileSize" value="500KB" />
            <param name="MaxBackupIndex" value="5" />
            <layout class="org.apache.log4j.PatternLayout">
                <param name="ConversionPattern"
                    value="[%d{ISO8601}] %-5p %m%n" />
            </layout>
        </appender>
    
        <root>
            <level value="ERROR" />
            <appender-ref ref="STDERR" />
            <appender-ref ref="SW_ROLLING_FILE" />
        </root>
    
    </log4j:configuration>
    
     
  • giluka
    giluka
    2013-06-13

    thanks !!