KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > util > SimpleLogWriter


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// Copyright (C) 1997-@year@ by Per Nyfelt. All rights reserved.
5
//
6
// $Id: LogWriter.java,v 1.5 2002/12/29 11:15:58 per_nyfelt Exp $
7
package org.ozoneDB.util;
8
9 import java.io.Serializable JavaDoc;
10
11 /**
12  * Provides simple console logging
13  * Usage: LogWriter logWriter = SimpleLogWriter.getInstance().setDebugLevel(LogWriter.DEBUG);
14  * @author Per Nyfelt
15  */

16 public class SimpleLogWriter implements LogWriter, Serializable JavaDoc {
17
18     private int debugLevel;
19     private static SimpleLogWriter instance;
20
21
22     public synchronized static SimpleLogWriter getInstance(){
23         if(instance == null){
24             instance = new SimpleLogWriter();
25         }
26         return instance;
27     }
28
29     public SimpleLogWriter setDebugLevel(int debugLevel) {
30         this.debugLevel = debugLevel;
31         return this;
32     }
33
34     private SimpleLogWriter() {
35
36     }
37
38     /**
39      * This method allows to quickly find out if there is any log target that
40      * would receive entries of the specified level.
41      */

42     public boolean hasTarget(int level) {
43         return level <= debugLevel;
44     }
45
46     public void newEntry(Object JavaDoc sender, String JavaDoc msg, int level) {
47         if (hasTarget(level)) {
48             System.out.println("[" + sender.getClass().getName() + "] " + msg);
49         }
50     }
51
52     public void newEntry(Object JavaDoc sender, String JavaDoc msg, Throwable JavaDoc e, int level) {
53         if (hasTarget(level)) {
54             System.out.println("[" + sender.getClass().getName() + "] "
55                     + msg
56                     + ", "
57                     + e.toString());
58         }
59     }
60 }
61
Popular Tags