I am working on a Spring-MVC application in which I am using Jetty as our application server. I want to debug log a specific java class, but even after enabling log4j or slf4j, and adding files it's not working.
Finally, I created a war file, added to start log4j with this command java -jar start.jar --add-to-start=logging-log4j, which created a log4j.xml file in resources directory. In that log4j.xml, I added the class I want to debug, but no [DEBUG] entries are getting added.
Enabled modules :
log4j2-slf4j.mod
slf4j-log4j2.mod
slf4j-api.mod
logging-slf4j.mod
I have also tried their combinations and different loggings as documentation suggested.
log4j.xml present in resources directory of both IDE and Jetty.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
debug="false">
<!-- console 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="%-5p %c{1} - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="INFO" />
<param name="AcceptOnMatch" value="true" />
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter"/>
</appender>
<logger name="com.zaxxer.hikari" additivity="false">
<level value="DEBUG" />
<appender-ref ref="console" />
</logger>
<root>
<priority value="ERROR" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
What am I doing wrong?
It seems that the problem is that you have only bridges and not log4j itself in the classpath.
As requested a simple logback.xml config file:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<logger name="test" level="DEBUG" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Related
I have an standalone java application with following config 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="file" class="org.apache.log4j.RollingFileAppender">
<param name="maxFileSize" value="1MB" />
<param name="maxBackupIndex" value="1" />
<param name="File"
value=".\\myComp.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd.MM.yy HH\:mm\:ss.SSS} %5p %c{1}:%L - %m%n " />
</layout>
</appender>
<category name="com.mycomp.project.starter">
<priority value="${project.client.log.level.starter}" />
</category>
<category name="com.security">
<priority value="${project.client.log.level.security}" />
</category>
<root>
<level value="ERROR" />
<appender-ref ref="file" />
</root>
</log4j:configuration>
When I start the Application I can set the log level through a ini file which can contain:
-Dproject.client.log.level.starter=INFO
-Dproject.client.log.level.security=DEBUG
What I would like to archive is, if the -Dproject.client.log.level.security=DEBUG is not set it should use ERROR.
How could I achieve this? I appreciate any help.
Okay, I got a solution, unfortunately it involved switching to log4j2.
I found out that log4j allows Property Substitution allows to use system properties with prefix sys:. In combination with this finding (where I can set default values with a simple -), I changed my config to:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<RollingFile name="file" fileName=".\\myComp.log" append="true" filePattern=".\\myComp-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout pattern="%d{dd.MM.yy HH:mm:ss.SSS} %5p %c{1}:%L - %m%n "/>
<Policies>
<SizeBasedTriggeringPolicy size="1 MB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<!-- setting log level per default to INFO if not set through system properties -->
<Logger name="com.myComp.starter" level="${sys:project.client.log.level.starter:-INFO}" />
<!-- setting log level per default to ERROR if not set through system properties -->
<Logger name="com.security" level="${sys:project.client.log.level.security:-ERROR}" />
<Root level="ERROR">
<AppenderRef ref="file"/>
</Root>
</Loggers>
</Configuration>
Now I am able to set it within a ini file or leave it out if not needed and still have a defined log level.
Docx4j gives me a bunch of messages like this
[AWT-EventQueue-0] INFO org.docx4j.model.listnumbering.Emulator -
How to turn that off?
The following log configuration gets loaded, but doesnt turns the logging off.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<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>
<root>
<priority value ="OFF" />
</root>
</log4j:configuration>
Add the following line to your configuration:
<logger name="org.docx4j.model.listnumbering.Emulator">
<level value="ERROR" />
<appender-ref ref="console"/>
</logger>
This will configure log4j to log only ERROR messages originating from logger org.docx4j.model.listnumbering.Emulator. If you want to turn off INFO messages from all classes/packages under org.docx4j, use the following:
<logger name="org.docx4j" >
<level value="ERROR" />
<appender-ref ref="console"/>
</logger>
More information here : http://www.javabeat.net/baisc-steps-to-configure-log4j-using-xml-and-properties-file/
I'm using docx4j 8.3.2, logged by slf4j 1.7.30 + logback 1.2.3.
The annoying log messages disappeared after adding a logback.xml under src/main/resources. The logback.xml content:
<configuration>
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder
by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} %line - %msg%n
</pattern>
</encoder>
</appender>
<!-- docx4j loggers -->
<logger name="org.docx4j" level="ERROR" />
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>
I don't have a much knowledge in log4j and slf4j. But I want to understand how the actual logger is working in the below case.
I have a Web module,Service module and a Dao module.Web module has a dependency to Service and Service has a dependency to Dao .
In my web module there are 3 jars. slf4j jar, log4j jar and slf4j-log4j12 jar and log4j.xml is as below.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
<!-- ============================== -->
<!-- Append messages to the console -->
<!-- ============================== -->
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="Target" value="System.out"/>
<param name="Threshold" value="INFO"/>
<layout class="org.apache.log4j.PatternLayout">
<!-- The default pattern: Date Priority [Category] Message\n -->
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="C:\\temp\\web.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%t] %d{HH:mm:ss,SSS} %-5p %l - %m%n" />
</layout>
</appender>
<!-- ======================= -->
<!-- Setup the Root category -->
<!-- ======================= -->
<root>
<!--
Set the root logger priority via a system property. Note this is parsed by log4j
-->
<level value="trace" />
<appender-ref ref="CONSOLE"/>
</root>
<logger name="com.mywork">
<level value="DEBUG" />
<appender-ref ref="FILE" />
</logger>
<logger name="org.hibernate">
<level value="ERROR" />
<appender-ref ref="FILE" />
</logger>
<logger name="org">
<level value="ERROR" />
<appender-ref ref="FILE" />
</logger>
</log4j:configuration>
And In my service module and dao module contain only slf4j jar. And logging details are in file.(logging details of all modules)
Should the loggers of hibernate and Spring also available in my file ?
How the exception occur are available in logger file?
Thank you
You explicitly constrain org.* with ERROR level.
This effectively eliminates almost all output of hibernate and spring loggers.
But exceptions logged with error level should still get to file log.
Module dependencies configured in IDE are irrelevant. Logging happens in JBoss container and depends only on actual application's runtime classpath.
BTW, I highly recommend against trace level on root category.
See Production settings file for log4j? for the explanation.
I have the following two packages:
com.mycorp.project.first
com.mycorp.project.second
I'd like to configure Log4J (SLF4J) to write the logs from one package to one file, and from the other package to a second file. I do not want them to be mixed in together.
To be clear, this is one project/one process running.
I've tried filtering and with loggers but they seem to be ignored by log4j. Both files are always identical.
Edit: Thank you for the answers so far, this is what I've got and it's not working. Both output files are identical.
<configuration debug="true">
<contextName>dev</contextName>
<appender name="FIRST_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>A:/dev/LogTesting/logs/first.log</file>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>A:/dev/LogTesting/logs/first.%d{yyyyMMdd}%d{_HHmmss,aux}.log.gz</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{MMM dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="SECOND_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>A:/dev/LogTesting/logs/second.log</file>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>A:/dev/LogTesting/logs/second.%d{yyyyMMdd}%d{_HHmmss,aux}.log.gz</fileNamePattern>
</rollingPolicy>
<encoder>
<Pattern>%d{MMM dd HH:mm:ss.SSS} %property{HOSTNAME} [%thread] %level %logger{36} %msg%n</Pattern>
</encoder>
</appender>
<!-- =============================================================== -->
<logger name="com.test.apples" additivity="false">
<level value="DEBUG" />
<appender-ref ref="FIRST_FILE" />
</logger>
<logger name="com.test.oranges" additivity="false">
<level value="DEBUG" />
<appender-ref ref="SECOND_FILE" />
</logger>
<!-- =============================================================== -->
<category name="com.test.apples" additivity="false">
<priority value="DEBUG"/>
<appender-ref ref="FIRST_FILE"/>
</category>
<category name="com.test.oranges" additivity="false">
<priority value="DEBUG"/>
<appender-ref ref="SECOND_FILE"/>
</category>
<!-- =============================================================== -->
<!-- default -->
<root level="WARN">
<appender-ref ref="FIRST_FILE" />
<appender-ref ref="SECOND_FILE" />
</root>
Try this is the example of properties file:
log4j.rootLogger=DEBUG, CONSOLE
# Each package has different appender name
log4j.logger.com.mycorp.project.first=DEBUG, FIRST
log4j.logger.com.mycorp.project.second=DEBUG, SECOND
log4j.appender.FIRST=org.apache.log4j.RollingFileAppender
log4j.appender.FIRST.File=./first.log
log4j.appender.FIRST.layout=org.apache.log4j.PatternLayout
log4j.appender.SECOND=org.apache.log4j.RollingFileAppender
log4j.appender.SECOND.File=./second.log
log4j.appender.SECOND.layout=org.apache.log4j.PatternLayout
or this for XML (only highlight important part):
<!-- FIRST appender definition -->
<appender name="FIRST" class="org.apache.log4j.FileAppender">
...
</appender>
<!-- SECOND appender definition -->
<appender name="SECOND" class="org.apache.log4j.FileAppender">
...
</appender>
<!-- logger package com.mycorp.project.first -->
<logger name="com.mycorp.project.first">
<level value="DEBUG" />
<appender-ref ref="FIRST"/>
</logger>
<!-- logger for package com.mycorp.project.second -->
<logger name="com.mycorp.project.second">
<level value="DEBUG" />
<appender-ref ref="SECOND"/>
</logger>
Create two appenders LOG1 and LOG2, one for each log file. After that define two categories(one for each package) with reference to these appenders int the log4j config file.
<category name="com.mycorp.project.first" additivity="true">
<priority value="TRACE"/>
<appender-ref ref="LOG1"/>
</category>
Appender2
<category name="com.mycorp.project.second" additivity="true">
<priority value="TRACE"/>
<appender-ref ref="LOG2"/>
</category>
put dynamiclly file path .in log4j.properties
log4j.appender.FILE.File=${file.name}
And in java set file path System.setProperty("file.name", FILEPATH);
you just specify 2 appenders, but give same output log file name for both.
#same appender for both packages
log4j.logger.package1=INFO,MyCommonLogger
log4j.logger.package2=INFO,MyCommonLogger
log4j.appender.MyCommonLogger=org.apache.log4j.DailyRollingFileAppender
log4j.appender.MyCommonLogger.layout=org.apache.log4j.PatternLayout
log4j.appender.MyCommonLogger.layout.ConversionPattern=%t %d{MM/dd/yy HH:mm:ss} %x %-5p %c %M - %m%n
#single log file for logs from both packages
log4j.appender.MyCommonLogger.File=/somewhereonserver/log/mysinglelogfile.log
I'm trying to configure log4j in an Eclipse plugin project using the following XML property file, that includes a custom appender called EclipseLoggingAppender:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
<appender name="eclipseErrorView" class="com.lior.ibd.utils.logging.EclipseLoggingAppender"/>
<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>
<root>
<priority value ="debug" />
<appender-ref ref="console" />
</root>
<logger name="com.lior">
<level value ="warn" />
<appender-ref ref="eclipseErrorView" />
</logger>
</log4j:configuration>
I pass this property file to the following statement in the code:
DOMConfigurator.configure(filename);
But when loading the application I get the following error message:
log4j:ERROR Could not create an Appender. Reported error follows.
java.lang.ClassNotFoundException: com.lior.ibd.utils.logging.EclipseLoggingAppender
Anyone knows what's the deal? could be a classpath issue?..
Yes, this is a classpath issue. Log4j is looking for class com.lior.ibd.utils.logging.EclipseLoggingAppender.
(probably appender that wrote someone in your organisation?)
If you remove lines:
<appender name="eclipseErrorView" class="com.lior.ibd.utils.logging.EclipseLoggingAppender"/>
and
<logger name="com.lior">
<level value ="warn" />
<appender-ref ref="eclipseErrorView" />
</logger>
log4j should handle it.
Or add EclipseLoggingAppender to classpath by locating a appropriate jar file and add it to the classpath. I.e. run
java -cp appender.jar com.mypackage.MyClass
for starters you can only have one <root> element. You want something more like
<appender name="eclipseErrorView" class="com.mypackage.EclipseLoggingAppender">
<filter class="org.apache.log4j.varia.LevelRangeFilter Source code of org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="WARN" />
</filter>
</appender>
<root>
<priority value ="debug" />
<appender-ref ref="console" />
<appender-ref ref="eclipseErrorView" />
</root>
How have you added your custom logger to the classpath?