KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencrx > kernel > log > AuditLog


1 /*
2  * ====================================================================
3  * Project: opencrx, http://www.opencrx.org/
4  * Name: $Id: AuditLog.java,v 1.3 2005/05/11 23:38:30 wfro Exp $
5  * Description: AuditLog
6  * Revision: $Revision: 1.3 $
7  * Owner: CRIXP AG, Switzerland, http://www.crixp.com
8  * Date: $Date: 2005/05/11 23:38:30 $
9  * ====================================================================
10  *
11  * This software is published under the BSD license
12  * as listed below.
13  *
14  * Copyright (c) 2004, CRIXP Corp., Switzerland
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  *
21  * * Redistributions of source code must retain the above copyright
22  * notice, this list of conditions and the following disclaimer.
23  *
24  * * Redistributions in binary form must reproduce the above copyright
25  * notice, this list of conditions and the following disclaimer in
26  * the documentation and/or other materials provided with the
27  * distribution.
28  *
29  * * Neither the name of CRIXP Corp. nor the names of the contributors
30  * to openCRX may be used to endorse or promote products derived
31  * from this software without specific prior written permission
32  *
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
35  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
36  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
39  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
40  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
41  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
43  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46  * POSSIBILITY OF SUCH DAMAGE.
47  *
48  * ------------------
49  *
50  * This product includes software developed by the Apache Software
51  * Foundation (http://www.apache.org/).
52  *
53  * This product includes software developed by contributors to
54  * openMDX (http://www.openmdx.org/)
55  */

