Reputation: 174
I am trying to use a SocketAppender in a project which uses log4j2 for logging. When I add the configrations for Socket and deploy the application, it throws a parsing error. Below are the details.
log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO" strict="true" packages="">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<Socket name="Socket1" host="localhost" port="4712" reconnectionDelay="5000">
<SerializedLayout/>
</Socket>
</Appenders>
<Loggers>
<Logger name="com.example.web" level="INFO">
<AppenderRef ref="Socket1"/>
</Logger>
<Root level="DEBUG">
<AppenderRef ref="Console" level="DEBUG" />
</Root>
</Loggers>
</Configuration>
POM.XML:
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.6</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.0-rc1</version>
</dependency>
Exception on deploying the project in Tomcat:
log4j:WARN Fatal parsing error 31 and column 6
log4j:WARN Premature end of file.
log4j:ERROR Could not parse url [file:/apps/work/Catalina/example/loader/log4j.xml].
org.xml.sax.SAXParseException: Premature end of file.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:249)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse (DocumentBuilderImpl.java:284)
at org.apache.log4j.xml.DOMConfigurator$2.parse(DOMConfigurator.java:767)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:866)
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
It works if I remove the Socket related changes from log4j2.xml. Could anyone please guide on the right way to add Socket Appender? Or find any issues in above configuration.
Upvotes: 0
Views: 5058
Reputation: 5608
It seems that your problem is not related to Log4j2 configuration file, but with your Log4j (v1) configuration file (/apps/work/Catalina/example/loader/log4j.xml
) that Spring seems to use. We can see org.apache.log4j
in the stack trace, which is clearly Log4j v1 (Log4j v2 is more like org.apache.logging.log4j
).
You may want to post your Log4j v1 file (/apps/work/Catalina/example/loader/log4j.xml
) here so that we can help you if you don't manage to fix the error syntax in this configuration file.
PS: I've tried your log4j2.xml
file and it works great. You may want to replace status="INFO"
with status="trace"
in your Log4j2 configuration file to get more debug info in the console (eg. socket error, bad log4j2 config, ...)
Upvotes: 3