Syslog-ng and Log4j2 configuration - log4j2

I am trying to write to Syslog from Log4J2 and I am having problems connecting to Syslog-ng. I believe the port is the problem, but I could not find anywhere in the syslog-ng.conf file what is the port.
This is my Log4j2 XML file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" packages="com.payon.logging.v2">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{ABSOLUTE} [%x][%X{MASKEDSERVLETPATH}] %5p %c{1}: %k%n"/>
</Console>
<Syslog name="Syslog" host="localhost" port="514" protocol="TCP">
<PatternLayout pattern="%d{ABSOLUTE} [%x][%X{MASKEDSERVLETPATH}] %5p %c{1}: %m%n"/>
</Syslog>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console"/>
<AppenderRef ref="Syslog"/>
</Root>
</Loggers>
</Configuration>
Syslog-ng is running:
service syslog-ng status
● syslog-ng.service - System Logger Daemon
Loaded: loaded (/lib/systemd/system/syslog-ng.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2021-02-15 10:08:28 CET; 31min ago
Docs: man:syslog-ng(8)
Main PID: 745 (syslog-ng)
Tasks: 1 (limit: 4915)
Memory: 11.9M
CGroup: /system.slice/syslog-ng.service
└─745 /usr/sbin/syslog-ng -F
However, I am getting this error: ERROR TcpSocketManager (TCP:localhost:514) caught exception and will continue: java.io.IOException: Unable to create socket for localhost at port 514 using ip addresses and ports
What am I missing in the configuration? With Log4j1, I did not have to provide a port

<Syslog name="Syslog" host="localhost" port="514" protocol="TCP"> requires a network source that needs to be specified in the syslog-ng configuration, for example:
source { network(port(514)); };
Alternatively, default-network-drivers() can be used, which sets good defaults (TCP/UDP 514 and 601):
log {
source { default-network-drivers(); };
# ...
};

Related

log4j2 not logging to a file

Java 1.8, WebSphere Liberty 19.0.0.3 running in localhost, log4j v.2.17.1, Maven v.3.5.2
I have read some posts of similar issues, but I have not seen a solution that works for my case.
I cannot get anything to write to the log files. Presently, I am focusing on the root logger, as that writes to both the console and to file.
Pom file:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.17.1</version>
</dependency>
Here is the configuration for the root logger:
<Root>
<!-- change level to EROR -->
<Level value="TRACE"/>
<AppenderRef ref="APS-FILE"/>
<AppenderRef ref="STDOUT"/>
</Root>
The root logger is correctly calling the STDOUT appender and writing to the console. Note the pair of asterisks. Those match the output I am seeing in the console.
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout>
<Pattern>"** %d{DATE} %5p %c{1}:%L - %m%n **"</Pattern>
</PatternLayout>
</Console>
console output:
"** 07 Jan 2022 09:50:42,331 INFO WSWebSsoFilter:44 - Exiting WSWebSsoFilter.doFilter **"
"** 07 Jan 2022 09:50:42,331 INFO WSWebSsoFilter:44 - Exiting WSWebSsoFilter.doFilter **"
This is the appender configuration for root logger to write to file.
<RollingFile name="APS-FILE" fileName="/logs/aps/${company-code}/aps-A.log"
filePattern="logs/aps/${company-code}/aps-1.log">
<PatternLayout>
<Pattern>"%d{DATE} %5p %c{1}:%L - %m%n"</Pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy minSize="0"/>
<!--SizeBasedTriggeringPolicy size="10 MB"/-->
<SizeBasedTriggeringPolicy size="1 KB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
The OnStartupTriggeringPolicy is firing, as you can see from this file:
LastWriteTime Length Name
------------------ ------ ---------
1/7/2022 9:49 AM 0 aps-A.log
Any ideas about how to fix this? Thanks.
Try to change the STDOUT target to your rolling file appender
<Console name="STDOUT" target="**APS-FILE**">
<PatternLayout>
<Pattern>"** %d{DATE} %5p %c{1}:%L - %m%n **"</Pattern>
</PatternLayout>
</Console>

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 Logs not writing to file when running as jar

I have configured log4j2.xml within eclipse and all logs write correctly to a file.
When I export maven project as a jar and run from command promt the logs are displayed on the console instead of writing to a file.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="file_all" fileName="C:/log/logsALL.log" immediateFlush="true" append="true">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="ERROR">
<AppenderRef ref="file_all"/>
</Root>
<Logger name="com.api.main" level="INFO">
<AppenderRef ref="file_all"/>
</Logger>
</Loggers>
</Configuration>
When log4j starts up, it shows internal logging on the console (because status=trace in the configuration).
This internal log should show the location of the config file that is being used. Double-check that this is the correct location: I suspect that an old config file is being loaded that logs to the console...

How to configure port in Apache Chainsaw with 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/

Resources