View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
3    * Thomschke.
4    * 
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    * 
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.logging;
14  
15  import java.util.logging.Level;
16  import java.util.logging.LogRecord;
17  
18  import net.sf.oval.internal.util.Assert;
19  
20  /**
21   * JDK Logging Wrapper
22   * @author Sebastian Thomschke
23   */
24  public class LoggerJDKImpl implements Logger
25  {
26  	private final java.util.logging.Logger jdkLogger;
27  	private final String name;
28  
29  	/**
30  	 * @param name the name of the logger
31  	 * @throws IllegalArgumentException if <code>name == null</code>
32  	 */
33  	public LoggerJDKImpl(final String name) throws IllegalArgumentException
34  	{
35  		Assert.argumentNotNull("name", name);
36  
37  		this.name = name;
38  		jdkLogger = java.util.logging.Logger.getLogger(name);
39  	}
40  
41  	/**
42  	 * {@inheritDoc}
43  	 */
44  	public void debug(final String msg)
45  	{
46  		log(Level.FINE, msg, null);
47  	}
48  
49  	/**
50  	 * {@inheritDoc}
51  	 */
52  	public void debug(final String msg, final Throwable t)
53  	{
54  		log(Level.FINE, msg, t);
55  	}
56  
57  	/**
58  	 * {@inheritDoc}
59  	 */
60  	public void error(final String msg)
61  	{
62  		log(Level.SEVERE, msg, null);
63  	}
64  
65  	/**
66  	 * {@inheritDoc}
67  	 */
68  	public void error(final String msg, final Throwable t)
69  	{
70  		log(Level.SEVERE, msg, t);
71  	}
72  
73  	/**
74  	 * {@inheritDoc}
75  	 */
76  	public void info(final String msg)
77  	{
78  		log(Level.INFO, msg, null);
79  	}
80  
81  	/**
82  	 * {@inheritDoc}
83  	 */
84  	public void info(final String msg, final Throwable t)
85  	{
86  		log(Level.INFO, msg, t);
87  	}
88  
89  	/**
90  	 * {@inheritDoc}
91  	 */
92  	public boolean isDebug()
93  	{
94  		return jdkLogger.isLoggable(Level.FINE);
95  	}
96  
97  	/**
98  	 * {@inheritDoc}
99  	 */
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 }