KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > util > Log


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: Log.java,v 1.9 2007/01/07 06:14:00 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.util;
23
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.logging.LogManager JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31
32 /**
33  * Class responsible for instantiating of the system logger. This way anybody
34  * can use this default logger to output their log messages without worrying
35  * what logger to use.
36  *
37  * @version $Id: Log.java,v 1.9 2007/01/07 06:14:00 bastafidli Exp $
38  * @author Miro Halas
39  * @code.reviewer Miro Halas
40  * @code.reviewed 1.5 2006/04/05 05:05:38 bastafidli
41  */

42 public class Log
43 {
44    // Constants ////////////////////////////////////////////////////////////////
45

46    /**
47     * Name of the default logger. Name it by the root package.
48     */

49    public static final String JavaDoc DEFAULT_LOGGER_NAME = "org.opensubsystems.defaultlog";
50    
51    /**
52     * Default file name of configuration file.
53     */

54    public static final String JavaDoc DEFAULT_LOGCONFIG_FILE_NAME = "osslog.properties";
55
56    // Attributes ///////////////////////////////////////////////////////////////
57

58    /**
59     * Default Java logger to log messages to the application.
60     */

61    protected static transient Logger JavaDoc s_lgLogger = null;
62    
63    /**
64     * Helper mutex to synchronize some methods.
65     */

66    protected static transient String JavaDoc s_strMutex = "log.mutex";
67
68    /**
69     * Name of the configuration file actually used to configure the logger.
70     */

71    protected static String JavaDoc s_strConfigFile;
72    
73    /**
74     * Logger for this class. It will be initialized only after the log
75     * configuration is initialized.
76     */

77    protected static Logger JavaDoc s_logger;
78    
79    // Constructors /////////////////////////////////////////////////////////////
80

81    /**
82     * Static initializer
83     */

84    static
85    {
86       InputStream JavaDoc isConfigFile = null;
87       
88       try
89       {
90          // At first, try to use config file from command line definition
91
String JavaDoc strLogfilename = "";
92          
93          strLogfilename = System.getProperty("java.util.logging.config.file");
94          try
95          {
96             // In case you are wondering while property name below was chosen
97
// look at JavaDoc for java.util.logging.LogManager class which
98
// defines this property as a standard property to specify logging
99
// configuration
100
if ((strLogfilename != null) && (strLogfilename.length() > 0))
101             {
102                isConfigFile = new FileInputStream JavaDoc(strLogfilename);
103                s_strConfigFile = strLogfilename;
104             }
105          }
106          catch (FileNotFoundException JavaDoc fnfExc1)
107          {
108             // Try to search for it, this will also setup s_strConfigFile
109
isConfigFile = findConfigFile(strLogfilename);
110             
111             if (isConfigFile == null)
112             {
113                // We have to use system.out since the logger is not initialized yet
114
System.out.println("File " + strLogfilename
115                   + " set in java.util.logging.config.file doesn't exist");
116             }
117          }
118          if (isConfigFile == null)
119          {
120             try
121             {
122                // Open the default file
123
isConfigFile = new FileInputStream JavaDoc(DEFAULT_LOGCONFIG_FILE_NAME);
124                s_strConfigFile = DEFAULT_LOGCONFIG_FILE_NAME;
125             }
126             catch (FileNotFoundException JavaDoc fnfExc2)
127             {
128                // Try to search for it, this will also setup s_strConfigFile
129
isConfigFile = findConfigFile(DEFAULT_LOGCONFIG_FILE_NAME);
130
131                if (isConfigFile == null)
132                {
133                   // We have to use system.out since the logger is not initialized yet
134
System.out.println("Default log configuration file "
135                                      + DEFAULT_LOGCONFIG_FILE_NAME
136                                      + " doesn't exist");
137                }
138             }
139          }
140          if (isConfigFile != null)
141          {
142             LogManager.getLogManager().readConfiguration(isConfigFile);
143          }
144       }
145       catch (IOException JavaDoc ioeExc)
146       {
147          // Logger is not initialized yet, use System.out.
148
System.out.println("Cannot initialize logging subsystem.");
149          ioeExc.printStackTrace();
150       }
151       finally
152       {
153          try
154          {
155             if (isConfigFile != null)
156             {
157                isConfigFile.close();
158             }
159          }
160          catch (IOException JavaDoc ioeExc)
161          {
162             // Ignore this
163
}
164          finally
165          {
166             s_logger = Log.getInstance(Log.class);
167             // Print this as info because otherwise we wouldn't know where to
168
// change it
169
s_logger.info("Using log configuration file " + s_strConfigFile);
170          }
171       }
172    }
173    
174    /**
175     * Hidden constructor to disable creation of instances of this class.
176     */

177    protected Log(
178    )
179    {
180    }
181    
182    // Accessors ////////////////////////////////////////////////////////////////
183

184    /**
185     * Get instance of configured logger which can be used to log messages from
186     * the application.
187     *
188     * @return Logger - instance of logger ready to use
189     * @deprecated
190     */

191    public static Logger JavaDoc getInstance(
192    )
193    {
194       if (s_lgLogger == null)
195       {
196          synchronized (s_strMutex)
197          {
198             // We may do any aditional configuration here but for now we are fine
199
// with the default configuration read from file specified by property
200
// java.util.logging.config.file as specified in documentation for
201
// LogManager
202
s_lgLogger = getInstance(DEFAULT_LOGGER_NAME);
203          }
204       }
205
206       return s_lgLogger;
207    }
208    
209    /**
210     * Get instance of configured logger which can be used to log messages from
211     * the application.
212     *
213     * @param classIdentifier - identifier of class to get the logger for
214     * @return Logger - instance of logger ready to use
215     */

216    public static Logger JavaDoc getInstance(
217       Class JavaDoc classIdentifier
218    )
219    {
220       return getInstance(classIdentifier.getName());
221    }
222
223    /**
224     * Get instance of configured logger which can be used to log messages from
225     * the application.
226     *
227     * @param logIdentifier - identifier of log to get
228     * @return Logger - instance of logger ready to use
229     */

230    public static Logger JavaDoc getInstance(
231       String JavaDoc logIdentifier
232    )
233    {
234       // We may do any aditional configuration here but for now we are fine
235
// with the default configuration read from file specified by property
236
// java.util.logging.config.file as specified in documentation for
237
// LogManager
238
return Logger.getLogger(logIdentifier);
239    }
240
241    /**
242     * Find and configuration file.
243     *
244     * @param strConfigFileName - name of the configuration file
245     * @return InputStream - opened config file
246     * @throws IOException - an error has occured opening configuration file
247     * @throws FileNotFoundException - file cannot be found
248     */

249    protected static InputStream JavaDoc findConfigFile(
250       String JavaDoc strConfigFileName
251    ) throws IOException JavaDoc,
252             FileNotFoundException JavaDoc
253    {
254       InputStream JavaDoc isConfigFile = null;
255       URL JavaDoc urlDefaultPropertyFile;
256
257       urlDefaultPropertyFile = ClassLoader.getSystemResource(strConfigFileName);
258       if (urlDefaultPropertyFile == null)
259       {
260          // if there was not found configuration file by using
261
// ClassLoader.getSystemResource(), try to use
262
// getClass().getClassLoader().getResource()
263
// This is here due to the fact that in J2EE server
264
// ClassLoader.getSystemResource() search the configuration file
265
// OUTSIDE of the packaged application (ear, war, jar) so that this
266
// file can override the configuration file which is packaged INSIDE
267
// the application which will be found belog with class loader
268
// for this class
269
urlDefaultPropertyFile = Log.class.getClassLoader().getResource(
270                                      strConfigFileName);
271       }
272       if (urlDefaultPropertyFile == null)
273       {
274          throw new FileNotFoundException JavaDoc("Cannot find configuration file "
275                                          + strConfigFileName);
276       }
277       else
278       {
279          isConfigFile = urlDefaultPropertyFile.openStream();
280          s_strConfigFile = urlDefaultPropertyFile.toString();
281       }
282       
283       return isConfigFile;
284    }
285 }
286
287
Popular Tags