56 package org.opencrx.kernel.log;
57
58 import java.util.Properties JavaDoc;
59
60 import org.openmdx.application.Dependencies;
61 import org.openmdx.kernel.exception.VersionMismatchException;
62 import org.openmdx.kernel.log.Config;
63 import org.openmdx.kernel.log.LogLevel;
64 import org.openmdx.kernel.log.SysLog;
65 import org.openmdx.kernel.log.impl.Log;
66
67 public class AuditLog extends Log {
68
69     private AuditLog() {}
70
71     /**
72      * Set the config name that the logs pertain to. Must be called
73      * before doing any logging.
74      *
75      * @param cfgName a config name
76      */

77     public static void setConfigName(
78         String JavaDoc cfgName
79     ) {
80         if (!initialized) {
81             if (cfgName == null) return; // reject null strings
82
cfgName = cfgName.trim();
83             if (cfgName.length()==0) return; // reject empty strings
84
AuditLog.configName = cfgName;
85         }else{
86             SysLog.error("A config name must be set before any logging " +
87                          "takes place", new Exception JavaDoc());
88         }
89     }
90
91     /**
92      * Set a log source object. The logging framework uses the toString() method
93      * to determine the log source for each log event.
94      * The object may be set at any time.
95      *
96      * <p>
97      * Dynamic log source example:
98      * <code>
99      *
100      * class LogSource
101      * {
102      * public String toString()
103      * {
104      * return "LogSource-" + System.currentTimeMillis();
105      * }
106      * }
107      *
108      * LogSource logSource = new LogSource();
109      * AuditLog.setLogSource(logSource)
110      *
111      * </code>
112      *
113      * <p>
114      * Static log source example:
115      * <code>
116      *
117      * AuditLog.setLogSource("LogSource")
118      *
119      * </code>
120      *
121      * @param logSource a log source object
122      */

123     public static void setLogSource(
124         Object JavaDoc logSource
125     ) {
126         if (logSource == null) return;
127         AuditLog.logSource = logSource;
128     }
129
130     /**
131      * Sets the log properties [Config-Level-3]. setLogProperties() must be
132      * called before doing any logging.
133      *
134      * @param properties the log properties
135      */

136     public static void setLogProperties(
137         Properties JavaDoc props
138     ) {
139         // Setting log properties is only possible when the config
140
// object does not exist and nothing has been logged yet.
141
if (!initialized) {
142             AuditLog.logProperties = props;
143         } else{
144             SysLog.error("Log properties must be set before any logging " +
145                          "takes place", new Exception JavaDoc());
146         }
147     }
148
149     /**
150      * Returns the logger's configuration object
151      *
152      * @return the configuration object
153      */

154     public static Config getLogConfig(
155     ) {
156         if (!initialized) init();
157         return new Config(singleton.getConfig());
158     }
159
160     /**
161      * Checks if trace logging is active
162      *
163      * <p>
164      * Applications can use this method before they call AuditLog.trace(...) if
165      * the log string creation is very time consuming.
166      *
167      * <pre>
168      * AuitLog.trace("Customer created", "First=Mark, Last=Smith");
169      *
170      * if (AuditLog.isTraceOn()) {
171      * summary = expensive_creation();
172      * detail = expensive_creation();
173      * AuditLog.trace(summary, detail);
174      * }
175      * </pre>
176      *
177      * @return true if trace is active
178      */

179     public static boolean isTraceOn(
180     ) {
181         if (!initialized) init();
182         return (singleton.getLoggingLevel() >= LogLevel.LOG_LEVEL_TRACE);
183     }
184
185     /**
186      * Logs a text string at CRITICAL_ERROR_LEVEL.
187      *
188      * @param logString
189      * a concise summary message. The message must be single line and
190      * must therefore not contain any '\r' or '\n' characters. The '\r'
191      * and '\n' characters are removed silently from the message.
192      * @param logObj
193      * a log object providing detail information. The log object is
194      * stringified using its <code>toString</code> method before getting
195      * logged. The log object may be a null object. If the log object
196      * is a <code>Throwable</code> it's message and stack trace is
197      * logged.
198      * @see #criticalError(String)
199      * @see #criticalError(String, Object, int)
200      */

201     public static void criticalError(
202         String JavaDoc logString,
203         Object JavaDoc logObj
204     ) {
205         if (!initialized) init();
206
207         if (LogLevel.LOG_LEVEL_CRITICAL_ERROR <= singleton.getLoggingLevel()) {
208             singleton.logString(
209                 AuditLog.logSource,
210                 logString,
211                 logObj,
212                 LogLevel.LOG_LEVEL_CRITICAL_ERROR,
213                 0);
214         }
215     }
216
217     /**
218      * Logs a text string at ERROR_LEVEL.
219      *
220      * @param logString
221      * a concise summary message. The message must be single line and
222      * must therefore not contain any '\r' or '\n' characters. The '\r'
223      * and '\n' characters are removed silently from the message.
224      * @param logObj
225      * a log object providing detail information. The log object is
226      * stringified using its <code>toString</code> method before getting
227      * logged. The log object may be a null object. If the log object
228      * is a <code>Throwable</code> it's message and stack trace is
229      * logged.
230      * @see #error(String)
231      * @see #error(String, Object, int)
232      */

233     public static void error(
234         String JavaDoc logString,
235         Object JavaDoc logObj
236     ) {
237         if (!initialized) init();
238         if (LogLevel.LOG_LEVEL_ERROR <= singleton.getLoggingLevel()) {
239             singleton.logString(
240                 AuditLog.logSource,
241                 logString,
242                 logObj,
243                 LogLevel.LOG_LEVEL_ERROR,
244                 0);
245         }
246     }
247
248     /**
249      * Logs a text string at ERROR_LEVEL.
250      *
251      * @param logString
252      * a concise summary message. The message must be single line and
253      * must therefore not contain any '\r' or '\n' characters. The '\r'
254      * and '\n' characters are removed silently from the message.
255      * @see #error(String, Object)
256      * @see #error(String, Object, int)
257      */

258     public static void error(
259         String JavaDoc logString
260     ) {
261         if (!initialized) init();
262         if (LogLevel.LOG_LEVEL_ERROR <= singleton.getLoggingLevel()) {
263             singleton.logString(
264                 AuditLog.logSource,
265                 logString,
266                 null,
267                 LogLevel.LOG_LEVEL_ERROR,
268                 0);
269         }
270     }
271
272     /**
273      * Logs a text string at ERROR_LEVEL.
274      *
275      * @param logString
276      * a concise summary message. The message must be single line and
277      * must therefore not contain any '\r' or '\n' characters. The '\r'
278      * and '\n' characters are removed silently from the message.
279      * @param logObj
280      * a log object providing detail information. The log object is
281      * stringified using its <code>toString</code> method before getting
282      * logged. The log object may be a null object. If the log object
283      * is a <code>Throwable</code> it's message and stack trace is
284      * logged.
285      * @param callStackOff
286      * a call stack correction offset. The offset must be a positive
287      * number: 0, 1, 2, 3, ...
288      * @see #error(String)
289      * @see #error(String, Object)
290      */

291     public static void error(
292         String JavaDoc logString,
293         Object JavaDoc logObj,
294         int callStackOff
295     ) {
296         if (!initialized) init();
297         if (LogLevel.LOG_LEVEL_ERROR <= singleton.getLoggingLevel()) {
298             singleton.logString(
299                 AuditLog.logSource,
300                 logString,
301                 logObj,
302                 LogLevel.LOG_LEVEL_ERROR,
303                 callStackOff);
304         }
305     }
306
307     /**
308      * Logs a text string at WARNING_LEVEL.
309      *
310      * @param logString
311      * a concise summary message. The message must be single line and
312      * must therefore not contain any '\r' or '\n' characters. The '\r'
313      * and '\n' characters are removed silently from the message.
314      * @param logObj
315      * a log object providing detail information. The log object is
316      * stringified using its <code>toString</code> method before getting
317      * logged. The log object may be a null object. If the log object
318      * is a <code>Throwable</code> it's message and stack trace is
319      * logged.
320      * @see #warning(String)
321      * @see #warning(String, Object, int)
322      */

323     public static void warning(
324         String JavaDoc logString,
325         Object JavaDoc logObj
326     ) {
327         if (!initialized) init();
328         if (LogLevel.LOG_LEVEL_WARNING <= singleton.getLoggingLevel()) {
329             singleton.logString(
330                 AuditLog.logSource,
331                 logString,
332                 logObj,
333                 LogLevel.LOG_LEVEL_WARNING,
334                 0);
335         }
336     }
337
338     /**
339      * Logs a text string at WARNING_LEVEL.
340      *
341      * @param logString
342      * a concise summary message. The message must be single line and
343      * must therefore not contain any '\r' or '\n' characters. The '\r'
344      * and '\n' characters are removed silently from the message.
345      * @see #warning(String, Object)
346      * @see #warning(String, Object, int)
347      */

348     public static void warning(
349         String JavaDoc logString
350     ) {
351         if (!initialized) init();
352         if (LogLevel.LOG_LEVEL_WARNING <= singleton.getLoggingLevel()) {
353             singleton.logString(
354                 AuditLog.logSource,
355                 logString,
356                 null,
357                 LogLevel.LOG_LEVEL_WARNING,
358                 0);
359         }
360     }
361
362     /**
363      * Logs a text string at WARNING_LEVEL.
364      *
365      * @param logString
366      * a concise summary message. The message must be single line and
367      * must therefore not contain any '\r' or '\n' characters. The '\r'
368      * and '\n' characters are removed silently from the message.
369      * @param logObj
370      * a log object providing detail information. The log object is
371      * stringified using its <code>toString</code> method before getting
372      * logged. The log object may be a null object. If the log object
373      * is a <code>Throwable</code> it's message and stack trace is
374      * logged.
375      * @param callStackOff
376      * a call stack correction offset. The offset must be a positive
377      * number: 0, 1, 2, 3, ...
378      * @see #warning(String)
379      * @see #warning(String, Object)
380      */

381     public static void warning(
382         String JavaDoc logString,
383         Object JavaDoc logObj,
384         int callStackOff
385     ) {
386         if (!initialized) init();
387         if (LogLevel.LOG_LEVEL_WARNING <= singleton.getLoggingLevel()) {
388             singleton.logString(
389                 AuditLog.logSource,
390                 logString,
391                 logObj,
392                 LogLevel.LOG_LEVEL_WARNING,
393                 callStackOff);
394         }
395     }
396
397     /**
398      * Logs a text string at INFO_LEVEL.
399      *
400      * @param logString
401      * a concise summary message. The message must be single line and
402      * must therefore not contain any '\r' or '\n' characters. The '\r'
403      * and '\n' characters are removed silently from the message.
404      * @param logObj
405      * a log object providing detail information. The log object is
406      * stringified using its <code>toString</code> method before getting
407      * logged. The log object may be a null object. If the log object
408      * is a <code>Throwable</code> it's message and stack trace is
409      * logged.
410      * @see #info(String)
411      * @see #info(String, Object, int)
412      */

413     public static void info(
414         String JavaDoc logString,
415         Object JavaDoc logObj
416     ) {
417         if (!initialized) init();
418         if (LogLevel.LOG_LEVEL_INFO <= singleton.getLoggingLevel()) {
419             singleton.logString(
420                 AuditLog.logSource,
421                 logString,
422                 logObj,
423                 LogLevel.LOG_LEVEL_INFO,
424                 0);
425         }
426     }
427
428     /**
429      * Logs a text string at INFO_LEVEL.
430      *
431      * @param logString
432      * a concise summary message. The message must be single line and
433      * must therefore not contain any '\r' or '\n' characters. The '\r'
434      * and '\n' characters are removed silently from the message.
435      * @see #info(String, Object)
436      * @see #info(String, Object, int)
437      */

438     public static void info(
439         String JavaDoc logString
440     ) {
441         if (!initialized) init();
442         if (LogLevel.LOG_LEVEL_INFO <= singleton.getLoggingLevel()) {
443             singleton.logString(
444                 AuditLog.logSource,
445                 logString,
446                 null,
447                 LogLevel.LOG_LEVEL_INFO,
448                 0);
449         }
450     }
451
452     /**
453      * Logs a text string at INFO_LEVEL.
454      *
455      * @param logString
456      * a concise summary message. The message must be single line and
457      * must therefore not contain any '\r' or '\n' characters. The '\r'
458      * and '\n' characters are removed silently from the message.
459      * @param logObj
460      * a log object providing detail information. The log object is
461      * stringified using its <code>toString</code> method before getting
462      * logged. The log object may be a null object. If the log object
463      * is a <code>Throwable</code> it's message and stack trace is
464      * logged.
465      * @param callStackOff
466      * a call stack correction offset. The offset must be a positive
467      * number: 0, 1, 2, 3, ...
468      * @see #info(String)
469      * @see #info(String, Object)
470      */

471     public static void info(
472         String JavaDoc logString,
473         Object JavaDoc logObj,
474         int callStackOff
475     ) {
476         if (!initialized) init();
477         if (LogLevel.LOG_LEVEL_INFO <= singleton.getLoggingLevel()) {
478             singleton.logString(
479                 AuditLog.logSource,
480                 logString,
481                 logObj,
482                 LogLevel.LOG_LEVEL_INFO,
483                 callStackOff);
484         }
485     }
486
487     /**
488      * Logs a text string at DETAIL_LEVEL.
489      *
490      * @param logString
491      * a concise summary message. The message must be single line and
492      * must therefore not contain any '\r' or '\n' characters. The '\r'
493      * and '\n' characters are removed silently from the message.
494      * @param logObj
495      * a log object providing detail information. The log object is
496      * stringified using its <code>toString</code> method before getting
497      * logged. The log object may be a null object. If the log object
498      * is a <code>Throwable</code> it's message and stack trace is
499      * logged.
500      * @see #detail(String)
501      * @see #detail(String, Object, int)
502      */

503     public static void detail(
504         String JavaDoc logString,
505         Object JavaDoc logObj
506     ) {
507         if (!initialized) init();
508         if (LogLevel.LOG_LEVEL_DETAIL <= singleton.getLoggingLevel()) {
509             singleton.logString(
510                 AuditLog.logSource,
511                 logString,
512                 logObj,
513                 LogLevel.LOG_LEVEL_DETAIL,
514                 0);
515         }
516     }
517
518     /**
519      * Logs a text string at DETAIL_LEVEL.
520      *
521      * @param logString
522      * a concise summary message. The message must be single line and
523      * must therefore not contain any '\r' or '\n' characters. The '\r'
524      * and '\n' characters are removed silently from the message.
525      * @see #detail(String, Object)
526      * @see #detail(String, Object, int)
527      */

528     public static void detail(
529         String JavaDoc logString
530     ) {
531         if (!initialized) init();
532         if (LogLevel.LOG_LEVEL_DETAIL <= singleton.getLoggingLevel()) {
533             singleton.logString(
534                 AuditLog.logSource,
535                 logString,
536                 null,
537                 LogLevel.LOG_LEVEL_DETAIL,
538                 0);
539         }
540     }
541
542     /**
543      * Logs a text string at DETAIL_LEVEL.
544      *
545      * @param logString
546      * a concise summary message. The message must be single line and
547      * must therefore not contain any '\r' or '\n' characters. The '\r'
548      * and '\n' characters are removed silently from the message.
549      * @param logObj
550      * a log object providing detail information. The log object is
551      * stringified using its <code>toString</code> method before getting
552      * logged. The log object may be a null object. If the log object
553      * is a <code>Throwable</code> it's message and stack trace is
554      * logged.
555      * @param callStackOff
556      * a call stack correction offset. The offset must be a positive
557      * number: 0, 1, 2, 3, ...
558      * @see #detail(String)
559      * @see #detail(String, Object)
560      */

561     public static void detail(
562         String JavaDoc logString,
563         Object JavaDoc logObj,
564         int callStackOff
565     ) {
566         if (!initialized) init();
567         if (LogLevel.LOG_LEVEL_DETAIL <= singleton.getLoggingLevel()) {
568             singleton.logString(
569                 AuditLog.logSource,
570                 logString,
571                 logObj,
572                 LogLevel.LOG_LEVEL_DETAIL,
573                 callStackOff);
574         }
575     }
576
577     /**
578      * Logs a text string at TRACE_LEVEL.
579      *
580      * @param logString
581      * a concise summary message. The message must be single line and
582      * must therefore not contain any '\r' or '\n' characters. The '\r'
583      * and '\n' characters are removed silently from the message.
584      * @param logObj
585      * a log object providing detail information. The log object is
586      * stringified using its <code>toString</code> method before getting
587      * logged. The log object may be a null object. If the log object
588      * is a <code>Throwable</code> it's message and stack trace is
589      * logged.
590      * @see #trace(String)
591      * @see #trace(String, Object, int)
592      */

593     public static void trace(
594         String JavaDoc logString,
595         Object JavaDoc logObj
596     ) {
597         if (!initialized) init();
598         if (LogLevel.LOG_LEVEL_TRACE <= singleton.getLoggingLevel()) {
599             singleton.logString(
600                 AuditLog.logSource,
601                 logString,
602                 logObj,
603                 LogLevel.LOG_LEVEL_TRACE,
604                 0);
605         }
606     }
607
608     /**
609      * Logs a text string at TRACE_LEVEL.
610      *
611      * @param logString
612      * a concise summary message. The message must be single line and
613      * must therefore not contain any '\r' or '\n' characters. The '\r'
614      * and '\n' characters are removed silently from the message.
615      * @see #trace(String, Object)
616      * @see #trace(String, Object, int)
617      */

618     public static void trace(
619         String JavaDoc logString
620     ) {
621         if (!initialized) init();
622         if (LogLevel.LOG_LEVEL_TRACE <= singleton.getLoggingLevel()) {
623             singleton.logString(
624                 AuditLog.logSource,
625                 logString,
626                 null,
627                 LogLevel.LOG_LEVEL_TRACE,
628                 0);
629         }
630     }
631
632     /**
633      * Logs a text string at TRACE_LEVEL.
634      *
635      * @param logString
636      * a concise summary message. The message must be single line and
637      * must therefore not contain any '\r' or '\n' characters. The '\r'
638      * and '\n' characters are removed silently from the message.
639      * @param logObj
640      * a log object providing detail information. The log object is
641      * stringified using its <code>toString</code> method before getting
642      * logged. The log object may be a null object. If the log object
643      * is a <code>Throwable</code> it's message and stack trace is
644      * logged.
645      * @param callStackOff
646      * a call stack correction offset. The offset must be a positive
647      * number: 0, 1, 2, 3, ...
648      * @see #trace(String)
649      * @see #trace(String, Object)
650      */

651     public static void trace(
652         String JavaDoc logString,
653         Object JavaDoc logObj,
654         int callStackOff
655     ) {
656         if (!initialized) init();
657         if (LogLevel.LOG_LEVEL_TRACE <= singleton.getLoggingLevel()) {
658             singleton.logString(
659                 AuditLog.logSource,
660                 logString,
661                 logObj,
662                 LogLevel.LOG_LEVEL_TRACE,
663                 callStackOff);
664         }
665     }
666
667     /**
668      * Set the application name string that the logs pertain to. Must be called
669      * before doing any logging.
670      *
671      * @param appName the application name
672      * @deprecated use {@link AuditLog#setConfigName(String)} instead
673      */

674     public static void setApplicationName(String JavaDoc appName)
675     {
676         setConfigName(appName);
677     }
678
679     /**
680      * Set logging level
681      *
682      * <p>
683      * This method should not be used by applications. The logging level is set
684      * in the log property file.
685      *
686      * @param level a new logging level
687      * @deprecated use {@link Config#setLogLevel(int)} from the
688      * {@link AuditLog#getLogConfig()} class
689      */

690     public static void setLogLevel(
691         int logLevel
692     ) {
693         if (!initialized) init();
694         singleton.setLoggingLevel(logLevel);
695     }
696
697     private static void init(
698     ) {
699         synchronized(singleton) {
700             if (!initialized) {
701                 initialized = true;
702         
703                 singleton.loadConfig(AuditLog.configName, AuditLog.logProperties);
704                 singleton.loadMechanisms();
705           
706                 SysLog.register(AuditLog.getLogConfig());
707                 SysLog.trace("AuditLog (CfgName=" + AuditLog.configName + ") initialized");
708         
709                 // openmdx jar version logging
710
AuditLog.info("openMDX kernel version : "
711                             + org.openmdx.kernel.Version.getImplementationVersion());
712                             
713                 AuditLog.info("openMDX base version : "
714                             + org.openmdx.base.Version.getImplementationVersion());
715                 
716                 AuditLog.info("openMDX application version: "
717                             + org.openmdx.application.Version.getImplementationVersion());
718                 
719                 // openmdx jar version dependeny check
720
try {
721                     Dependencies.checkDependencies();
722                 }
723                 catch (VersionMismatchException exception) {
724                     AuditLog.error("Dependency check failed", exception);
725                     throw exception;
726                 }
727             }
728         }
729     }
730
731     /** Constants */
732     final private static String JavaDoc LOGNAME = "AuditLog";
733     final static String JavaDoc LOGSOURCE = "Audit";
734
735     /**
736      * Provides the class variable for the Singleton pattern,
737      * to keep track of the one and only instance of this class.
738      */

739     private static volatile Log singleton = newLog("AuditLog", AuditLog.class);
740
741     private static volatile boolean initialized = false;
742
743     /** The application properties. These properties are optional */
744     private static Properties JavaDoc logProperties = null;
745
746     /** The application name. Default is the log name */
747     private static String JavaDoc configName = LOGNAME;
748
749     /** The log source */
750     private static Object JavaDoc logSource = LOGSOURCE;
751 }
752
753 //--- End of File -----------------------------------------------------------
754

755
Popular Tags