What is logging====================logging is essential for debugging and for maintaing our applicationWe must know what i...
http://www.slf4j.org/SLF4j=======       The Simple Logging Facade for Java or (SLF4J) serves as       a simple facade or a...
WARN                                             ERROR                                             FATAL                  ...
private static final Logger logger=LoggerFactory.getLogger(Applications.class);        public static void main(String[] ar...
log4j.appender.file.MaxBackupIndex=1log4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.appender.file.layout.Co...
Upcoming SlideShare
Loading in...5
×

Java Logging discussion Log4j,Slf4j

2,212

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,212
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Java Logging discussion Log4j,Slf4j

  1. 1. What is logging====================logging is essential for debugging and for maintaing our applicationWe must know what is going in our application, specially when error comeSOP 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 fileLogging framewrok?----------------------Log 4jlog backCommons loggingjava.util.loggingmost commonly used one is log4j---------------------------------------we should not fix ourself with any one specific logging framework as wehave to change as required....go for facade ...use Simple Logging Facade for Java --------------------------Rajeev Gupta Logging rgupta.trainer@gmail.com
  2. 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 INFORajeev Gupta Logging rgupta.trainer@gmail.com
  3. 3. WARN ERROR FATAL OFF----------->Log nothingStarting log4j---------------------steps:1.Download log4j http://www.apache.org/dyn/closer.cgi/logging/log4j/1.2.17/log4j-1.2.17.zip2. Downlod SLF4j http://www.slf4j.org/3. put 3 jar files in classpath4. 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 optionlog4j.rootLogger=INFO, stdout# Direct log messages to stdoutlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%nHello World application--------------------------package com.demo;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class Applications {Rajeev Gupta Logging rgupta.trainer@gmail.com
  4. 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 optionlog4j.rootLogger=INFO, file# Direct log messages to a log filelog4j.appender.file=org.apache.log4j.RollingFileAppenderlog4j.appender.file.File=C:loging.loglog4j.appender.file.MaxFileSize=1MBRajeev Gupta Logging rgupta.trainer@gmail.com
  5. 5. log4j.appender.file.MaxBackupIndex=1log4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%nRajeev Gupta Logging rgupta.trainer@gmail.com

×