001    /*******************************************************************************
002     * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
003     * Thomschke.
004     * 
005     * All Rights Reserved. This program and the accompanying materials
006     * are made available under the terms of the Eclipse Public License v1.0
007     * which accompanies this distribution, and is available at
008     * http://www.eclipse.org/legal/epl-v10.html
009     * 
010     * Contributors:
011     *     Sebastian Thomschke - initial implementation.
012     *******************************************************************************/
013    package net.sf.oval.logging;
014    
015    import java.util.logging.Level;
016    import java.util.logging.LogRecord;
017    
018    import net.sf.oval.internal.util.Assert;
019    
020    /**
021     * JDK Logging Wrapper
022     * @author Sebastian Thomschke
023     */
024    public class LoggerJDKImpl implements Logger
025    {
026            private final java.util.logging.Logger jdkLogger;
027            private final String name;
028    
029            /**
030             * @param name the name of the logger
031             * @throws IllegalArgumentException if <code>name == null</code>
032             */
033            public LoggerJDKImpl(final String name) throws IllegalArgumentException
034            {
035                    Assert.argumentNotNull("name", name);
036    
037                    this.name = name;
038                    jdkLogger = java.util.logging.Logger.getLogger(name);
039            }
040    
041            /**
042             * {@inheritDoc}
043             */
044            public void debug(final String msg)
045            {
046                    log(Level.FINE, msg, null);
047            }
048    
049            /**
050             * {@inheritDoc}
051             */
052            public void debug(final String msg, final Throwable t)
053            {
054                    log(Level.FINE, msg, t);
055            }
056    
057            /**
058             * {@inheritDoc}
059             */
060            public void error(final String msg)
061            {
062                    log(Level.SEVERE, msg, null);
063            }
064    
065            /**
066             * {@inheritDoc}
067             */
068            public void error(final String msg, final Throwable t)
069            {
070                    log(Level.SEVERE, msg, t);
071            }
072    
073            /**
074             * {@inheritDoc}
075             */
076            public void info(final String msg)
077            {
078                    log(Level.INFO, msg, null);
079            }
080    
081            /**
082             * {@inheritDoc}
083             */
084            public void info(final String msg, final Throwable t)
085            {
086                    log(Level.INFO, msg, t);
087            }
088    
089            /**
090             * {@inheritDoc}
091             */
092            public boolean isDebug()
093            {
094                    return jdkLogger.isLoggable(Level.FINE);
095            }
096    
097            /**
098             * {@inheritDoc}
099             */
100            public boolean isError()
101            {
102                    return jdkLogger.isLoggable(Level.SEVERE);
103            }
104    
105            /**
106             * {@inheritDoc}
107             */
108            public boolean isInfo()
109            {
110                    return jdkLogger.isLoggable(Level.INFO);
111            }
112    
113            /**
114             * {@inheritDoc}
115             */
116            public boolean isTrace()
117            {
118                    return jdkLogger.isLoggable(Level.FINEST);
119            }
120    
121            /**
122             * {@inheritDoc}
123             */
124            public boolean isWarn()
125            {
126                    return jdkLogger.isLoggable(Level.WARNING);
127            }
128    
129            private void log(final Level level, final String msg, final Throwable t)
130            {
131                    final LogRecord record = new LogRecord(level, msg);
132                    record.setLoggerName(name);
133                    record.setThrown(t);
134    
135                    /* java.lang.Throwable
136                     *      at net.sf.oval.logging.LoggerJDKImpl.log(LoggerJDKImpl.java:123)
137                     *      at net.sf.oval.logging.LoggerJDKImpl.warn(LoggerJDKImpl.java:136)
138                     *      at net.sf.oval.internal.Log.warn(Log.java:180)
139                     */
140                    final int offset = 2;
141                    final StackTraceElement[] steArray = new Throwable().getStackTrace();
142                    record.setSourceClassName(steArray[offset].getClassName());
143                    record.setSourceMethodName(steArray[offset].getMethodName());
144    
145                    jdkLogger.log(record);
146            }
147    
148            /**
149             * {@inheritDoc}
150             */
151            public void trace(final String msg)
152            {
153                    log(Level.FINEST, msg, null);
154            }
155    
156            /**
157             * {@inheritDoc}
158             */
159            public void trace(final String msg, final Throwable t)
160            {
161                    log(Level.FINEST, msg, t);
162            }
163    
164            /**
165             * {@inheritDoc}
166             */
167            public void warn(final String msg)
168            {
169                    log(Level.WARNING, msg, null);
170            }
171    
172            /**
173             * {@inheritDoc}
174             */
175            public void warn(final String msg, final Throwable t)
176            {
177                    log(Level.WARNING, msg, t);
178            }
179    }