Home  >  Core Java

Example of Logger in Java

By Arvind Rai, April 13, 2013
A Logger in java is used to log the message in different level. Level can be
config : logs configuration message.
entering: logs a message when control enters in a method.
existing: logs the message when control is exiting from the method.
fine: logs fine message
finer: logs finer message.
finest: logs finest message.
info: logs information.
severe: logs severe message.
LoggerTest.java
package com.concretepage;
import java.io.IOException;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Logger;
import java.util.logging.XMLFormatter;
public class LoggerTest {
    public static void main(String[] args) throws SecurityException, IOException {
        
        Logger logger = Logger.getLogger("conlog");
        
        Handler handler = new ConsoleHandler();
        
        handler.setFormatter(new XMLFormatter());
        
        logger.addHandler(handler);
        
        logger.info("logger info message");
                
        logger.severe("logger severe message");
      }
}
Output
<?xml version="1.0" encoding="windows-1252" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
  <date>2013-04-10T18:04:53</date>
  <millis>1365597293900</millis>
  <sequence>0</sequence>
  <logger>conlog</logger>
  <level>INFO</level>
  <class>com.concretepage.LoggerTest</class>
  <method>main</method>
  <thread>10</thread>
  <message>logger info message</message>
</record>

<record>
  <date>2013-04-10T18:04:53</date>
  <millis>1365597293915</millis>
  <sequence>1</sequence>
  <logger>conlog</logger>
  <level>SEVERE</level>
  <class>com.concretepage.LoggerTest</class>
  <method>main</method>
  <thread>10</thread>
  <message>logger severe message</message>
</record> 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us