How to configure port in Apache Chainsaw with Log4j2 - log4j2

I got log4j2.xml that generates log files. Here is the config:
<Configuration status="INFO" advertiser="multicastdns">
<Properties>
<Property name="layout">%d | %-5p | [%t] | %c{2} | %M | %m%n
</Property>
</Properties>
<Appenders>
<RollingFile name="LogFile" fileName="${sys:user.home}/logs/webapp.log"
filePattern="${sys:user.home}/logs/webapp-%d{yyyy-MM-dd}-%i.log"
bufferedIO="false" advertiseURI="file:///C://users/bilguuna/logs/webapp.log"
advertise="true">
<PatternLayout pattern="${layout}" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="1 MB" />
</Policies>
<DefaultRolloverStrategy max="10" />
</RollingFile>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="LogFile" level="INFO" />
</Root>
</Loggers>
</Configuration>
I'm able to see a log file using
file:///C://users/bilguuna/logs/webapp.log
on the browser.
A problem is that on the ZeroConf tab, my appender "LogFile" is appeared with Connection status "Connected". But when I double click on that row, it's just changed to disconnected/connected. When I check Chainsaw-log. It said that Connection refused as image below:
I guess it's because of that Chainsaw chooses 4555 as a default port which is not open on my machine. So what port should choose and how to set it in my log4j2.xml file?
Thanks
UPDATE: After I used the developer snapshot version as #Scott suggested, it seems like "connection refused" problem went away. But still I can't see the logs. When I click on entry at ZeroConf tab, it still changes the Connection Status to Connected/Not Connected. Here is the log from chainsaw-log tab:
Again, was I supposed to see the actual logs when I double click on entry at ZeroConf tab?
Update: I got following exception on my console:
WARNING: SocketListener(WS00943.local.).run() exception
java.io.IOException: DNSIncoming corrupted message
at javax.jmdns.impl.DNSIncoming.<init>(DNSIncoming.java:239)
at javax.jmdns.impl.SocketListener.run(SocketListener.java:50)
Caused by: java.lang.IllegalStateException: Can't overwrite cause with java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load [javax.jmdns.impl.constants.DNSResultCode]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
at java.lang.Throwable.initCause(Unknown Source)
at org.apache.catalina.loader.WebappClassLoaderBase.checkStateForClassLoading(WebappClassLoaderBase.java:1344)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1206)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
at javax.jmdns.impl.DNSIncoming.readAnswer(DNSIncoming.java:342)
at javax.jmdns.impl.DNSIncoming.<init>(DNSIncoming.java:229)
... 1 more
Caused by: java.lang.ClassNotFoundException
at org.apache.catalina.loader.WebappClassLoaderBase.checkStateForClassLoading(WebappClassLoaderBase.java:1343)
... 5 more

The problem is you need to use the latest developer snapshot of Chainsaw to work with log4j2. The webstart-runnable version works only with log4j1.
Developer snapshot is available at: http://home.apache.org/~sdeboy/

Related

Log4j2 stops logging to log file after clearing log file

I am running my application on a WebLogic within a Linux environment. after clearing the content of the log file using ">log4j2.log". my application stops logging at all.
I have tried restarting my application, and then the server but still no logs.
also tried deleting the file using rm -f and then doing the restart but still no logs. see the config below.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" monitorInterval="60">
<Appenders>
<RollingFile name="RollingFileAppender"
fileName="./PortalLogs/log4j2.log"
filePattern="./PortalLogs/log4j2.log.%i.%d{yyyy-MM-dd}"
immediateFlush="true">
<PatternLayout>
<Pattern>%d %-5p [%X{messageExtTxt}] [%c] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" />
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
<RegexFilter regex="*DefaultMessageListenerContainer*" onMatch="DENY" onMismatch="ACCEPT"/>
<DefaultRolloverStrategy max="30"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="org.springframework.jms.listener.DefaultMessageListenerContainer" level="ERROR" >
<AppenderRef ref="RollingFileAppender"/>
</Logger>
<Root level="INFO">
<AppenderRef ref="RollingFileAppender" level="INFO"/>
</Root>
</Loggers>
</Configuration>
When I tried using delete from WinSCP instead and then restarted the server, the log files were again produced and with logs in it.
My question is that is there a way to clean the log files safely without causing this issue? thanks in advance!
EDIT: Linux's RM on the log file before restarting the server actually works and recreates the log file, but still is there a way to make it work using ">log4j2.log" or after modifying the file? we need this as we have a protocol to do an initial run for automated tests, clear everything and then do an actual run.

