<level value="info" /> to show debug statement in log file - java

I am using log4j to manage logs in my java application.
<?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="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<param name="Threshold" value="debug" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c{1}:%L %m %n" />
</layout>
</appender>
<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="INFO" />
<param name="Threshold" value="DEBUG" />
<param name="maxFileSize" value="10MB" />
<param name="maxBackupIndex" value="10" />
<param name="file" value="C:/test_reports/infoToolsLog.log"/>
<param name="append" value="true" />
<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="info" />
<appender-ref ref="fileAppender" />
</root>
</log4j:configuration>
Test Code:
import org.apache.log4j.Logger;
public class TestLogFile {
private static final Logger LOGGER = Logger.getLogger(TestLogFile.class);
public static void main(String args[]){
System.out.println("In TestLogFIle");
LOGGER.info("in TestLogFile, info statement");
LOGGER.debug("in TestLogFile debug statement");
LOGGER.error("in TestLogfile, error statement");
int i=10;
try {
int j=i/0;
}catch(Exception e){
LOGGER.error("error occured in TestLogfile : " ,e);
}
}
}
messages displayed in log file :
2017-06-16 11:34:57 INFO TestLogFile:10 in TestLogFile, info statement
2017-06-16 11:34:57 ERROR TestLogFile:12 in TestLogfile, error statement
2017-06-16 11:34:57 ERROR TestLogFile:18 error occured in TestLogfile :
java.lang.ArithmeticException: / by zero...
When <level value="info" />, how to show the debug statements also included in log file.
What changes need to be done to to show info,error and debug statements printed in the log file when <level value="info" />

See the different types of log levels here:
https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Level.html
This image can help you understand it better:
You have to use DEBUG to show FATAL, ERROR, WARN, INFO and DEBUG
<level value="DEBUG" />
Similarly you have to use TRACE to show FATAL, ERROR, WARN, INFO, DEBUG and TRACE
<level value="TRACE" />

Change
<level value="info" />
to
<level value="debug" />
If you set the level to info then you filter out messages that are lower (trace and debug). Since you want to see the debug messages, you need to set the level to debug.

Related

Log4j level not working for the root logger

Okay, this is driving me nuts. I have an app that uses log4j 1.2.16, my configuration file is properly picked up and log4j writes to the file that I want, BUT it only logs error level.
Code:
import org.apache.log4j.Logger;
- (... code omitted)
private static final Logger LOGGER = Logger.getLogger(PostLoginController.class);
LOGGER.debug("TOTO debug");
LOGGER.info("TOTO Info");
LOGGER.warn("TOTO warn");
LOGGER.error("TOTO error");
XML:
<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/jboss/jboss-eap-6.4/standalone/log/myLogFile.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p [%t:%c] : %m%n"/>
</layout>
</appender>
<!-- Root Logger -->
<root>
<priority value ="debug" />
<appender-ref ref="fileAppender"/>
</root>
When my app runs myLogFile is created and I can see the error log in it, but not the other ones:
2019-06-06 19:37:31 ERROR [http-/0.0.0.0:8080-2:my.classpath.PostLoginController] : TOTO error
Any idea what's wrong here?
You have to use like this for logging debug to error.
<log4j:configuration debug="false">
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<param name="target" value="System.out" />
<param name="threshold" value="debug" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] - %m%n" />
</layout>
</appender>
<appender name="fileAppender" class="org.apache.log4j.FileAppender">
<param name="file" value="/jboss/jboss-eap-6.4/standalone/log/myLogFile.log"/>
<param name="append" value="false" />
<param name="threshold" value="debug" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] - %m%n" />
</layout>
</appender>
<root>
<priority value="info" />
<appender-ref ref="default.console" />
<appender-ref ref="default.file" />
</root>
</log4j:configuration>
Okay, figured it out. I had to add
<category name="my.classpath">
<priority value="all" />
<appender-ref ref="fileAppender" />
</category>
because somehow the rootLogger level can't be altered.

i was create two log files using RollingFileAppender in log4j.xml

