Jdk13LumberjackLogger.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.logging.impl;

  18. import java.io.PrintWriter;
  19. import java.io.Serializable;
  20. import java.io.StringWriter;
  21. import java.util.StringTokenizer;
  22. import java.util.logging.Level;
  23. import java.util.logging.LogRecord;
  24. import java.util.logging.Logger;

  25. import org.apache.commons.logging.Log;

  26. /**
  27.  * Implements the {@code org.apache.commons.logging.Log}
  28.  * interface tp wrap the standard JDK logging mechanisms that are
  29.  * available in SourceForge's Lumberjack for JDKs prior to 1.4.
  30.  *
  31.  * @since 1.1
  32.  * @deprecated Scheduled for removal because the Lumberjack Project has been discontinued.
  33.  */
  34. @Deprecated
  35. public class Jdk13LumberjackLogger implements Log, Serializable {

  36.     /** Serializable version identifier. */
  37.     private static final long serialVersionUID = -8649807923527610591L;

  38.     /**
  39.      * This member variable simply ensures that any attempt to initialize
  40.      * this class in a pre-1.4 JVM will result in an ExceptionInInitializerError.
  41.      * It must not be private, as an optimizing compiler could detect that it
  42.      * is not used and optimize it away.
  43.      *
  44.      * @deprecated No longer used.
  45.      */
  46.     @Deprecated
  47.     protected static final Level dummyLevel = Level.FINE;

  48.     /**
  49.      * The underlying Logger implementation we are using.
  50.      */
  51.     protected transient Logger logger;

  52.     /**
  53.      * Name.
  54.      */
  55.     protected String name;

  56.     /** Source class name. */
  57.     private String sourceClassName = "unknown";

  58.     /** Source method name. */
  59.     private String sourceMethodName = "unknown";

  60.     /** Class and method found flag. */
  61.     private boolean classAndMethodFound;

  62.     /**
  63.      * Constructs a named instance of this Logger.
  64.      *
  65.      * @param name Name of the logger to be constructed
  66.      */
  67.     public Jdk13LumberjackLogger(final String name) {
  68.         this.name = name;
  69.         logger = getLogger();
  70.     }

  71.     /**
  72.      * Logs a message with {@link java.util.logging.Level#FINE}.
  73.      *
  74.      * @param message to log
  75.      * @see org.apache.commons.logging.Log#debug(Object)
  76.      */
  77.     @Override
  78.     public void debug(final Object message) {
  79.         log(Level.FINE, String.valueOf(message), null);
  80.     }

  81.     /**
  82.      * Logs a message with {@link java.util.logging.Level#FINE}.
  83.      *
  84.      * @param message to log
  85.      * @param exception log this cause
  86.      * @see org.apache.commons.logging.Log#debug(Object, Throwable)
  87.      */
  88.     @Override
  89.     public void debug(final Object message, final Throwable exception) {
  90.         log(Level.FINE, String.valueOf(message), exception);
  91.     }

  92.     /**
  93.      * Logs a message with {@link java.util.logging.Level#SEVERE}.
  94.      *
  95.      * @param message to log
  96.      * @see org.apache.commons.logging.Log#error(Object)
  97.      */
  98.     @Override
  99.     public void error(final Object message) {
  100.         log(Level.SEVERE, String.valueOf(message), null);
  101.     }

  102.     /**
  103.      * Logs a message with {@link java.util.logging.Level#SEVERE}.
  104.      *
  105.      * @param message to log
  106.      * @param exception log this cause
  107.      * @see org.apache.commons.logging.Log#error(Object, Throwable)
  108.      */
  109.     @Override
  110.     public void error(final Object message, final Throwable exception) {
  111.         log(Level.SEVERE, String.valueOf(message), exception);
  112.     }

  113.     /**
  114.      * Logs a message with {@link java.util.logging.Level#SEVERE}.
  115.      *
  116.      * @param message to log
  117.      * @see org.apache.commons.logging.Log#fatal(Object)
  118.      */
  119.     @Override
  120.     public void fatal(final Object message) {
  121.         log(Level.SEVERE, String.valueOf(message), null);
  122.     }

  123.     /**
  124.      * Logs a message with {@link java.util.logging.Level#SEVERE}.
  125.      *
  126.      * @param message to log
  127.      * @param exception log this cause
  128.      * @see org.apache.commons.logging.Log#fatal(Object, Throwable)
  129.      */
  130.     @Override
  131.     public void fatal(final Object message, final Throwable exception) {
  132.         log(Level.SEVERE, String.valueOf(message), exception);
  133.     }

  134.     /**
  135.      * Gets the class and method by looking at the stack trace for the
  136.      * first entry that is not this class.
  137.      */
  138.     private void getClassAndMethod() {
  139.         try {
  140.             final Throwable throwable = new Throwable();
  141.             throwable.fillInStackTrace();
  142.             final StringWriter stringWriter = new StringWriter();
  143.             final PrintWriter printWriter = new PrintWriter(stringWriter);
  144.             throwable.printStackTrace(printWriter);
  145.             final String traceString = stringWriter.toString();
  146.             final StringTokenizer tokenizer = new StringTokenizer(traceString, "\n");
  147.             tokenizer.nextToken();
  148.             String line = tokenizer.nextToken();
  149.             while (!line.contains(this.getClass().getName())) {
  150.                 line = tokenizer.nextToken();
  151.             }
  152.             while (line.contains(this.getClass().getName())) {
  153.                 line = tokenizer.nextToken();
  154.             }
  155.             final int start = line.indexOf("at ") + 3;
  156.             final int end = line.indexOf('(');
  157.             final String temp = line.substring(start, end);
  158.             final int lastPeriod = temp.lastIndexOf('.');
  159.             sourceClassName = temp.substring(0, lastPeriod);
  160.             sourceMethodName = temp.substring(lastPeriod + 1);
  161.         } catch (final Exception ex) {
  162.             // ignore - leave class and methodname unknown
  163.         }
  164.         classAndMethodFound = true;
  165.     }

  166.     /**
  167.      * Gets the native Logger instance we are using.
  168.      *
  169.      * @return the native Logger instance we are using.
  170.      */
  171.     public Logger getLogger() {
  172.         if (logger == null) {
  173.             logger = Logger.getLogger(name);
  174.         }
  175.         return logger;
  176.     }

  177.     /**
  178.      * Logs a message with {@link java.util.logging.Level#INFO}.
  179.      *
  180.      * @param message to log
  181.      * @see org.apache.commons.logging.Log#info(Object)
  182.      */
  183.     @Override
  184.     public void info(final Object message) {
  185.         log(Level.INFO, String.valueOf(message), null);
  186.     }

  187.     /**
  188.      * Logs a message with {@link java.util.logging.Level#INFO}.
  189.      *
  190.      * @param message to log
  191.      * @param exception log this cause
  192.      * @see org.apache.commons.logging.Log#info(Object, Throwable)
  193.      */
  194.     @Override
  195.     public void info(final Object message, final Throwable exception) {
  196.         log(Level.INFO, String.valueOf(message), exception);
  197.     }

  198.     /**
  199.      * Is debug logging currently enabled?
  200.      */
  201.     @Override
  202.     public boolean isDebugEnabled() {
  203.         return getLogger().isLoggable(Level.FINE);
  204.     }

  205.     /**
  206.      * Is error logging currently enabled?
  207.      */
  208.     @Override
  209.     public boolean isErrorEnabled() {
  210.         return getLogger().isLoggable(Level.SEVERE);
  211.     }

  212.     /**
  213.      * Is fatal logging currently enabled?
  214.      */
  215.     @Override
  216.     public boolean isFatalEnabled() {
  217.         return getLogger().isLoggable(Level.SEVERE);
  218.     }

  219.     /**
  220.      * Is info logging currently enabled?
  221.      */
  222.     @Override
  223.     public boolean isInfoEnabled() {
  224.         return getLogger().isLoggable(Level.INFO);
  225.     }

  226.     /**
  227.      * Is trace logging currently enabled?
  228.      */
  229.     @Override
  230.     public boolean isTraceEnabled() {
  231.         return getLogger().isLoggable(Level.FINEST);
  232.     }

  233.     /**
  234.      * Is warn logging currently enabled?
  235.      */
  236.     @Override
  237.     public boolean isWarnEnabled() {
  238.         return getLogger().isLoggable(Level.WARNING);
  239.     }

  240.     private void log( final Level level, final String msg, final Throwable ex ) {
  241.         if ( getLogger().isLoggable(level) ) {
  242.             final LogRecord record = new LogRecord(level, msg);
  243.             if ( !classAndMethodFound ) {
  244.                 getClassAndMethod();
  245.             }
  246.             record.setSourceClassName(sourceClassName);
  247.             record.setSourceMethodName(sourceMethodName);
  248.             if ( ex != null ) {
  249.                 record.setThrown(ex);
  250.             }
  251.             getLogger().log(record);
  252.         }
  253.     }

  254.     /**
  255.      * Logs a message with {@link java.util.logging.Level#FINEST}.
  256.      *
  257.      * @param message to log
  258.      * @see org.apache.commons.logging.Log#trace(Object)
  259.      */
  260.     @Override
  261.     public void trace(final Object message) {
  262.         log(Level.FINEST, String.valueOf(message), null);
  263.     }

  264.     /**
  265.      * Logs a message with {@link java.util.logging.Level#FINEST}.
  266.      *
  267.      * @param message to log
  268.      * @param exception log this cause
  269.      * @see org.apache.commons.logging.Log#trace(Object, Throwable)
  270.      */
  271.     @Override
  272.     public void trace(final Object message, final Throwable exception) {
  273.         log(Level.FINEST, String.valueOf(message), exception);
  274.     }

  275.     /**
  276.      * Logs a message with {@link java.util.logging.Level#WARNING}.
  277.      *
  278.      * @param message to log
  279.      * @see org.apache.commons.logging.Log#warn(Object)
  280.      */
  281.     @Override
  282.     public void warn(final Object message) {
  283.         log(Level.WARNING, String.valueOf(message), null);
  284.     }

  285.     /**
  286.      * Logs a message with {@link java.util.logging.Level#WARNING}.
  287.      *
  288.      * @param message to log
  289.      * @param exception log this cause
  290.      * @see org.apache.commons.logging.Log#warn(Object, Throwable)
  291.      */
  292.     @Override
  293.     public void warn(final Object message, final Throwable exception) {
  294.         log(Level.WARNING, String.valueOf(message), exception);
  295.     }
  296. }