| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| LogFormatter |
|
| 6.0;6 |
| 1 | /* | |
| 2 | StatCvs - CVS statistics generation | |
| 3 | Copyright (C) 2002 Lukasz Pekacki <lukasz@pekacki.de> | |
| 4 | http://statcvs.sf.net/ | |
| 5 | | |
| 6 | This library is free software; you can redistribute it and/or | |
| 7 | modify it under the terms of the GNU Lesser General Public | |
| 8 | License as published by the Free Software Foundation; either | |
| 9 | version 2.1 of the License, or (at your option) any later version. | |
| 10 | ||
| 11 | This library is distributed in the hope that it will be useful, | |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 14 | Lesser General Public License for more details. | |
| 15 | ||
| 16 | You should have received a copy of the GNU Lesser General Public | |
| 17 | License along with this library; if not, write to the Free Software | |
| 18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 19 | | |
| 20 | $RCSfile: LogFormatter.java,v $ | |
| 21 | Created on $Date: 2008/04/02 11:22:15 $ | |
| 22 | */ | |
| 23 | ||
| 24 | package net.sf.statcvs.util; | |
| 25 | ||
| 26 | import java.io.PrintWriter; | |
| 27 | import java.io.StringWriter; | |
| 28 | import java.util.logging.Formatter; | |
| 29 | import java.util.logging.Level; | |
| 30 | import java.util.logging.LogRecord; | |
| 31 | ||
| 32 | /** | |
| 33 | * Logging formatter for StatCvs | |
| 34 | * @author Lukasz Pekacki <lukasz@pekacki.de> | |
| 35 | * @version $Id: LogFormatter.java,v 1.9 2008/04/02 11:22:15 benoitx Exp $ | |
| 36 | */ | |
| 37 | 0 | public class LogFormatter extends Formatter { |
| 38 | ||
| 39 | 0 | private final String lineSeparator = System.getProperty("line.separator"); |
| 40 | ||
| 41 | /** | |
| 42 | * @see java.util.logging.Formatter#format(LogRecord) | |
| 43 | */ | |
| 44 | public String format(final LogRecord record) { | |
| 45 | 0 | final StringBuffer sb = new StringBuffer(); |
| 46 | 0 | if (record.getLevel().intValue() < Level.INFO.intValue()) { |
| 47 | 0 | sb.append(record.getLevel().getLocalizedName()); |
| 48 | 0 | sb.append(" "); |
| 49 | 0 | if (record.getSourceClassName() != null) { |
| 50 | 0 | String className = record.getSourceClassName(); |
| 51 | 0 | className = className.substring(7); |
| 52 | 0 | sb.append(className); |
| 53 | 0 | } else { |
| 54 | 0 | sb.append(record.getLoggerName()); |
| 55 | } | |
| 56 | 0 | if (record.getSourceMethodName() != null) { |
| 57 | 0 | sb.append(" "); |
| 58 | 0 | sb.append(record.getSourceMethodName()); |
| 59 | } | |
| 60 | 0 | sb.append("(): "); |
| 61 | } | |
| 62 | 0 | final String message = formatMessage(record); |
| 63 | 0 | sb.append(message); |
| 64 | 0 | sb.append(lineSeparator); |
| 65 | 0 | if (record.getThrown() != null) { |
| 66 | try { | |
| 67 | 0 | final StringWriter sw = new StringWriter(); |
| 68 | 0 | final PrintWriter pw = new PrintWriter(sw); |
| 69 | 0 | record.getThrown().printStackTrace(pw); |
| 70 | 0 | pw.close(); |
| 71 | 0 | sb.append(sw.toString()); |
| 72 | 0 | } catch (final Exception ex) { |
| 73 | 0 | System.err.println("Error formatting logmessage! " + ex.toString()); |
| 74 | 0 | } |
| 75 | } | |
| 76 | 0 | return sb.toString(); |
| 77 | } | |
| 78 | ||
| 79 | } |