001 /* 002 * Copyright 2006 Stephen J. McConnell. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 013 * implied. 014 * 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019 package net.dpml.util; 020 021 import java.util.logging.Logger; 022 import java.util.logging.Level; 023 024 import net.dpml.lang.PID; 025 026 /** 027 * Generic logging channel. 028 * 029 * @author <a href="http://www.dpml.net">Digital Product Meta Library</a> 030 * @version 1.1.0 031 */ 032 public final class DefaultLogger implements net.dpml.util.Logger 033 { 034 // ------------------------------------------------------------------------ 035 // static 036 // ------------------------------------------------------------------------ 037 038 private static final PID ID = new PID(); 039 040 private static String clean( String category ) 041 { 042 String result = category.replace( '/', '.' ); 043 if( result.startsWith( "." ) ) 044 { 045 return clean( result.substring( 1 ) ); 046 } 047 else if( result.endsWith( "." ) ) 048 { 049 return clean( result.substring( 0, result.length() -1 ) ); 050 } 051 else 052 { 053 return result; 054 } 055 } 056 057 // ------------------------------------------------------------------------ 058 // state 059 // ------------------------------------------------------------------------ 060 061 private Logger m_logger; 062 063 // ------------------------------------------------------------------------ 064 // constructor 065 // ------------------------------------------------------------------------ 066 067 /** 068 * Creation of a new console adapter that is used to redirect transit events 069 * the system output stream. 070 */ 071 public DefaultLogger() 072 { 073 this( "" ); 074 } 075 076 /** 077 * Creation of a new console adapter that is used to redirect transit events 078 * the system output stream. 079 * @param category the logging channel category name 080 */ 081 public DefaultLogger( String category ) 082 { 083 this( Logger.getLogger( clean( category ) ) ); 084 } 085 086 /** 087 * Creation of a new console adapter that is used to redirect transit events 088 * the system output stream. 089 * @param logger the assigned logging channel 090 */ 091 public DefaultLogger( Logger logger ) 092 { 093 m_logger = logger; 094 } 095 096 // ------------------------------------------------------------------------ 097 // Logger 098 // ------------------------------------------------------------------------ 099 100 /** 101 * Return TRUE is trace level logging is enabled. 102 * @return the enabled state of trace logging 103 */ 104 public boolean isTraceEnabled() 105 { 106 return m_logger.isLoggable( Level.FINER ); 107 } 108 109 /** 110 * Return TRUE is debug level logging is enabled. 111 * @return the enabled state of debug logging 112 */ 113 public boolean isDebugEnabled() 114 { 115 return m_logger.isLoggable( Level.FINE ); 116 } 117 118 /** 119 * Return TRUE is info level logging is enabled. 120 * @return the enabled state of info logging 121 */ 122 public boolean isInfoEnabled() 123 { 124 return m_logger.isLoggable( Level.INFO ); 125 } 126 127 /** 128 * Return TRUE is error level logging is enabled. 129 * @return the enabled state of error logging 130 */ 131 public boolean isWarnEnabled() 132 { 133 return m_logger.isLoggable( Level.WARNING ); 134 } 135 136 /** 137 * Return TRUE is error level logging is enabled. 138 * @return the enabled state of error logging 139 */ 140 public boolean isErrorEnabled() 141 { 142 return m_logger.isLoggable( Level.SEVERE ); 143 } 144 145 /** 146 * Log a debug message is trace mode is enabled. 147 * @param message the message to log 148 */ 149 public void trace( String message ) 150 { 151 if( isTraceEnabled() ) 152 { 153 m_logger.finer( message ); 154 } 155 } 156 157 /** 158 * Log a debug message is debug mode is enabled. 159 * @param message the message to log 160 */ 161 public void debug( String message ) 162 { 163 if( isDebugEnabled() ) 164 { 165 m_logger.fine( message ); 166 } 167 } 168 169 /** 170 * Log a info level message. 171 * @param message the message to log 172 */ 173 public void info( String message ) 174 { 175 if( isInfoEnabled() ) 176 { 177 m_logger.info( message ); 178 } 179 } 180 181 /** 182 * Record a warning message. 183 * @param message the warning message to record 184 */ 185 public void warn( String message ) 186 { 187 if( isWarnEnabled() ) 188 { 189 m_logger.warning( message ); 190 } 191 } 192 193 /** 194 * Record a warning message. 195 * @param message the warning message to record 196 * @param cause the causal exception 197 */ 198 public void warn( String message, Throwable cause ) 199 { 200 if( isWarnEnabled() ) 201 { 202 m_logger.log( Level.WARNING, message, cause ); 203 } 204 } 205 206 /** 207 * Log a error message. 208 * @param message the message to log 209 */ 210 public void error( String message ) 211 { 212 if( isErrorEnabled() ) 213 { 214 m_logger.log( Level.SEVERE, message ); 215 } 216 } 217 218 /** 219 * Log a error message. 220 * @param message the message to log 221 * @param e the causal exception 222 */ 223 public void error( String message, Throwable e ) 224 { 225 if( isErrorEnabled() ) 226 { 227 m_logger.log( Level.SEVERE, message, e ); 228 } 229 } 230 231 /** 232 * Return a child logger. 233 * @param category the sub-category name. 234 * @return the child logging channel 235 */ 236 public net.dpml.util.Logger getChildLogger( String category ) 237 { 238 if( ( null == category ) || "".equals( category ) ) 239 { 240 return this; 241 } 242 else 243 { 244 String name = m_logger.getName(); 245 String path = trim( name + "." + category ); 246 return new DefaultLogger( Logger.getLogger( path ) ); 247 } 248 } 249 250 private String trim( String path ) 251 { 252 if( path.startsWith( "." ) ) 253 { 254 return trim( path.substring( 1 ) ); 255 } 256 else if( ".".equals( path ) ) 257 { 258 return ""; 259 } 260 else 261 { 262 return path; 263 } 264 } 265 } 266