KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > util > Logger


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.admin.util;
25
26 /**
27     Provides wrapper to direct logging messages to some kind of output.
28     
29  */

30 public class Logger
31 {
32     private final static int LOG_BASE = 13;
33     // caution: this are assumed to be in order
34
public final static int LOG_OFF = LOG_BASE;
35     public final static int LOG_ERRORS = LOG_BASE + 1;
36     public final static int LOG_DEBUG = LOG_BASE + 2;
37     
38     static private Logger mInstance = new Logger();
39     
40     
41     static protected int sLogLevel = LOG_DEBUG;
42     
43     
44     Logger()
45     {
46     }
47     
48         protected void
49     _log( String JavaDoc msg )
50     {
51         System.out.println( msg );
52     }
53     
54     
55     //--------------------------------------------------------
56

57     /**
58         set the log level; return the old log level
59      */

60         public static int
61     setLogLevel( int logLevel )
62     {
63         int oldLevel = getLogLevel();
64         
65         Assert.assertit( logLevel != LOG_OFF &&
66                         logLevel != LOG_ERRORS &&
67                         logLevel != LOG_DEBUG, "illegal log level" );
68                         
69         sLogLevel = logLevel;
70             
71         return( oldLevel );
72     }
73     
74         Logger
75     getInstance()
76     {
77         return( mInstance );
78     }
79     
80         public static int
81     getLogLevel()
82     {
83         return( sLogLevel );
84     }
85     
86     
87     
88     /**
89         OK, but log( object ) preferred if it avoids
90         generating a String from an object prior to the call.
91     */

92         public static void
93     log( String JavaDoc msg )
94     {
95         if ( sLogLevel >= LOG_DEBUG )
96         {
97             System.out.println( msg );
98         }
99     }
100     
101     /**
102         optimization for the caller; caller can pass an object
103         directly so that if logging is off, we won't ever call
104         toString()
105     */

106         public static void
107     log( Object JavaDoc object )
108     {
109         if ( sLogLevel >= LOG_DEBUG && object != null )
110         {
111             log( object.toString() );
112         }
113     }
114     
115         public static void
116     logError( String JavaDoc msg )
117     {
118         if ( sLogLevel >= LOG_ERRORS && msg != null )
119         {
120             log( msg );
121         }
122     }
123     
124         public static void
125     logFatal( String JavaDoc msg )
126     {
127         // always log
128
log( msg );
129     }
130 };
131
132
Popular Tags