new line is not coming in log4j - log4j

I have added logging in my project but i have two issues.First is that new lines are not coming between various log enteries. And second is that I am not able to change the log location to relative path also. Here is my log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<File name="A1" fileName="C:\log\A1.log" append="false">
<PatternLayout pattern="%t %-5p %c{2} - %m%n"/>
</File>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="org.apache.log4j.xml" level="debug">
<AppenderRef ref="A1"/>
</Logger>
<Root level="debug">
<AppenderRef ref="A1"/>
</Root>
</Loggers>
</Configuration>

Your configuration looks correct: the pattern for both appenders ends in %n, which should make every message appear on separate lines. If you want an empty line between two log entries, you can try using a pattern that ends in %n%n.
If your output looks like all messages are concatenated without newlines, like this:
main INFO my.class - message1main INFO my.class - message2main INFO my.class - message3
Then it is most likely that your application is actually using a different configuration than the one shown in your question. Could it be that one of your jars contains an old log4j2.xml config file?

Related

Is there a way to change the default log level of Hazelcast?

I am trying to start the hazelcast using the default hazelcast.xml and the start script provide by hazelcast. I am setting the logging type to log4j2 in the hazelcast.xml. I am seeing this message in the logs.
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'log4j2.debug' to show Log4j2 internal initialization logging.
I couldn't figure out the way to change the default log level. Could anyone help me on how to set the default log level?
This works for me,
export JAVA_OPTS="-Dhazelcast.logging.type=log4j2 -Dlog4j.configurationFile=./log4j2.xml"
And then have a file named log4j2.xml in the current folder containing
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="ConsoleRed" target="SYSTEM_OUT">
<PatternLayout pattern="%red{%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n}"/>
</Console>
<Console name="ConsoleWhite" target="SYSTEM_OUT">
<PatternLayout pattern="%white{%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n}"/>
</Console>
<Console name="ConsoleYellow" target="SYSTEM_OUT">
<PatternLayout pattern="%yellow{%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n}"/>
</Console>
</Appenders>
<Loggers>
<Logger name="com.hazelcast" level="info" additivity="false">
<AppenderRef ref="ConsoleYellow"/>
</Logger>
<Logger name="com.hazelcast.core" level="info" additivity="false">
<AppenderRef ref="ConsoleRed"/>
</Logger>
<Root level="error">
<AppenderRef ref="ConsoleWhite"/>
</Root>
</Loggers>
</Configuration>
If you have colours on, you'll see different colours for the different types of messages.

How do I correct this error while appending to a log file using log4j2?

Error report says:
2016-06-17 09:37:14,122 main ERROR Error processing element Appender ([Appenders: null]): CLASS_NOT_FOUND
2016-06-17 09:37:14,168 main ERROR Unable to locate appender "Console" for logger config "root"
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Appender type="File" name="Console" fileName="C:\Users\raghi\Documents\NetBeansProjects\mdepth\JavaApplication3\build\classes\oo.txt">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Appender>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
<Root level="info">
<appender-ref ref="Console"/>
</Root>
<Root level="trace">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
It looks like we can't append this way to the log file. There is something wrong here. Please help me out on this instead of any downvotes.
You are using a mix of the strict syntax and non-strict syntax. You also have 3 root loggers but you can only have one. Please review http://logging.apache.org/log4j/2.x/manual/configuration.html

Incorporating ESAPI log4j requirements

I have a simple XML format log4j config file. I am trying to incorporate ESAPI log4j requirement of adding their factory.
How do I reconcile this format (I am guessing strict XML format) to simple XML format that I am using below. If I just add this line in, ESAPI just dies with the exception:
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.spi.LoggerFactory
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

Why isn't my Log4J code working with the FlumeAppender?

