I am getting two console warnings:
log4j:WARN No appenders could be found for logger (org.roussev.selenium4j.MainTestCase).
log4j:WARN Please initialize the log4j system properly.
Following is my log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="appender" class="org.apache.log4j.FileAppender">
<param name="File" value="selenium4j.log"/>
<param name="Append" value="false"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %p - %m%n"/>
</layout>
</appender>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %p - %m%n"/>
</layout>
</appender>
<root>
<priority value ="DEBUG"/>
<appender-ref ref="appender"/>
<appender-ref ref="console"/>
</root>
</log4j:configuration>
I am not sure why am I getting this error. The xml seems to be fine to me.
Most common reason for this undescriptive message is ::
The log4j.xml is not properly added in your classpath.
See more here ::: https://stackoverflow.com/a/6616858/1085285
Related
My requirement is little weird.
I want different formatting pattern for different Logging levels that's why I have used this code. But I am unable to set the min and max limit. Because when I am setting logging level to Debug, it is also printing the ERROR and FATAL log messages in that format but I have defined a different format for ERROR and FATAL and because of this ERROR messages are printing two times for this code. I do not want to print ERROR and FATAL log messages in DEBUG pattern format.
log4j.rootLogger=DEBUG, infoLog, errorLog
log4j.appender.infoLog=org.apache.log4j.ConsoleAppender
log4j.appender.infoLog.Target=System.out
log4j.appender.infoLog.layout=org.apache.log4j.PatternLayout
log4j.appender.infoLog.layout.ConversionPattern=%m%n
#log4j.appender.infoLog.Threshold=DEBUG
log4j.appender.infoLog.filter.a=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.infoLog.filter.a.LevelToMatch=INFO
log4j.appender.infoLog.filter.a.AcceptOnMatch=true
log4j.appender.infoLog.filter.a.LevelMin=DEBUG
log4j.appender.infoLog.filter.a.LevelMax=INFO
#log4j.appender.infoLog.filter.b=org.apache.log4j.varia.LevelMatchFilter
#log4j.appender.infoLog.filter.b.LevelToMatch=DEBUG
#log4j.appender.infoLog.filter.b.AcceptOnMatch=true
log4j.appender.errorLog.Threshold=ERROR
log4j.appender.errorLog=org.apache.log4j.ConsoleAppender
log4j.appender.errorLog.Target=System.out
log4j.appender.errorLog.layout=org.apache.log4j.PatternLayout
log4j.appender.errorLog.layout.ConversionPattern=\u001b[31;1m%d{yyyy-MM-dd HH:mm:ss} [%-5p] %c{1}:%M():%L - %m%n
This xml Configuration will resolve this problem.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="debugLog" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="DEBUG"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG"/>
<param name="LevelMax" value="INFO"/>
</filter>
</appender>
<appender name="errorLog" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="WARN"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%-5p] %c{1}:%M():%L - %m%n"/>
</layout>
</appender>
<root>
<level value="DEBUG"></level>
<appender-ref ref="debugLog"/>
<appender-ref ref="errorLog"/>
</root>
</log4j:configuration>
I want to redirect log to specific log file, by classname. The intention is to have a group of classes log output to a specific log file.
Right now the all output comes to console, and on startup it throws and error saying log4j:ERROR Could not retrieve category [com.mycomp]. Reported error follows..
What am I doing wrong here?
In my java class :-
private static final Logger log = Logger.getLogger(SpringBootLog4jApplication.class.getName());
My log4j.xml :-
<?xml version="1.0" encoding="UTF-8" ?>
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/' debug="false">
<appender name="LOGFILE" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="/git/services.log"/>
<param name="maxBackupIndex" value="10"/>
<param name="maxFileSize" value="20MB"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %x %p %c{1} :: %m\n"/>
</layout>
</appender>
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<!-- Pattern to output the caller's file name and line number -->
<param name="ConversionPattern" value="%5p [%t] (%F:%L) - %m%n"/>
</layout>
</appender>
<logger name="com.mycomp" class="com.mycomp.SpringBootLog4jApplication" additivity="false">
<level value="INFO"/>
<appender-ref ref="LOGFILE"/>
</logger>
<root>
<level value="INFO"/>
<appender-ref ref="stdout"/>
</root>
I believe that the name for the logger should look like
<logger name="com.mycomp.*"...
as that would resolve to any concrete classes in the package.
I have a java web application that runs with tomcat7 and logs with log4j. I want to send my logs to rsyslog.
I have use the appeneder in my log4j.xml file.
<appender name="syslog" class="org.apache.log4j.net.SyslogAppender">
<param name="Facility" value="USER"/>
<param name="SyslogHost" value="localhost"/>
<param name="Threshold" value="WARN"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM dd HH:mm:ss} MYAPP: %-5p %m%n"/>
</layout>
</appender>
But I still don't see the logs that I've implemented in my java application in rsyslog. I can see them in catalina.out but not on rsyslog.
Do I have to define new rules for rsyslog configuration?
Thanks in advance.
Updated 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="System.out" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="Target" value="System.out" />
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<appender name="SYSLOG" class="org.apache.log4j.net.SyslogAppender">
<param name="SyslogHost" value="localhost"/>
<param name="Facility" value="LOCAL1"/>
<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>
<!-- for package specific -->
<!-- <category name ="my.package" /> -->
<logger name ="Logger">
<level value ="debug" />
</logger>
<!-- root logger -->
<root>
<level value="info"/>
<appender-ref ref="SYSLOG"/>
</root>
I have Log 4j configured to roll over daily at mid night.
<?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="appender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/data/log/engine.log"/>
<param name="Append" value="true"/>
<param name="datePattern" value="'.'MM-dd-yyyy" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %p - %m%n"/>
</layout>
</appender>
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<!-- <param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/> -->
<!-- <param name="ConversionPattern" value="%d [%t] %p (%M-%L)- %m%n"/> -->
<param name="ConversionPattern" value="%d [%t] %%-5p %d %l - %m%n"/>
</layout>
</appender>
<root>
<priority value ="debug"/>
<appender-ref ref="appender"/>
<appender-ref ref="STDOUT"/>
</root>
</log4j:configuration>
During midnight setfile is called .But logs are rolling in current log as well as previous day log.Why it is still rolling in previous day log?
I see a bunch of sample log4j configurations that contain xmlns:log4j="http://jakarta.apache.org/log4j/" in the <log4j:configuration> tag, is this attribute required? What does having this attribute in my configuration do for me?
Example:
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="infoLogsFile" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="MyApplication.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
<root>
<priority value ="DEBUG" />
<appender-ref ref="infoLogsFile"/>
</root>
</log4j:configuration>
Could I do this instead?
<log4j:configuration>
<appender name="infoLogsFile" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="MyApplication.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
<root>
<priority value ="DEBUG" />
<appender-ref ref="infoLogsFile"/>
</root>
</log4j:configuration>
The xmlns attribute specifies that elements prepended with log4j are defined by the schema referenced by that URL. Log4J does not actually perform schema validation before attempting to parse the configuration file, so it is not actually required.