facing issue with log4j2 : java.lang.ExceptionInInitializerError

I'm facing issue with log4j2
below is my log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="com.opensymphony.xwork2" level="info"/>
<Logger name="org.apache.struts2" level="info"/>
<Root level="info">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
below is the exception
<Jun 21, 2018 7:23:48 PM IST> <Error> <HTTP> <BEA-101165> <Could not load user defined filter in web.xml: org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter.
java.lang.ExceptionInInitializerError
at org.apache.logging.log4j.core.impl.Log4jLogEvent.createContextData(Log4jLogEvent.java:472)
at org.apache.logging.log4j.core.impl.Log4jLogEvent.<init>(Log4jLogEvent.java:331)
at org.apache.logging.log4j.core.impl.DefaultLogEventFactory.createEvent(DefaultLogEventFactory.java:54)
at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:401)
at org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(AwaitCompletionReliabilityStrategy.java:63)
Truncated. see log file for complete stacktrace
Caused By: java.lang.IllegalArgumentException: Initial capacity must be at least one but was 0
at org.apache.logging.log4j.util.SortedArrayStringMap.<init>(SortedArrayStringMap.java:102)
at org.apache.logging.log4j.core.impl.ContextDataFactory.createContextData(ContextDataFactory.java:109)
at org.apache.logging.log4j.core.impl.ContextDataFactory.<clinit>(ContextDataFactory.java:57)
at org.apache.logging.log4j.core.impl.Log4jLogEvent.createContextData(Log4jLogEvent.java:472)
at org.apache.logging.log4j.core.impl.Log4jLogEvent.<init>(Log4jLogEvent.java:331)
Truncated. see log file for complete stacktrace
>
above exception resulting to failure of war file deployment.
below are the jars used
1.commons-fileupload-1.3.3.jar
2.commons-io-2.5.jar
3.commons-lang3-3.6.jar
4.commons-logging-1.1.3.jar
5.freemarker-2.3.26-incubating.jar
6.javassist-3.20.0-GA.jar
7.log4j-1.2-api-2.11.0.jar
8.log4j-api-2.10.0.jar
9.log4j-core-2.11.0.jar
10.ognl-3.1.15.jar
11.struts2-core-2.5.16.jar
what do i need to correct?
Try upgrading log4j-api-2.10.0.jar to 2.11.0.
IllegalArgumentException is thrown from the constructor of org.apache.logging.log4j.util.SortedArrayStringMap:
public SortedArrayStringMap(final int initialCapacity) {
if (initialCapacity < 1) {
throw new IllegalArgumentException("Initial capacity must be at least one but was " + initialCapacity);
}
threshold = ceilingNextPowerOfTwo(initialCapacity);
}
and the given parameter initialCapacity has been changed since 2.11 as follows:
https://github.com/apache/logging-log4j2/blob/log4j-2.10.0/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ContextDataFactory.java#L54
https://github.com/apache/logging-log4j2/blob/log4j-2.11.0/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ContextDataFactory.java#L57
Updating the log4j-api and log4j-core from 2.11.1 to 2.12.0 solved the same problem for me.

Log4j2 not logging statements from WAR and Non-EJB jars in an EAR

