4

In my Java Web Service, I am trying to write logs to windows event viewer, using log4j. I am using Eclipse as an IDE to deploy it using Tomcat.

this is my log4j.properties:

log4j.rootLogger=DEBUG, CA, NTEventLog 


log4j.rootLogger=DEBUG, CA, NTEventLog 


#Console Appender 
log4j.appender.CA=org.apache.log4j.ConsoleAppender 
log4j.appender.CA.layout=org.apache.log4j.PatternLayout 
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n 

#File Appender 
log4j.appender.FA=org.apache.log4j.FileAppender 
log4j.appender.FA.File=sample.log 
log4j.appender.FA.layout=org.apache.log4j.PatternLayout 
log4j.appender.FA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

#Event Log Appender
log4j.appender.NTEventLog=org.apache.log4j.nt.NTEventLogAppender
log4j.appender.NTEventLog.source=ESB Web Services


# Set the logger level of File Appender to WARN 
log4j.appender.FA.Threshold = WARN

this is my classpath:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre6">
        <attributes>
            <attribute name="owner.project.facets" value="java"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v6.0">
        <attributes>
            <attribute name="owner.project.facets" value="jst.web"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
    <classpathentry kind="lib" path="dom4j-1.6.1.jar">
        <attributes>
            <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="jide-oss-2.1.1.jar">
        <attributes>
            <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="jtds-1.2.6.jar">
        <attributes>
            <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="poi-3.9-20121203.jar">
        <attributes>
            <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="poi-ooxml-3.9-20121203.jar">
        <attributes>
            <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="poi-ooxml-schemas-3.9-20121203.jar">
        <attributes>
            <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="stax-api-1.0.1.jar">
        <attributes>
            <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="xmlbeans-2.3.0.jar">
        <attributes>
            <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="NTEventLogAppender.amd64.dll">
        <attributes>
            <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="NTEventLogAppender.dll">
        <attributes>
            <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="log4j-1.2.17.jar">
        <attributes>
            <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="commons-logging-1.1.2.jar">
        <attributes>
            <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="commons-logging-1.1.2-javadoc.jar">
        <attributes>
            <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="commons-logging-1.1.2-sources.jar">
        <attributes>
            <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="commons-logging-1.1.2-test-sources.jar">
        <attributes>
            <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="commons-logging-1.1.2-tests.jar">
        <attributes>
            <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="commons-logging-adapters-1.1.2.jar">
        <attributes>
            <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="commons-logging-api-1.1.2.jar">
        <attributes>
            <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="lib" path="commons-logging-tests.jar">
        <attributes>
            <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="output" path="build/classes"/>
</classpath>

once I deploy the the code on tomcat the following

exception occur:

org.apache.commons.logging.LogConfigurationException: User-specified log class 'org.apache.commons.logging.impl.Log4JLogger' cannot be found or is not useable.
    at org.apache.commons.logging.impl.LogFactoryImpl.discoverLogImplementation(LogFactoryImpl.java:874)
    at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:604)
    at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:336)
    at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:310)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:685)
    at org.apache.axis2.deployment.DeploymentEngine.<clinit>(DeploymentEngine.java:76)
    at org.apache.axis2.transport.http.AxisServlet.initConfigContext(AxisServlet.java:584)
    at org.apache.axis2.transport.http.AxisServlet.init(AxisServlet.java:454)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1173)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:809)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)
Roman C
  • 49,761
  • 33
  • 66
  • 176
user1912404
  • 386
  • 4
  • 11
  • 26

3 Answers3

1

I think there's a copy of commons-logging in TOMCAT_HOME/lib, so potentially you could be encountering a classpath conflict, but I'd check the apache commons logging docs to make sure you've got it set up properly.

Biswajit
  • 2,434
  • 2
  • 28
  • 35
1

remove all references of NTEventLog and it will work. If you uncomment this appender it may be work if the source is a valid name.

#log4j.rootLogger=DEBUG,CA,NTEventLog 
log4j.rootLogger=DEBUG,CA        

#Console Appender 
log4j.appender.CA=org.apache.log4j.ConsoleAppender 
log4j.appender.CA.layout=org.apache.log4j.PatternLayout 
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n 

#File Appender 
log4j.appender.FA=org.apache.log4j.FileAppender 
log4j.appender.FA.File=sample.log 
log4j.appender.FA.layout=org.apache.log4j.PatternLayout 
log4j.appender.FA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

#Event Log Appender
#log4j.appender.NTEventLog=org.apache.log4j.nt.NTEventLogAppender
#log4j.appender.NTEventLog.layout=org.apache.log4j.PatternLayout
#log4j.appender.NTEventLog.layout.ConversionPattern=%m%n
#log4j.appender.NTEventLog.source=ESBWebServices


# Set the logger level of File Appender to WARN 
log4j.appender.FA.Threshold = WARN

Do not forget to place NTEventLogAppender.dll, NTEventLogAppender.amd64.dll, NTEventLogAppender.ia64.dll or NTEventLogAppender.x86.dll as appropriate in a directory that is on the PATH of the Windows system. Otherwise, you will get a java.lang.UnsatisfiedLinkError.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • but i got another error :java.lang.UnsatisfiedLinkError: no NTEventLogAppender in java.library.path at java.lang.ClassLoader.loadLibrary(Unknown Source) at java.lang.Runtime.loadLibrary0(Unknown Source) at java.lang.System.loadLibrary(Unknown Source) – user1912404 Apr 03 '13 at 12:06
  • the code is crashing at this line:NTEventLogAppender eventLogAppender = new NTEventLogAppender(); – user1912404 Apr 03 '13 at 12:06
  • am only adding the dll to the classpath as you can see above – user1912404 Apr 03 '13 at 12:06
  • read the answer again, please, I've edited it – Roman C Apr 03 '13 at 12:07
  • you need to put dlls to the path. – Roman C Apr 03 '13 at 12:19
  • No, this is not a `classpath` entry, it's a system `PATH` entry make it in `cmd` or put these dlls to the folder already on the path, for example `%SystemRoot%\System32`. Use `set path` command in `cmd`. – Roman C Apr 03 '13 at 12:32
  • am sorry to ask this, but can you be a bit more specific. I should put all the the dlls in a folder then what exactly ? – user1912404 Apr 03 '13 at 12:45
  • It depends on your system processor architecture, if it `amd64` then put `NTEventLogAppender.amd64.dll`, if it's `32bit` then put `NTEventLogAppender.dll or NTEventLogAppender.x86.dll` make sure it's 32bit dll. – Roman C Apr 03 '13 at 13:05
  • if you run the command `echo %SystemRoot%\System32` it will give you exact location where you should put the library. You should run `cmd` as Administrator to make changes to the system. – Roman C Apr 03 '13 at 13:15
  • org.apache.log4j.nt.NTEventLogAppender.registerEventSource(Ljava/lang/String;Ljava/lang/String;)I java.lang.UnsatisfiedLinkError: org.apache.log4j.nt.NTEventLogAppender.registerEventSource(Ljava/lang/String;Ljava/lang/String;)I at org.apache.log4j.nt.NTEventLogAppender.registerEventSource(Native Method) – user1912404 Apr 03 '13 at 13:21
0

This could be an issue: the class file is present in two jars in your classpath.

Class file : org.apache.commons.logging.impl.Log4JLogger

1st Jar : commons-logging-1.1.2.jar

2nd Jar : commons-logging-adapters-1.1.2.jar

Try to remove the second jar and try again.

Garry
  • 4,493
  • 3
  • 28
  • 48