I am working with jax-rs and jboss 7.1 and it happens that when I put log4j.xml in src/main/resources and I start the application apparently does not recognize the configuration in the file in log4j.xml because it does not create the logs file in D:/logs/learning.log.
my log4j file looks like this:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n"/>
</layout>
</appender>
<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false"/>
<param name="file" value="D:/logs/learning.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="consoleAppender"/>
<appender-ref ref="fileAppender"/>
</root>
</log4j:configuration>
someone could help me solve it please.
Related
I try to make my log file more readable so I want to separate records in my log file by a blank line after every application run. I use log4j.
I understand that the easiest way is to append %m%n to a single record in code. But I'm interested in doing it via configuration file.
So the question is what I have to add to configuration file log4j.xml?
Here is the context of my log4j.xml configuration file:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="true"/>
<param name="maxFileSize" value="10MB"/>
<param name="maxBackupIndex" value="10"/>
<param name="file" value="logs/logs.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="file"/>
</root>
</log4j:configuration>
You could use another Logger defined in your configuration that uses the same settings except its ConversionPattern which will contain only %n. Here is an example:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true">
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="true"/>
<param name="maxFileSize" value="10MB"/>
<param name="maxBackupIndex" value="10"/>
<param name="file" value="logs/logs.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
</layout>
</appender>
<appender name="file2" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="true"/>
<param name="maxFileSize" value="10MB"/>
<param name="maxBackupIndex" value="10"/>
<param name="file" value="logs/logs.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%n"/>
</layout>
</appender>
<logger name="com.company.MyClass">
<level value="DEBUG"/>
<appender-ref ref="file2"/>
</logger>
<root>
<level value="DEBUG"/>
<appender-ref ref="file"/>
</root>
</log4j:configuration>
Then, when your app starts:
Logger logger = Logger.getLogger(com.company.MyClass.class);
logger.info(" ");
I'm not sure to understand what you want to achieve and why, but if you want an empty line between each log entry, you just have to add an extra %n at the end of your ConversionPattern:
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n%n"/>
(%n means "new line")
I have almost tried everything but log4j seems to not compress the log file. I am using 1.2.17 jars for log4j and log4j extras. Cant seem to understand whats wrong . it simply creates a gz file but thats a normal file without compression
log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="file" class="org.apache.log4j.rolling.RollingFileAppender">
<rollingPolicy name="file" class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="ActiveFileName" value="${catalina.home}/logs/broker.log" />
<param name="FileNamePattern" value="=${catalina.home}/logs/broker-.%d{yyyyMMdd-HH}.log.gz" />
</rollingPolicy>
<triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
<param name="MaxFileSize" value="10000"/>
</triggeringPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%"/>
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="file" />
</root>
</log4j:configuration>
Answer to my own question this is how I made it work
#Worked with 2.17 version
log4j.rootLogger=INFO, file
log4j.appender.file=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.file.rollingPolicy=org.apache.log4j.rolling.FixedWindowRollingPolicy
log4j.appender.file.rollingPolicy.maxIndex=5
log4j.appender.file.triggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.file.triggeringPolicy.MaxFileSize=10000
log4j.appender.file.rollingPolicy.FileNamePattern=${catalina.home}/logs/broker-%i.log.gz
log4j.appender.file.rollingPolicy.ActiveFileName=${catalina.home}/logs/broker.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n
I have been trying to change the logging from console to both console as well as file. Looking at the large documentation on log4j I was able to add the appender as shown in the code below. However I don't seem to be getting logs in the specified file.
Any ideas what might be wrong? Here is my log4j.xml (already located at src/)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="DEBUG"/>
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<param name="file" value="/data/MFlogs/log.log"/>
<param name="immediateFlush" value="true"/>
<param name="threshold" value="debug"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<root>
<priority value="DEBUG" />
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
</log4j:configuration>
change file path tag. try it with double slash.
value="//data//MFlogs//log.log"
Try adding full path to path location or add ${catalina.home}/logs/myApp.log to it.
Read this question, it tells how to use relative path. It is easy if you use properties file instead of xml file.
I am able to print debug logs to a file, but I am unable to print info. However, printing info and debug to console works correctly. How do I go about getting info to print in a file as well?
Thanks
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value= "%-5p [%t]: %m%n"/>
</layout>
</appender>
<appender name="R" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="example.log"/>
<param name="MaxFileSize" value="1000000KB"/>
<param name="MaxBackupIndex" value="1"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value= "%-5p [%t]: %m%n"/>
</layout>
</appender>
<root>
<priority value="info" />
<appender-ref ref="stdout" />
<appender-ref ref="R" />
</root>
</log4j:configuration>
So I was using the java logger object rather than the apache4j logger object...
I've tried I think all the info I've found in the Internet, with no results. I want to switch off the Hibernate Logging.
My log4j.properties:
log4j.logger.org.hibernate = INFO
log4j.logger.org.hibernate.SQL=INFO, SQL_APPENDER
log4j.additivity.org.hibernate.SQL=false
log4j.rootLogger=INFO
My log4.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<logger name="org.hibernate">
<level value="info"/>
</logger>
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n"/>
</layout>
</appender>
<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false"/>
<param name="file" value="out/learning.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="consoleAppender"/>
<appender-ref ref="fileAppender"/>
</root>
</log4j:configuration>
Inside my hibernate.cfg.xml:
<property name="hibernate.show_sql">false</property>
<property name="hibernate.generate_statistics">false</property>
<property name="hibernate.use_sql_comments">false</property>
At this moment I don't know what else can I do to stop those disturbing messages on my console.
Thanks in advance for your help.
try to use OFF level for each appender might help
http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Level.html
I would encourage you to read the SLF4J Manual, you should only have 1 Binding jar on your classpath.
Remove slf4-simple-1.4.2.jar from your classpath, it may be stopping log4j being used.
slf4j-simple-1.7.5.jar
Binding for Simple implementation, which outputs all events to System.err. Only messages of level INFO and
higher are printed. This binding may be useful in the context of small
applications.