tags:

views:

43

answers:

1

I have implemented log4j through log4j.properties but i have read there is some benefit of using xml of log4j so now i am trying but not getting it...

A: 

I always use the xml. To me it's easier to read and work with and also there are several log4j features that can only be implemented with XML. As JoseK said, if you have more specific questions about what you've tried, let us know.

For some basics, the properties file will be read automatically if it's in a file named log4j.xml located anywhere in the classpath. The easiest way to accomplish this is to put in the root src direcory. In our project, I make a logging folder under the resources directory and add that to the classpath, this allows me to also put the commons-logging.properties file there.


There's no shortage of descriptions online of the structure of log4j xml files. One random google example is here. You can find plenty more on the web.

Hopefully, to help you more, I'll provide my exact logging file from an active project. As well as the accompanying commons-logging.properties file:

    <!--  ================ APPENDERS ================ -->
    <appender name="ProjectAppender" class="org.apache.log4j.FileAppender">
            <param name="File" value="/blazeds/tomcat/logs/all.out" />
            <param name="Append" value="true" />
            <layout class="org.apache.log4j.PatternLayout">
                    <param name="ConversionPattern" value="%m%n" />
            </layout>
    </appender>

    <appender name="AspectAppender" class="org.apache.log4j.FileAppender">
            <param name="File" value="/blazeds/tomcat/logs/aspects.out" />
            <param name="Append" value="true" />
            <layout class="org.apache.log4j.PatternLayout">
                    <param name="ConversionPattern" value="[%d{ABSOLUTE}]\t\t%m%n" />
            </layout>
    </appender>

    <appender name="DebugFileAppender" class="org.apache.log4j.FileAppender">
            <param name="File" value="/blazeds/tomcat/logs/debug.out" />
            <param name="Append" value="true" />
            <layout class="org.apache.log4j.PatternLayout">
                    <param name="ConversionPattern" value="[%d{ABSOLUTE}] %-5p\t%m%n" />
            </layout>
            <filter class="org.apache.log4j.varia.LevelRangeFilter">
                    <param name="LevelMin" value="DEBUG" />
                    <param name="LevelMax" value="DEBUG" />
                    <param name="AcceptOnMatch" value="true" />
            </filter>
            <filter class="org.apache.log4j.varia.DenyAllFilter" />
    </appender>

    <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
            <layout class="org.apache.log4j.SimpleLayout" />
    </appender>

    <appender name="ErrorConsoleAppender" class="org.apache.log4j.ConsoleAppender">
            <layout class="org.apache.log4j.PatternLayout">
                    <param name="ConversionPattern" value="[%d{ABSOLUTE}] %-5p > %m%n" />
            </layout>
            <filter class="org.apache.log4j.varia.LevelRangeFilter">
                    <param name="LevelMin" value="ERROR" />
                    <param name="LevelMax" value="ERROR" />
                    <param name="AcceptOnMatch" value="true" />
            </filter>
            <filter class="org.apache.log4j.varia.DenyAllFilter" />
    </appender>


    <!--  ========= LOGGERS - additive  ==== -->

    <logger name="com.companyname.projectname" additivity="true">
            <level value="ALL" />
            <appender-ref ref="ProjectAppender" />
            <appender-ref ref="DebugFileAppender" />
    </logger>


    <logger name="com.companyname.projectname.aspects" additivity="true">
            <level value="ALL" />
            <appender-ref ref="AspectAppender" />
    </logger>

    <root>
            <priority value="ALL" />
            <appender-ref ref="ErrorConsoleAppender" />
    </root>

We're not logging heavily here so it's a basic enough example to give you the idea. One cool feature of this logging setup is any call to log.error(string) automatically bubbles all the way up to the console.

Place these two files in your src directory, modify the output file paths, and you should see logging show up.

gmale