I'm getting the following error with Log4J :
2015-07-07 18:24:00,974 ERROR Error processing element Flume: CLASS_NOT_FOUND
2015-07-07 18:24:01,009 ERROR Appender AuditLogger cannot be located. Route igno
red
The following is my Log4J2 XML file :
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="MyFile" fileName="OutputLogFile.log" immediateFlush="false" append="false">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<Flume name="AuditLogger" compress="true">
<Agent host="192.168.10.101" port="8800"/>
<Agent host="192.168.10.102" port="8800"/>
<RFC5424Layout enterpriseNumber="18060" includeMDC="true" appName="MyApp"/>
</Flume>
<Routing name="Routing">
<Routes pattern="$${sd:type}">
<Route>
<RollingFile name="Rolling-${sd:type}" fileName="${sd:type}.log"
filePattern="${sd:type}.%i.log.gz">
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
<SizeBasedTriggeringPolicy size="100" />
</RollingFile>
</Route>
<Route ref="AuditLogger" key="Audit"/>
</Routes>
</Routing>
</Appenders>
<Loggers>
<Root level="all">
<Appender-Ref ref="Console"/>
<Appender-Ref ref="MyFile"/> <!-- added_in now -->
</Root>
</Loggers>
</Configuration>
Then , I tried to add in FlumeAppender like this:
import org.apache.logging.log4j.flume;
But it didn't work after that ... How to set-up FlumeAppender ?
<RollingFile name="MESSAGING_FILE" fileName="log/messaging.log"
filePattern="log/MM_messaging.log.%i">
<PatternLayout pattern="%m%n/>
<Policies>
<SizeBasedTriggeringPolicy size="1MB"/>
</Policies>
<DefaultRolloverStrategy max="3"/>
<Filters>
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
</RollingFile>
Here is an example that creates a new file every time the file size becomes 1 MB, you can set the size to standard sizes such as 10 kb 345 MB etc.
The %i in the file pattern must be included for doing the size based rollovers. The max variable determines the number of files it will keep. Over time you will lose the oldest logs.
Lastly the Threshold filter logs only INFO messages and Below ie. (INFO,WARN,ERROR...) to get an exact match set the onMatch to Neutral and then add another Threshold filter like this
<Filters>
<ThresholdFilter level="INFO" onMatch="NEUTRAL" onMismatch="DENY"/>
<ThresholdFilter level="EROR" onMatch="DENY" onMismatch="ACCEPT"/>
</Filters>
They get processed in order. Neutral just means move on to the next filter. Accept means it will be immediately logged. If the message gets passed through all the filters with neutral status it will be default accepted just as if there were no filters.
As far as seperating logs by output the most straightforward way is to choose the appenders class path's log to by defining the logger for that path and adding the appender ref to the log you want. Another simple way is to use a marker and markerfilter. Lastly i believe there is also the routingappender which is probably more efficient but more complicated and i don't have experience using it.
Spend some time on the developer website for log4j2 they have a relatively good api reference. It will help you with most everything you want to do in a new implementation.

log4j logging to console but not File blazeDS

I am seeing strange behavior using log4j that I am completely stumped on. This is my first time using it with BlazeDS and I am hoping it is a small issue I have missed.
In this web app, I am using log4j for logging from 2 java classes, as well as blazeDS. I am hooking BlazeDS into commons-logging via org.springframework.flex.core.CommonsLoggingTarget, and then using log4j as the underlying logger.
I have set appenders for both "Console" and "MyFile" under the blazeds logger, and while the console output is working as expected, nothing is written to MyFile. The file is in fact created, but stays at 0KB. On the other hand, my two java classes are logging just fine both to the console and their rolling file. I am seeing this behavior both when I test locally and also when I deploy to Tomcat.
One last detail - when testing locally, I changed the MyFile path to an absolute path, and saw that the file was beign appended to. This would lead me to believe it is an issue with the relative path however it has been working fine for my java logging.
commons-logging.properties:
org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
log4j.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="trace" debug="true">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<RollingFile name="RollingFile" fileName="logs/ImpactTradeQuery.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="25 MB"/>
</Policies>
<DefaultRolloverStrategy max="25"/>
</RollingFile>
<File name="MyFile" immediateFlush="true" fileName="logs/app.log">
<PatternLayout>
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</PatternLayout>
</File>
</appenders>
<loggers>
<root level="trace">
<appender-ref ref="Console"/>
</root>
<logger name="flex.samples.trade.tradeService" level="trace" additivity="False">
<appender-ref ref="RollingFile"/>
<appender-ref ref="Console"/>
</logger>
<logger name="flex.samples.ConnectionHelper" level="trace" additivity="False">
<appender-ref ref="RollingFile"/>
<appender-ref ref="Console"/>
</logger>
<logger name="blazeds" level="trace" additivity="True">
<appender-ref ref="MyFile"/>
<appender-ref ref="Console"/>
</logger>
</loggers>
</configuration>
services-config.xml:
<logging>
<target class="flex.messaging.log.ConsoleTarget" level="Error">
<properties>
<prefix>[BlazeDS] </prefix>
<includeDate>false</includeDate>
<includeTime>false</includeTime>
<includeLevel>false</includeLevel>
<includeCategory>false</includeCategory>
</properties>
<filters>
<pattern>Endpoint.*</pattern>
<pattern>Service.*</pattern>
<pattern>Configuration</pattern>
</filters>
</target>
<target class="org.springframework.flex.core.CommonsLoggingTarget" level="All">
<properties>
<categoryPrefix>blazeds</categoryPrefix>
</properties>
<filters>
<pattern>Endpoint.*</pattern>
<pattern>Service.*</pattern>
<pattern>Configuration</pattern>
</filters>
</target>
</logging>
Turned out to be a missing jar file in Tomcat (log4j-jcl) that was causing the issue. Once the library was properly packaged with the app the logging worked as expected.

Resources