A simple wrapper around JDK logging facilities : Log « Language Basics « Java






A simple wrapper around JDK logging facilities

   
/***
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *    Project: www.simpledbm.org
 *    Author : Dibyendu Majumdar
 *    Email  : dibyendu@mazumdar.demon.co.uk
 */

import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.LogManager;

/**
 * A simple wrapper around JDK logging facilities. The aim is to allow 
 * easy switch to another logging system, such as Log4J.
 * 
 * @author Dibyendu Majumdar
 */
public final class Logger {
  
  /**
   * Instance of the real logger object.
   */
  private final java.util.logging.Logger realLogger;
  
  /**
   * Obtain a new or existing Logger instance. 
   * @param name Name of the logger, package names are recommended
   */
  public static Logger getLogger(String name) {
    return new Logger(name);
  }
  
  public static void configure(String filename) {
    FileInputStream is = null; 
    try {
      is = new FileInputStream(filename);
      LogManager.getLogManager().readConfiguration(is);
    }
    catch (Exception e) {
      System.err.println("SIMPLEDBM-ERROR: Failed to initialize logging system due to following error: " + e.getMessage());
      e.printStackTrace();
    }
    finally {
      try {
        is.close();
      } catch (IOException e) {}
    }
  }
  
  public Logger(String name) {
    realLogger = java.util.logging.Logger.getLogger(name);
  }
  
  public void info(String sourceClass, String sourceMethod, String message) {
    realLogger.logp(Level.INFO, sourceClass, sourceMethod, message);
  }
  
  public void info(String sourceClass, String sourceMethod, String message, Throwable thrown) {
    realLogger.logp(Level.INFO, sourceClass, sourceMethod, message, thrown);
  }
  
  public void debug(String sourceClass, String sourceMethod, String message) {
    realLogger.logp(Level.FINE, sourceClass, sourceMethod, message);
  }
  
  public void debug(String sourceClass, String sourceMethod, String message, Throwable thrown) {
    realLogger.logp(Level.FINE, sourceClass, sourceMethod, message, thrown);
  }

  public void trace(String sourceClass, String sourceMethod, String message) {
    realLogger.logp(Level.FINER, sourceClass, sourceMethod, message);
  }
  
  public void trace(String sourceClass, String sourceMethod, String message, Throwable thrown) {
    realLogger.logp(Level.FINER, sourceClass, sourceMethod, message, thrown);
  }
  
  public void warn(String sourceClass, String sourceMethod, String message) {
    realLogger.logp(Level.WARNING, sourceClass, sourceMethod, message);
  }
  
  public void warn(String sourceClass, String sourceMethod, String message, Throwable thrown) {
    realLogger.logp(Level.WARNING, sourceClass, sourceMethod, message, thrown);
  }

  public void error(String sourceClass, String sourceMethod, String message) {
    realLogger.logp(Level.SEVERE, sourceClass, sourceMethod, message);
  }

  public void error(String sourceClass, String sourceMethod, String message, Throwable thrown) {
    realLogger.logp(Level.SEVERE, sourceClass, sourceMethod, message, thrown);
  }
  
  public boolean isTraceEnabled() {
    return realLogger.isLoggable(Level.FINER);
  }

  public boolean isDebugEnabled() {
    return realLogger.isLoggable(Level.FINE);
  }
  
  public void enableDebug() {
    realLogger.setLevel(Level.FINE);
  }

  public void disableDebug() {
    realLogger.setLevel(Level.INFO);
  }

}

   
    
    
  








Related examples in the same category

1.Log levelLog level
2.Simple Log Formatter ExampleSimple Log Formatter Example
3.Log to file with FileHandler and SimpleFomatterLog to file with FileHandler and SimpleFomatter
4.Log multiple HandlersLog multiple Handlers
5.Log multiple Handlers 2Log multiple Handlers 2
6.Override LogRecord toString()Override LogRecord toString()
7.Email LoggerEmail Logger
8.Log To File with FileHandlerLog To File with FileHandler
9.Logging LevelsLogging Levels
10.Creating a Custom Log Level
11.Logging Level ManipulationLogging Level Manipulation
12.Configure LoggingConfigure Logging
13.How to write custom Log handlerHow to write custom Log handler
14.Log Client Filter
15.Log HTML Table Formatter
16.Logging Example 1Logging Example 1
17.Basic Logging Example
18.Java Log:Basic Logging Java Log:Basic Logging
19.Java log: Hierarchy loggingJava log: Hierarchy logging
20.Java log: various log methodsJava log: various log methods
21.Java log: Stream Handler DemoJava log: Stream Handler Demo
22.Java log: File Handler DemoJava log: File Handler Demo
23.Java log: Memory Handler DemoJava log: Memory Handler Demo
24.Java log: Socket Handler Demo
25.Java log: Basic logging 2Java log: Basic logging 2
26.Java log: Logging Server
27.Java log: log filterJava log: log filter
28.Java log: XML log
29.Java log: alternate XML logJava log: alternate XML log
30.Java log: Localize LoggingJava log: Localize Logging
31.Java log: Custom XML FormatterJava log: Custom XML Formatter
32.Java log: Remote Config Reader
33.Java log: Log and Window(JFrame, frame)Java log: Log and Window(JFrame, frame)
34.File Logger
35.Setting the Formatter of a Logger Handler
36.Memory Handler Demo
37.Socket Handler Demo
38.XMLFormatter based Logging
39.#define the properties for the SocketHandler
40.Custom filter
41.Using Regular Expressions based on StreamHandler
42.The Quintessential Logging Program
43.Determining If a Message Will Be Logged
44.Logging a Method Call
45.Logging an Exception
46.Minimizing the Impact of Logging Code
47.Preventing a Logger from Forwarding Log Records to Its Parent
48.Writing Log Records to a Log File
49.A file handler that appends.
50.Writing Log Records to Standard Error
51.Writing Log Records Only After a Condition Occurs
52.Create a memory handler with a memory of 100 records and dumps the records into the file my.log
53.Setting a Filter on a Logger Handler
54.Comparing Log Levels: To compare the severity of two logging levels, use Level.intValue().
55.Creating a Custom Formatter for a Logger Handler
56.Limiting the Size of a Log File
57.Limiting the Size of a Log by Using a Rotating Sequence of Files
58.Configuring Logger Default Values with a Properties File
59.Determining When the Logging Configuration Properties are Reread
60.Handling Errors While Parsing an XML File
61.An example of a program providing the functionality of logging
62.Use Logger with simple formatter and FileHandler
63.Logger with XMLFormatter and FileHandler
64.Stream Handler
65.The Patterns in FileHandler
66.Flush File Handler and Logger
67.Config Demo
68.config.properties
69.Alternate XML by using FileHandler
70.Localized Logging
71.Define your own Custom Formatter
72.Remote ConfigReader with URLConnection
73.Window Handler: display log message in a window(JFrame)
74.Create log with package nameCreate log with package name
75.Logger Demo
76.Return a message for logging.
77.A modification of the image viewer program that logs various events
78.A class helper for logging service.
79.Logging Thread Pool
80.Logging Thread