log4J empty file - java

i have configured log4j appender to create log file every hour. The logged file should not be created if there is nothing to log.
Everything works correctly, but when I start jBoss the log file is created even if there is nothing to log.
Appender settings.
<appender name="ordersLogFile" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="Append" value="true"/>
<param name="Threshold" value="INFO"/>
<rollingPolicy name="ordersLogFile" class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern" value="${jboss.server.log.dir}/Portal_%d{yyyy_MM_dd-HH}-00-00.log" />
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="'%d{yyyyMMddHHmmss,SSS}';%m%n"/>
</layout>
</appender>
Is it possible to turn off creation of log file during jboss start?

Related

Write specific logs to a log file spring-boot/slf4j

I am able to write console logs to rotating log files using this method here:
https://www.codejava.net/frameworks/spring-boot/logback-rolling-files-example
But my requirement is to write only specific logs to the log files instead of whole console log. For example, i need to write only the json messages(i can print them on console using log.info(message) which is supported by slf4j) being received by Kafka listener to the log files and nothing more. How can i achieve this in my spring-boot app? Is this not possible with slf4j logback?
Specify the appender-ref in your logger element.
So something like this in your logger XML configuration file:
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
</layout>
</appender>
<appender name="bdfile" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false"/>
<param name="maxFileSize" value="1GB"/>
<param name="maxBackupIndex" value="2"/>
<param name="file" value="/tmp/bd.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
</layout>
</appender>
Then in the same config file, add a appender reference to the log file that you are interested in
<logger name="com.example.ClassWithJsonMessage" additivity="false">
<level value="debug"/>
<appender-ref ref="bdfile"/>
</logger>

log4j SMTPAppender, how to email only specific errors

I've configured an SMTPAppender in my JBoss server.
<appender name="SMTP" class="org.apache.log4j.net.SMTPAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="Threshold" value="ERROR"/>
<param name="To" value="steve.bucciacchio#muhc.mcgill.ca"/>
<param name="From" value="jboss.wnetvmap47#muhc.mcgill.ca"/>
<param name="Subject" value="PQDSN Sever Error on WNETVMAP47"/>
<param name="SMTPHost" value="smtpout.muhc.mcgill.ca"/>
<param name="BufferSize" value="512"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ABSOLUTE},%c{1}] %m%n"/>
</layout>
</appender>
It works, I get emails every time an error is logged in JBoss's server.log file. The problem is that I get an email for every single error that is logged, even if the error was a JBoss error, has nothing to do with my application. Is there a way to select or filter what errors are emailed? Also can I email only errors that are logged to my application's log file?

How to solve Log4J No Such Property warning/error?

I have a Log4J xml config file with appenders in it.
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<param name="File" value="/tomcat/website/webapps/app/logs/appInfo.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM dd HH:mm:ss} %F: %m%n" />
</layout>
</appender>
<appender name="rolling" class="org.apache.log4j.RollingFileAppender">
<param nawebsite/webapps/app/logs/app.log" />
<param name="MaxFileSize" value="1000KB" />
<param name="MaxBackupIndex" value="2" />
<param name="DatePattern" value="'.'yyyy-MM-dd'" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM dd HH:mm:ss} %F: %m%n" />
</layout>
</appender>
When I build my project, I get the following warnings/errors in the console which I am trying to get rid of:
log4j:WARN No such property [datePattern] in org.apache.log4j.RollingFileAppender.
log4j:WARN No such property [file] in org.apache.log4j.ConsoleAppender.
I did not make these log4j config xml files; they were already part of the project.
I've seen the DatePattern documentation online, and it seems to suggest exactly what is in the DatePattern value, so I have no idea why it's still showing me that warning. Same with file.
Did the person who made this before me just stick properties in these appenders that do not belong? I'm having a difficult time verifying this.
Every Appender has it's own configuration properties. file make sense for RollingFileAppender but doesn't make sense for ConsoleAppender.
If you switch one appender to another and do not update configuration properties, then you will get such warnings.
To fix them, you need to remove unused (or inapplicable) properties from your configuration:
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM dd HH:mm:ss} %F: %m%n" />
</layout>
</appender>
<appender name="rolling" class="org.apache.log4j.RollingFileAppender">
<param name="FileName" value="/tomcat/website/webapps/app/logs/app.log" />
<param name="MaxFileSize" value="1000KB" />
<param name="MaxBackupIndex" value="2" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM dd HH:mm:ss} %F: %m%n" />
</layout>
</appender>
Complete list of appenders and their parameters is available on the Apache Commons Log4j webpage:
https://logging.apache.org/log4j/2.x/manual/appenders.html

