AdapterLogRecord xref

View Javadoc

    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.log4j.lf5.util;
    18
    19  import org.apache.log4j.lf5.LogLevel;
    20  import org.apache.log4j.lf5.LogRecord;
    21
    22  import java.io.PrintWriter;
    23  import java.io.StringWriter;
    24
    25  /**
    26   * <p>A LogRecord to be used with the LogMonitorAdapter</p>
    27   *
    28   * @author Richard Hurst
    29   */
    30
    31  // Contributed by ThoughtWorks Inc.
    32
    33  public class AdapterLogRecord extends LogRecord {
    34    //--------------------------------------------------------------------------
    35    //   Constants:
    36    //--------------------------------------------------------------------------
    37
    38    //--------------------------------------------------------------------------
    39    //   Protected Variables:
    40    //--------------------------------------------------------------------------
    41
    42    //--------------------------------------------------------------------------
    43    //   Private Variables:
    44    //--------------------------------------------------------------------------
    45    private static LogLevel severeLevel = null;
    46
    47    private static StringWriter sw = new StringWriter();
    48    private static PrintWriter pw = new PrintWriter(sw);
    49
    50    //--------------------------------------------------------------------------
    51    //   Constructors:
    52    //--------------------------------------------------------------------------
    53    public AdapterLogRecord() {
    54      super();
    55    }
    56
    57    //--------------------------------------------------------------------------
    58    //   Public Methods:
    59    //--------------------------------------------------------------------------
    60    public void setCategory(String category) {
    61      super.setCategory(category);
    62      super.setLocation(getLocationInfo(category));
    63    }
    64
    65    public boolean isSevereLevel() {
    66      if (severeLevel == null) return false;
    67      return severeLevel.equals(getLevel());
    68    }
    69
    70    public static void setSevereLevel(LogLevel level) {
    71      severeLevel = level;
    72    }
    73
    74    public static LogLevel getSevereLevel() {
    75      return severeLevel;
    76    }
    77
    78    //--------------------------------------------------------------------------
    79    //   Protected Methods:
    80    //--------------------------------------------------------------------------
    81    protected String getLocationInfo(String category) {
    82      String stackTrace = stackTraceToString(new Throwable());
    83      String line = parseLine(stackTrace, category);
    84      return line;
    85    }
    86
    87    protected String stackTraceToString(Throwable t) {
    88      String s = null;
    89
    90      synchronized (sw) {
    91        t.printStackTrace(pw);
    92        s = sw.toString();
    93        sw.getBuffer().setLength(0);
    94      }
    95
    96      return s;
    97    }
    98
    99    protected String parseLine(String trace, String category) {
    100     int index = trace.indexOf(category);
    101     if (index == -1) return null;
    102     trace = trace.substring(index);
    103     trace = trace.substring(0, trace.indexOf(")") + 1);
    104     return trace;
    105   }
    106   //--------------------------------------------------------------------------
    107   //   Private Methods:
    108   //--------------------------------------------------------------------------
    109
    110   //--------------------------------------------------------------------------
    111   //   Nested Top-Level Classes or Interfaces
    112   //--------------------------------------------------------------------------
    113 }
    114