001/* 002 * jDTAUS Core RI JDK 1.4 Logging 003 * Copyright (C) 2005 Christian Schulte 004 * <cs@schulte.it> 005 * 006 * This library is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 2.1 of the License, or any later version. 010 * 011 * This library is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with this library; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 019 * 020 */ 021package org.jdtaus.core.logging.ri.jdk14; 022 023import java.util.logging.Level; 024import org.jdtaus.core.container.ContainerFactory; 025import org.jdtaus.core.logging.spi.Logger; 026 027/** 028 * jDTAUS Core SPI JDK 1.4 {@code Logger} implementation. 029 * <p>The name of the JDK logger is specified by property {@code name}. 030 * Property {@code name} defaults to {@code org.jdtaus.runtime}.</p> 031 * 032 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 033 * @version $JDTAUS: JDK14Logger.java 8743 2012-10-07 03:06:20Z schulte $ 034 * 035 * @see org.jdtaus.core.container.Container 036 */ 037public class JDK14Logger implements Logger 038{ 039 //--Constructors------------------------------------------------------------ 040 041// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausConstructors 042 // This section is managed by jdtaus-container-mojo. 043 044 /** Standard implementation constructor <code>org.jdtaus.core.logging.ri.jdk14.JDK14Logger</code>. */ 045 public JDK14Logger() 046 { 047 super(); 048 } 049 050// </editor-fold>//GEN-END:jdtausConstructors 051 052 //------------------------------------------------------------Constructors-- 053 //--Properties-------------------------------------------------------------- 054 055// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausProperties 056 // This section is managed by jdtaus-container-mojo. 057 058 /** 059 * Gets the value of property <code>name</code>. 060 * 061 * @return Name uniquely identifying the logger. 062 */ 063 public java.lang.String getName() 064 { 065 return (java.lang.String) ContainerFactory.getContainer(). 066 getProperty( this, "name" ); 067 068 } 069 070// </editor-fold>//GEN-END:jdtausProperties 071 072 //--------------------------------------------------------------Properties-- 073 //--Logger------------------------------------------------------------------ 074 075 public boolean isDebugEnabled() 076 { 077 return this.getLogger().isLoggable( Level.FINE ); 078 } 079 080 public void debug( final String message ) 081 { 082 this.log( Level.FINE, message, null ); 083 } 084 085 public void debug( final Throwable throwable ) 086 { 087 this.log( Level.FINE, throwable.getMessage(), throwable ); 088 } 089 090 public boolean isErrorEnabled() 091 { 092 return this.getLogger().isLoggable( Level.SEVERE ); 093 } 094 095 public void error( final String message ) 096 { 097 this.log( Level.SEVERE, message, null ); 098 } 099 100 public void error( final 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( final String message ) 111 { 112 this.log( Level.SEVERE, message, null ); 113 } 114 115 public void fatal( final 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( final String message ) 126 { 127 this.log( Level.INFO, message, null ); 128 } 129 130 public void info( final 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( final String message ) 141 { 142 this.log( Level.FINEST, message, null ); 143 } 144 145 public void trace( final 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( final String message ) 156 { 157 this.log( Level.WARNING, message, null ); 158 } 159 160 public void warn( final 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}