View Javadoc

1   /* 
2    * Copyright (c) 2007, Fraunhofer-Gesellschaft
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    * 
9    * (1) Redistributions of source code must retain the above copyright
10   *     notice, this list of conditions and the disclaimer at the end.
11   *     Redistributions in binary form must reproduce the above copyright
12   *     notice, this list of conditions and the following disclaimer in
13   *     the documentation and/or other materials provided with the
14   *     distribution.
15   * 
16   * (2) Neither the name of Fraunhofer nor the names of its
17   *     contributors may be used to endorse or promote products derived
18   *     from this software without specific prior written permission.
19   * 
20   * DISCLAIMER
21   * 
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   *  
34   */
35  package org.ogf.graap.wsag.wsrf.bootstrap;
36  
37  import java.text.MessageFormat;
38  import java.util.logging.Handler;
39  import java.util.logging.Level;
40  import java.util.logging.LogRecord;
41  
42  import org.apache.log4j.Logger;
43  import org.apache.log4j.Priority;
44  
45  /**
46   * MuseLog4jHandler
47   * 
48   * @author Oliver Waeldrich
49   * 
50   */
51  public class WSAG4JMuseLog4jHandler extends Handler
52  {
53  
54      private static final int OFF = Level.OFF.intValue();
55  
56      private static final int SEVERE = Level.SEVERE.intValue();
57  
58      private static final int WARNING = Level.WARNING.intValue();
59  
60      private static final int INFO = Level.INFO.intValue();
61  
62      private static final int CONFIG = Level.CONFIG.intValue();
63  
64      private static final int FINE = Level.FINE.intValue();
65  
66      private static final int FINER = Level.FINER.intValue();
67  
68      private static final int FINEST = Level.FINEST.intValue();
69  
70      private static final int ALL = Level.ALL.intValue();
71  
72      /**
73       * {@inheritDoc}
74       */
75      public void publish( LogRecord record )
76      {
77          org.apache.log4j.Logger log4j = getLog4JLogger( record.getLoggerName() );
78          Priority priority = toLog4jLogLevel( record.getLevel() );
79          log4j.log( priority, toLog4jMessage( record ), record.getThrown() );
80      }
81  
82      static Logger getLog4JLogger( String loggerName )
83      {
84          return Logger.getLogger( loggerName );
85      }
86  
87      /**
88       * Retrieves the Log4J logger for a given class-
89       * 
90       * @param clazz
91       *            the class for which the logger should be retrieved
92       * 
93       * @return a {@link Logger} instance
94       */
95      public static Logger getLogger( Class<?> clazz )
96      {
97          return getLog4JLogger( clazz.getName() );
98      }
99  
100     private String toLog4jMessage( LogRecord record )
101     {
102         String message = record.getMessage();
103 
104         // Format message
105         try
106         {
107             Object parameters[] = record.getParameters();
108             if ( parameters != null && parameters.length != 0 )
109             {
110                 message = MessageFormat.format( message, parameters );
111             }
112         }
113         catch ( Exception ex )
114         {
115             // ignore Exception
116             message = MessageFormat.format( "{0}\n{1}", new Object[] { message, ex.getMessage() } );
117         }
118 
119         return message;
120     }
121 
122     private org.apache.log4j.Level toLog4jLogLevel( Level level )
123     {
124 
125         if ( level.intValue() == ALL )
126         {
127             return org.apache.log4j.Level.ALL;
128         }
129         if ( level.intValue() <= FINEST )
130         {
131             return org.apache.log4j.Level.TRACE;
132         }
133         if ( level.intValue() <= FINER )
134         {
135             return org.apache.log4j.Level.TRACE;
136         }
137         if ( level.intValue() <= FINE )
138         {
139             return org.apache.log4j.Level.TRACE;
140         }
141         if ( level.intValue() <= CONFIG )
142         {
143             return org.apache.log4j.Level.INFO;
144         }
145         if ( level.intValue() <= INFO )
146         {
147             return org.apache.log4j.Level.INFO;
148         }
149         if ( level.intValue() <= WARNING )
150         {
151             return org.apache.log4j.Level.WARN;
152         }
153         if ( level.intValue() <= SEVERE )
154         {
155             return org.apache.log4j.Level.ERROR;
156         }
157         if ( level.intValue() == OFF )
158         {
159             return org.apache.log4j.Level.OFF;
160         }
161         return org.apache.log4j.Level.OFF;
162     }
163 
164     @Override
165     public void flush()
166     {
167         // nothing to do
168     }
169 
170     @Override
171     public void close()
172     {
173         // nothing to do
174     }
175 
176 }