View Javadoc

1   /*
2    *  jDTAUS Core RI JDK 1.4 Logging
3    *  Copyright (C) 2005 Christian Schulte
4    *  <cs@schulte.it>
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19   *
20   */
21  package org.jdtaus.core.logging.ri.jdk14;
22  
23  import java.util.logging.Level;
24  import org.jdtaus.core.container.ContainerFactory;
25  import org.jdtaus.core.logging.spi.Logger;
26  
27  /**
28   * jDTAUS Core SPI JDK 1.4 {@code Logger} implementation.
29   * <p>The name of the JDK logger is specified by property {@code name}.
30   * Property {@code name} defaults to {@code org.jdtaus.runtime}.</p>
31   *
32   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
33   * @version $JDTAUS: JDK14Logger.java 8641 2012-09-27 06:45:17Z schulte $
34   *
35   * @see org.jdtaus.core.container.Container
36   */
37  public class JDK14Logger implements Logger
38  {
39      //--Constructors------------------------------------------------------------
40  
41  // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausConstructors
42      // This section is managed by jdtaus-container-mojo.
43  
44      /** Standard implementation constructor <code>org.jdtaus.core.logging.ri.jdk14.JDK14Logger</code>. */
45      public JDK14Logger()
46      {
47          super();
48      }
49  
50  // </editor-fold>//GEN-END:jdtausConstructors
51  
52      //------------------------------------------------------------Constructors--
53      //--Properties--------------------------------------------------------------
54  
55  // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausProperties
56      // This section is managed by jdtaus-container-mojo.
57  
58      /**
59       * Gets the value of property <code>name</code>.
60       *
61       * @return Name uniquely identifying the logger.
62       */
63      public java.lang.String getName()
64      {
65          return (java.lang.String) ContainerFactory.getContainer().
66              getProperty( this, "name" );
67  
68      }
69  
70  // </editor-fold>//GEN-END:jdtausProperties
71  
72      //--------------------------------------------------------------Properties--
73      //--Logger------------------------------------------------------------------
74  
75      public boolean isDebugEnabled()
76      {
77          return this.getLogger().isLoggable( Level.FINE );
78      }
79  
80      public void debug( String message )
81      {
82          this.log( Level.FINE, message, null );
83      }
84  
85      public void debug( Throwable throwable )
86      {
87          this.log( Level.FINE, throwable.getMessage(), throwable );
88      }
89  
90      public boolean isErrorEnabled()
91      {
92          return this.getLogger().isLoggable( Level.SEVERE );
93      }
94  
95      public void error( String message )
96      {
97          this.log( Level.SEVERE, message, null );
98      }
99  
100     public void error( Throwable throwable )
101     {
102         this.log( Level.SEVERE, throwable.getMessage(), throwable );
103     }
104 
105     public boolean isFatalEnabled()
106     {
107         return this.getLogger().isLoggable( Level.SEVERE );
108     }
109 
110     public void fatal( String message )
111     {
112         this.log( Level.SEVERE, message, null );
113     }
114 
115     public void fatal( Throwable throwable )
116     {
117         this.log( Level.SEVERE, throwable.getMessage(), throwable );
118     }
119 
120     public boolean isInfoEnabled()
121     {
122         return this.getLogger().isLoggable( Level.INFO );
123     }
124 
125     public void info( String message )
126     {
127         this.log( Level.INFO, message, null );
128     }
129 
130     public void info( Throwable throwable )
131     {
132         this.log( Level.INFO, throwable.getMessage(), throwable );
133     }
134 
135     public boolean isTraceEnabled()
136     {
137         return this.getLogger().isLoggable( Level.FINEST );
138     }
139 
140     public void trace( String message )
141     {
142         this.log( Level.FINEST, message, null );
143     }
144 
145     public void trace( Throwable throwable )
146     {
147         this.log( Level.FINEST, throwable.getMessage(), throwable );
148     }
149 
150     public boolean isWarnEnabled()
151     {
152         return this.getLogger().isLoggable( Level.WARNING );
153     }
154 
155     public void warn( String message )
156     {
157         this.log( Level.WARNING, message, null );
158     }
159 
160     public void warn( Throwable throwable )
161     {
162         this.log( Level.WARNING, throwable.getMessage(), throwable );
163     }
164 
165     //------------------------------------------------------------------Logger--
166     //--JDK14Logger-------------------------------------------------------------
167 
168     /**
169      * Requests the JDK logger for the name given by property {@code name}.
170      *
171      * @return the JDK logger for the name given by property {@code name}.
172      */
173     public java.util.logging.Logger getLogger()
174     {
175         return java.util.logging.Logger.getLogger( this.getName() );
176     }
177 
178     private void log( final Level level, final String msg, final Throwable t )
179     {
180         if ( this.getLogger().isLoggable( level ) )
181         {
182             StackTraceElement caller;
183             final Throwable x = new Throwable();
184             final StackTraceElement[] elements = x.getStackTrace();
185 
186             String cname = "unknown";
187             String method = "unknown";
188 
189             if ( elements != null && elements.length > 2 )
190             {
191                 caller = elements[2];
192                 cname = caller.getClassName();
193                 method = caller.getMethodName();
194             }
195 
196             if ( t == null )
197             {
198                 this.getLogger().logp( level, cname, method, msg );
199             }
200             else
201             {
202                 this.getLogger().logp( level, cname, method, msg, t );
203             }
204         }
205     }
206 
207     //-------------------------------------------------------------JDK14Logger--
208 }