1. What is logging
====================
logging is essential for debugging and for maintaing our application
We must know what is going in our application, specially when error come
SOP and printing exception message is not good?
Writing system.out.println(“…..”);
-------------------------------------------
Should not be used for debugging messages, as it is very hard to remove those unnessary
Sop once coding is done …
It may produce serious problem in production enveronment…headach for admin peoples
Real advantage of logging is that it can be enable/disable and
debugging messages can be directed to the file
Logging framewrok?
----------------------
Log 4j
log back
Commons logging
java.util.logging
most commonly used one is log4j
---------------------------------------
we should not fix ourself with any one specific logging framework as we
have to change as required....
go for facade ...use Simple Logging Facade for Java
--------------------------
Rajeev Gupta Logging rgupta.trainer@gmail.com
2. http://www.slf4j.org/
SLF4j
=======
The Simple Logging Facade for Java or (SLF4J) serves as
a simple facade or abstraction for various logging frameworks,
e.g. java.util.logging, log4j and logback,
allowing the end user to plug in the desired
logging framework at deployment time
Levels of logging
----------------
ALL----------->log everything
DEBUG
INFO
Rajeev Gupta Logging rgupta.trainer@gmail.com
3. WARN
ERROR
FATAL
OFF----------->Log nothing
Starting log4j
---------------------
steps:
1.Download log4j
http://www.apache.org/dyn/closer.cgi/logging/log4j/1.2.17/log4j-1.2.17.zip
2. Downlod SLF4j
http://www.slf4j.org/
3. put 3 jar files in classpath
4. Now we need to configure the logging ie to tell what are levels of
logging and where to log?
greate code on MKYong.com
------------------
http://www.mkyong.com/logging/log4j-log4j-properties-examples/
create a file log4j.properties
---------------------------------
# Root logger option
log4j.rootLogger=INFO, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
Hello World application
--------------------------
package com.demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Applications {
Rajeev Gupta Logging rgupta.trainer@gmail.com
4. private static final Logger logger=LoggerFactory.getLogger(Applications.class);
public static void main(String[] args) {
System.out.println("Hello world logging");
logger.info("stating logging!!!!");
System.out.println("Hello world logging");
logger.info("finished logging!!!!");
}
}
example 2
----------------
package com.demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Application2 {
private static final Logger logger=LoggerFactory.getLogger(Applications.class);
public static void main(String[] args) {
logger.info("start logging");
String no="4x";
try
{
Integer.parseInt(no);
}
catch(NumberFormatException ex)
{
logger.error("connot formet :"+no+" to and no....");
}
}
}
now try Output to an file
-------------------------
# Root logger option
log4j.rootLogger=INFO, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:loging.log
log4j.appender.file.MaxFileSize=1MB
Rajeev Gupta Logging rgupta.trainer@gmail.com