I want to create two log file.
Because my project has two modules and log each module in separate log files.
After that ,I have to log each module logging data independently .i have Single Log4j file.when i use appender -ref inside the root then i can achive but at that time aop or java logging cant appear in that file only manualy logger msg shown there
Try Something like :
<log4j:configuration>
<appender name="fileAppender1" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="ALL" />
<param name="MaxFileSize" value="3KB" />
<param name="MaxBackupIndex" value="10" />
<param name="File" value="F:/logs/Testing/Project_moduleOne.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM-dd-yyyy HH:mm:ss:SSS} %-5p %m%n" />
</layout>
</appender>
<appender name="fileAppender2" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="ALL" />
<param name="MaxFileSize" value="3KB" />
<param name="MaxBackupIndex" value="10" />
<param name="File" value="F:/logs/PAD_Testing/Project_moduleTwo.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM-dd-yyyy HH:mm:ss:SSS} %-5p %m%n" />
</layout>
</appender>
<!--sets the priority log level for org.springframework -->
<logger name="com.sp.logger1" additivity="false">
<appender ref="fileAppender1"/>
</logger>
<logger name="com.sp.logger2" additivity="false">
<appender ref="fileAppender2" />
</logger>
<!--sets the default priority log level -->
<root>
<priority value="all"></priority>
<appender-ref ref="fileAppender1" />
<appender-ref ref="fileAppender2" />
</root>
</log4j:configuration>

Stop Hibernate SQL logs coming on catalina.out

I was making a log4j.xml for one of my projects in netbeans .
Here is its contents :
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="${catalina.home}/logs/MyLogs.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<param name="Threshold" value="INFO" />
<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="sqlLogs" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="${catalina.home}/logs/MySqlLogs.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<param name="Threshold" value="TRACE" />
<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 ="INFO" />
<appender-ref ref="FILE" />
</root>
<logger name="org.hibernate.SQL" additivity="false" >
<level value="DEBUG" />
<appender-ref ref="sqlLogs" />
</logger>
</log4j:configuration>
Now i want sql logs to go only to MySqlLogs.log but they are appearing on both MySqlLogs.log and catalina.out .
The format of logs in catalina.out is
Hibernate : select ....
Hibernate : insert into ....
Can anyone help me out on this please .
You can stop it, on your env shared properties.
hibernate.show_sql=true
you have to add the following line in your hibernate_cfg.xml file
show_sql to false
and in your file loging.properties :
log4j.logger.org.hibernate=debug
log4j.logger.org.hibernate.type=trace
log4j.logger.org.apache=debug

how to change to debug mode in java

I'm trying to use logger in java jvm. but I can't change the logger mode.
I can see only "info" message and I want to see also "debug" message.
I tried to change to debug mode in log4j.xml in this way:
``<appender name="DISCOVERY" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/tmp/ncs-logs/jvm-discovery-app.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10000KB"/>
<param name="MaxBackupIndex" value="100"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c{2}]:%L %m%n"/>
</layout>
</appender>
<!-- value="<%-4p> %d{dd-MMM-yyyy::HH:mm:ss.SSS} %C{1} %t: - %m%n" -->
<logger name="com.lte.btscompact.mngmt.sync.app">
<level value="DEBUG" />
<appender-ref ref="DISCOVERY"/>
</logger>
<root>
<appender-ref ref="DISCOVERY" />
</root>
</log4j:configuration>
and in cli I did this command:
set java-vm java-logging logger com.tailf.packages.ned.acs level level-all
but I still can't see debug message.
what else should I do?
Double-check your appender config - it must not have a threshold higher than DEBUG:
<param name="Threshold" value="INFO"/>
versus
<param name="Threshold" value="DEBUG"/>

How to conveniently store common logs and client specific logs

I have a huge code base which serves info requests about colleges. There is a base framework code, and we have some separate packages for college1, college2, etc.,
Whenever there is a request for some info about college X, both framework code and college specific code both are triggered. I want to see logs generated for all requests for Company X to go to one location, and company Y to go to one location.
How do I use JUL or apache's logging.properties file to achieve this ? How do I ensure logs written by common framework libraries for company X go to one location, and logs written by common framework libraries for company Y go to some other location ?
I would have an xml file that contain the appenders to specify what packages go into what log file.
You can then load the xml file in your application entry point. Below is an example I have used in my current project
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<param name="threshold" value="error" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%t] %p %c - %m%n" />
</layout>
</appender>
<appender name="codebaseXAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="codebaseX.log" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%t] %d %p %c - %m%n"/>
</layout>
</appender>
<appender name="codebaseYAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="codebaseY.log" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%t] %d %p %c - %m%n"/>
</layout>
</appender>
<!-- Root Logger -->
<root>
<priority value="error" />
<appender-ref ref="console" />
</root>
<!-- Application logger -->
<logger name="com.codebasePackageX" additivity="true">
<level value="info" />
<appender-ref ref="codebaseXAppender" />
</logger>
<!-- Application logger -->
<logger name="com.codebasePackageY" additivity="true">
<level value="info" />
<appender-ref ref="codebaseYAppender" />
</logger>
</log4j:configuration>

Categories