How to use syslog with LOG4J 1.x?

I want to config my LOG4J. I can't update the version to 2.x because it's a big application. The actual Documentation for the LOG4J version 1.x is offline.
Here is my actual config for the appender:
<appender name="syslog" class="org.apache.log4j.net.SyslogAppender">
<param name="SyslogHost" value="localhost:514"/>
<param name="Facility" value="LOCAL1"/>
<param name="FacilityPrinting" value="true"/>
<param name="Threshold" value="WARN"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd.MMM.yyyy HH:mm:ss} %-5p %m%n"/>
</layout>
</appender>
I tested it with : netstat -a -b > test.txt
But i dont't found something with the Port 514.
Can somebody help me(because he made experience with LOG4J 1.x), or has somebody a copy of the documentation from LOG4J 1.x ?
<appender name="Syslog" class="org.apache.log4j.net.SyslogAppender">
<param name="SyslogHost" value="IP:514"/>
<param name="Facility" value="USER"/>
<param name="FacilityPrinting" value="true"/>
<param name="Threshold" value="INFO"/>
<layout class="org.apache.log4j.IvyLog4jLayout">
<param name="DateFormat" value="MM/dd/yyyy hh:mm:ss"/>
</layout>
</appender>
This way it worked for me :)

Why is log4j logging to the console?

This is a standalone java application.
I am using the configuration file below and having two problems.
1) I'm getting logs to stdout and I don't know why.
2) I'm getting ALL log messages in my error log even though I have tried to direct only error and higher to the error log.
I am using the BasicConfigurator without specifying an explicit path to the log4j.xml file. The xml file is in the same jar as my classes. It is creating and writing to the appropriate logs in addition to these problems so the configuration is being applied.
3) In addition, I have had no luck having the log4j.xml file outside of the jar so I can change it at runtime. How do I do that?
<!--appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="DEBUG"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p [%F:%L] - %m%n"/>
</layout>
</appender-->
<!-- working dir is $CATALINA_TMPDIR. send logs to log dir. -->
<appender name="ROLL" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/var/log/company/application.log"/>
<param name="MaxFileSize" value="5MB"/>
<param name="MaxBackupIndex" value="9"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %x - %m%n"/>
</layout>
</appender>
<appender name="ERRORLOG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/var/log/rocketvox/company/error.log"/>
<param name="MaxFileSize" value="5MB"/>
<param name="MaxBackupIndex" value="9"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %x - %m%n"/>
</layout>
</appender>
<category name="com.company">
<priority value="ALL"/>
<appender-ref ref="ROLL"/>
</category>
<category name="com.mattx">
<priority value="ALL"/>
<appender-ref ref="ROLL"/>
</category>
<root>
<priority value="error"/>
<appender-ref ref="ERRORLOG"/>
</root>
Add -Dlog4j.debug to the application's JVM args to see exactly what log4j is doing and which configuration file it uses.
Your problem is using BasicConfigurer - for configuring using a file named log4j.xml, you don't need to use any explicit log4j Configurer in your application code, as the default log4j initialization logic will kick in to pick log4j.xml (or log4j.properties, if no xml is found) from the classpath.
See Default Initialization Procedure from the log4j manual.

Categories

Resources