View Javadoc

1   /*
2    * This file is part of Domingo
3    * an Open Source Java-API to Lotus Notes/Domino
4    * hosted at http://domingo.sourceforge.net
5    *
6    * Copyright (c) 2003-2007 Beck et al. projects GmbH Munich, Germany (http://www.bea.de)
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   * License as published by the Free Software Foundation; either
11   * version 2.1 of the License, or (at your option) any later version.
12   *
13   * This library is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   * Lesser General Public License for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public
19   * License along with this library; if not, write to the Free Software
20   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21   */
22  
23  package de.bea.domingo.monitor;
24  
25  import java.util.logging.Level;
26  import java.util.logging.Logger;
27  
28  import de.bea.domingo.DNotesMonitor;
29  
30  /***
31   * Adapter from Java Logging to the domingo monitor interface.
32   *
33   * <p>Use an instance of this class as argument to
34   * DNotesFactory.getInstance(DNotesMonitor theMonitor).</p>
35   *
36   * @since domingo 1.1
37   */
38  public final class Jdk14LoggerMonitor implements DNotesMonitor {
39  
40      /*** Reference to the log that will receive all log messages from Domingo. */
41      private Logger logger;
42  
43      /***
44       * Constructor.
45       *
46       * <p>Creates a monitor with a default log.</p>
47       */
48      public Jdk14LoggerMonitor() {
49          this(Logger.getLogger("JDK14LoggerMonitor"));
50      }
51  
52      /***
53       * Constructor.
54       *
55       * @param log a log instance
56       */
57      public Jdk14LoggerMonitor(final Logger log) {
58          logger = log;
59      }
60  
61      /***
62       * Returns the current log.
63       *
64       * @return current log
65       */
66      public Logger getLogger() {
67          return logger;
68      }
69  
70      /***
71       * Sets a new log.
72       *
73       * @param log the new log
74       */
75      public void setLogger(final Logger log) {
76          this.logger = log;
77      }
78  
79      /***
80       * {@inheritDoc}
81       * @see de.bea.domingo.DNotesMonitor#debug(java.lang.String, java.lang.Throwable)
82       */
83      public void debug(final String message, final Throwable throwable) {
84          log(Level.FINE, message, throwable);
85      }
86  
87      /***
88       * {@inheritDoc}
89       * @see de.bea.domingo.DNotesMonitor#debug(java.lang.String)
90       */
91      public void debug(final String message) {
92          log(Level.FINE, message, null);
93      }
94  
95      /***
96       * {@inheritDoc}
97       * @see de.bea.domingo.DNotesMonitor#info(java.lang.String, java.lang.Throwable)
98       */
99      public void info(final String message, final Throwable throwable) {
100         log(Level.INFO, String.valueOf(message), throwable);
101     }
102 
103     /***
104      * {@inheritDoc}
105      * @see de.bea.domingo.DNotesMonitor#info(java.lang.String)
106      */
107     public void info(final String message) {
108         log(Level.INFO, String.valueOf(message), null);
109     }
110 
111     /***
112      * {@inheritDoc}
113      * @see de.bea.domingo.DNotesMonitor#warn(java.lang.String, java.lang.Throwable)
114      */
115     public void warn(final String message, final Throwable throwable) {
116         log(Level.WARNING, String.valueOf(message), throwable);
117     }
118 
119     /***
120      * {@inheritDoc}
121      * @see de.bea.domingo.DNotesMonitor#warn(java.lang.String)
122      */
123     public void warn(final String message) {
124         log(Level.WARNING, String.valueOf(message), null);
125     }
126 
127     /***
128      * {@inheritDoc}
129      * @see de.bea.domingo.DNotesMonitor#error(java.lang.String, java.lang.Throwable)
130      */
131     public void error(final String message, final Throwable throwable) {
132         log(Level.SEVERE, String.valueOf(message), throwable);
133     }
134 
135     /***
136      * {@inheritDoc}
137      * @see de.bea.domingo.DNotesMonitor#error(java.lang.String)
138      */
139     public void error(final String message) {
140         log(Level.SEVERE, String.valueOf(message), null);
141     }
142 
143     /***
144      * {@inheritDoc}
145      * @see de.bea.domingo.DNotesMonitor#fatalError(java.lang.String, java.lang.Throwable)
146      */
147     public void fatalError(final String message, final Throwable throwable) {
148         log(Level.SEVERE, String.valueOf(message), throwable);
149     }
150 
151     /***
152      * {@inheritDoc}
153      * @see de.bea.domingo.DNotesMonitor#fatalError(java.lang.String)
154      */
155     public void fatalError(final String message) {
156         log(Level.SEVERE, String.valueOf(message), null);
157     }
158 
159     /***
160      * {@inheritDoc}
161      * @see de.bea.domingo.DNotesMonitor#isDebugEnabled()
162      */
163     public boolean isDebugEnabled() {
164         return logger.isLoggable(Level.FINE);
165     }
166 
167     /***
168      * {@inheritDoc}
169      * @see de.bea.domingo.DNotesMonitor#isInfoEnabled()
170      */
171     public boolean isInfoEnabled() {
172         return logger.isLoggable(Level.INFO);
173     }
174 
175     /***
176      * {@inheritDoc}
177      * @see de.bea.domingo.DNotesMonitor#isWarnEnabled()
178      */
179     public boolean isWarnEnabled() {
180         return logger.isLoggable(Level.WARNING);
181     }
182 
183     /***
184      * {@inheritDoc}
185      * @see de.bea.domingo.DNotesMonitor#isErrorEnabled()
186      */
187     public boolean isErrorEnabled() {
188         return logger.isLoggable(Level.SEVERE);
189     }
190 
191     /***
192      * {@inheritDoc}
193      * @see de.bea.domingo.DNotesMonitor#isFatalErrorEnabled()
194      */
195     public boolean isFatalErrorEnabled() {
196         return logger.isLoggable(Level.SEVERE);
197     }
198 
199     /***
200      * Logs a log event depending on the level of the logger.
201      *
202      * @param level the log level
203      * @param msg the message
204      * @param throwable optional throwable
205      */
206     private void log(final Level level, final String msg, final Throwable throwable) {
207         if (logger.isLoggable(level)) {
208             Throwable dummyException = new Throwable();
209             StackTraceElement[] locations = dummyException.getStackTrace();
210             String cname = "unknown";
211             String method = "unknown";
212             if (locations != null && locations.length > 2) {
213                 StackTraceElement caller = locations[2];
214                 cname = caller.getClassName();
215                 method = caller.getMethodName();
216             }
217             if (throwable == null) {
218                 logger.logp(level, cname, method, msg);
219             } else {
220                 logger.logp(level, cname, method, msg, throwable);
221             }
222         }
223     }
224 }