001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.log4j.lf5.util; 018 019import org.apache.log4j.lf5.LogLevel; 020import org.apache.log4j.lf5.LogRecord; 021 022import java.io.PrintWriter; 023import java.io.StringWriter; 024 025/** 026 * <p>A LogRecord to be used with the LogMonitorAdapter</p> 027 * 028 * @author Richard Hurst 029 */ 030 031// Contributed by ThoughtWorks Inc. 032 033public class AdapterLogRecord extends LogRecord { 034 //-------------------------------------------------------------------------- 035 // Constants: 036 //-------------------------------------------------------------------------- 037 038 //-------------------------------------------------------------------------- 039 // Protected Variables: 040 //-------------------------------------------------------------------------- 041 042 //-------------------------------------------------------------------------- 043 // Private Variables: 044 //-------------------------------------------------------------------------- 045 private static LogLevel severeLevel = null; 046 047 private static StringWriter sw = new StringWriter(); 048 private static PrintWriter pw = new PrintWriter(sw); 049 050 //-------------------------------------------------------------------------- 051 // Constructors: 052 //-------------------------------------------------------------------------- 053 public AdapterLogRecord() { 054 super(); 055 } 056 057 //-------------------------------------------------------------------------- 058 // Public Methods: 059 //-------------------------------------------------------------------------- 060 public void setCategory(String category) { 061 super.setCategory(category); 062 super.setLocation(getLocationInfo(category)); 063 } 064 065 public boolean isSevereLevel() { 066 if (severeLevel == null) return false; 067 return severeLevel.equals(getLevel()); 068 } 069 070 public static void setSevereLevel(LogLevel level) { 071 severeLevel = level; 072 } 073 074 public static LogLevel getSevereLevel() { 075 return severeLevel; 076 } 077 078 //-------------------------------------------------------------------------- 079 // Protected Methods: 080 //-------------------------------------------------------------------------- 081 protected String getLocationInfo(String category) { 082 String stackTrace = stackTraceToString(new Throwable()); 083 String line = parseLine(stackTrace, category); 084 return line; 085 } 086 087 protected String stackTraceToString(Throwable t) { 088 String s = null; 089 090 synchronized (sw) { 091 t.printStackTrace(pw); 092 s = sw.toString(); 093 sw.getBuffer().setLength(0); 094 } 095 096 return s; 097 } 098 099 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