Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Java Logging discussion Log4j,Slf4j


Published on

Published in: Education
  • Be the first to comment

  • Be the first to like this

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
  2. 2. 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
  3. 3. WARN ERROR FATAL OFF----------->Log nothingStarting log4j---------------------steps:1.Download log4j Downlod SLF4j 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 ------------------ create a file ---------------------------------# 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
  4. 4. private static final Logger logger=LoggerFactory.getLogger(Applications.class); public static void main(String[] args) { System.out.println("Hello world logging");"stating logging!!!!"); System.out.println("Hello world logging");"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) {"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
  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