I have an unpacked EAR deployed at $JBOSS_HOME/standalone/deployments in Wildfly-8.2.0.Final AS. It contains unpacked WAR and application jar files.
HelloWorldEar-0.0.1-SNAPSHOT.ear
-HelloWorldServlet-0.0.1-SNAPSHOT.war
-lib
-META-INF
-aspectjrt-1.8.6.jar
-HelloWorldAnnotation-0.0.1-SNAPSHOT.jar
-HelloWorldAspect-0.0.1-SNAPSHOT.jar
-HelloWorldCommonLib-0.0.1-SNAPSHOT.jar
-HelloWorldEJB-0.0.1-SNAPSHOT.jar
-log4j2.xml
lib folder of EAR contains log4j2 related APIs for providing logging feature.
lib
-commons-logging-1.2.jar
-HelloWorldLog4jPlugin-0.0.1-SNAPSHOT.jar (Jar containing custom log4j2 plugins)
-log4j-api-2.4.1.jar
-log4j-core-2.4.1.jar
-log4j-jcl-2.4.1.jar
-log4j-web-2.4.1.jar
I am initializing log4j2 configuration by initializing log4j2.xml using in a startup singleton bean in HelloWorldEJB-0.0.1-SNAPSHOT.jar.
String path = System.getProperty("jboss.home.dir")
+ "/standalone/deployments/HelloWorldEar-0.0.1-SNAPSHOT.ear/log4j2.xml";
ConfigurationSource source;
File configFile = new File(path);
try {
source = new ConfigurationSource(new FileInputStream(configFile), configFile);
Configurator.initialize(null, source);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
log4j2.xml content
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
<Appenders>
<RollingFile name="SERVER_FILE" fileName="${sys:LOGS}/sample.log" filePattern="${sys:LOGS}/sample.log.%i" append="true">
<PatternLayout>
<pattern>%d %-5p [%c{1}] [EventId: %e] [%t] %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="500 KB" />
</Policies>
<DefaultRolloverStrategy max="5"/>
</RollingFile>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout>
<pattern>%d{ABSOLUTE} %-5p [%c{1}] %m%n</pattern>
</PatternLayout>
</Console>
</Appenders>
<Loggers>
<Logger name="com.test.prototype.log4j" level="debug">
<AppenderRef ref="SERVER_FILE"/>
</Logger>
<Root level="debug" additivity="false">
<AppenderRef ref="CONSOLE"/>
</Root>
</Loggers>
</Configuration>
All the sub-packages and classes are defined under the package com.test.prototype.log4j.
Log statements added in the classes of HelloWorldEJB-0.0.1-SNAPSHOT.jar gets logged in the log file.
My issue is that 1. log statements from other application jars and WAR are not getting logged into the log file.
2. Logging not working through commons-logging API though required system parameters for log4j2 is added.
System.setProperty("org.apache.commons.logging.Log",
"org.apache.commons.logging.impl.Log4JLogger");
I have tried adding the class-path in the application jars and below listener in the web.xml of WAR file but nothing worked.
org.apache.logging.log4j.web.Log4jServletContextListener
I need to have log4j2.xml under EAR and it should server for the all the application jars and WARs added in the EAR. Could anyone please help me about how to resolve this issue.
Have you tried specifying the log4j2.xml config file location with system property -Dlog4j.configurationFile=path/to/log4j2.xml?
I suspect that the way you're initializing log4j now is not visible from other WARs.
If this fails, please raise this issue on the log4j2 Jira issue tracker. https://issues.apache.org/jira/browse/LOG4J2

InvocationTargetException with RollingFileAppender in Log4J2

I'm trying to use a RollingFileAppender in Log4J2 and apparently I'm missing something. The file is created with 0 bytes, but no logging goes to it. When I turn on the trace I see the following error message logged (to the console) by log4J2:
2014-07-08 19:51:11,354 DEBUG Starting RollingFileManager c:/logs/blah.log
2014-07-08 19:51:11,464 DEBUG Generated plugins in 0.108079890 seconds
2014-07-08 19:51:11,465 ERROR Unable to invoke method createAppender in class org.apache.logging.log4j.core.appender.RollingFileAppender for element RollingFile java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.logging.log4j.core.config.BaseConfiguration.createPluginObject(BaseConfiguration.java:913)
This is my config file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="TRACE" verbose="false">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<RollingFile name="Blah" fileName="c:/logs/blah.log" filePattern="c:/logs/blah-%i.log">
<PatternLayout pattern="%d %p %c: %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="1 MB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</appenders>
<loggers>
<root level="INFO">
<appender-ref ref="Blah" level="INFO"/>
</root>
</loggers>
</configuration>
It's probably something basic but I can't find it. Can anyone help?
Could you create a ticket on the Log4j2 issue tracker so the team can take a look at it? It may be a bug in version rc2. (I'm assuming you are using rc2 and have both the api and core jars in the classpath.)
Thanks!
I took a look and the class at the beginning of the stack trace (org.apache.logging.log4j.core.config.BaseConfiguration) no longer exists in log4j-core-2.0-rc2. It was renamed to AbstractConfiguration.
So I think this error is caused by an old log4j-core-2.0-rc1 jar that is still floating around somewhere.

log4j2 to chainsaw hello world not working... what am I doing wrong?

I'm trying to stream a basic hello world log message to show up in chainsaw from log4j2. I don't care if it uses "Zeroconf" or not, I just want something that works. I know that my test program is logging messages since they show on the console, and I know it's finding my config file because I can change the format of the messages that get printed in the console, but that's all I know.
My config file (containing various failed guesses):
<?xml version="1.0" encoding="UTF-8"?>
<configuration advertiser="org.apache.logging.log4j.core.net.MulticastDNSAdvertiser">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %m%n"/>
</Console>
<File name="testFile" fileName="logs/test.log" bufferedIO="false" advertiseURI="file://localhost/home/matt/code/ade/logs/test.log" advertise="true">
<XMLLayout />
</File>
<SocketAppender name="socketTest" host="localhost" immediateFlush="true" port="4560" protocol="TCP" advertiseURI="http://localhost" advertise="true">
<XMLLayout />
</SocketAppender>
</appenders>
<loggers>
<root level="TRACE">
<appender-ref ref="Console"/>
<appender-ref ref="testFile"/>
<appender-ref ref="socketTest"/>
</root>
</loggers>
</configuration>
I've tried various combinations of: including jmdns.jar on the classpath, restarting chainsaw at various points, and getting frustrated, but nothing has helped.
Any ideas?
Edit: I figured out why it couldn't read the log files I was saving to disk, (I hadn't been using XMLLayout) so I've updated the question to reflect that I now only need to get streaming working.
The 'advertiser' uses the log4j2 plugin mechanism, so you must provide the 'name' defined on the Advertiser in the configuration - not the fully-qualified class name.
The log4j2 advertisement mechanism currently supports advertisement of FileAppenders and SocketAppenders. However, Chainsaw only supports discovery of log4j2 FileAppenders which are advertised with a PatternLayout. XMLLayout support will show up in the near future.
The latest developer snapshot of Chainsaw must be used in order to leverage log4j2's advertiser mechanism. Chainsaw tarball and DMG available at: http://people.apache.org/~sdeboy/
Chainsaw will discover the advertised fileappender configuration and parse (and tail if you get the latest Chainsaw developer build) the log file - no Chainsaw configuration required.
Note, you need to use a PatternLayout, and JMDNS must be on the classpath of the application using this appender configuration.
Here is an example Log4j2 -appender- configuration that will advertise a fileappender configuration:
<?xml version="1.0" encoding="UTF-8"?>
<configuration advertiser="multicastdns">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %m%n"/>
</Console>
<File name="testFile" fileName="logs/test.log" bufferedIO="false" advertiseURI="file:///localhost/home/matt/code/ade/logs/test.log" advertise="true">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %m%n"/>
</File>
</appenders>
<loggers>
<root level="TRACE">
<appender-ref ref="Console"/>
<appender-ref ref="testFile"/>
</root>
</loggers>
</configuration>
Once you have started your app that is using the appender
configuration, open the 'Zeroconf' tab in Chainsaw.
You should see a row with your appender's name (assuming you added
jmdns to the classpath for the app using the fileappender
configuration).
You can click 'Autoconnect' if you'd like to always start Chainsaw
with this configuration if it is available.
Next, double-click
on the row with the appender name and Chainsaw will start parsing and tailing your log file.
The advertised URL provided in your file appender
configuration must be accessible network-wise to Chainsaw (looks like you are working
locally with Chainsaw and your fileappender, so file:/// paths will
work fine - note the three slashes).
Chainsaw will work best if there are delimiters around each field - square brackets, dashes, etc. as long as that character won't be present in the field you are delimiting.

Resources