| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ConsoleFormatter |
|
| 0.0;0 |
| 1 | /* | |
| 2 | Copyright (c) 2006, University of Tromsø | |
| 3 | All rights reserved. | |
| 4 | ||
| 5 | Redistribution and use in source and binary forms, with or without | |
| 6 | modification, are permitted provided that the following conditions are met: | |
| 7 | ||
| 8 | * Redistributions of source code must retain the above copyright notice, this list | |
| 9 | of conditions and the following disclaimer. | |
| 10 | ||
| 11 | * Redistributions in binary form must reproduce the above copyright notice, this | |
| 12 | list of conditions and the following disclaimer in the documentation and/or other | |
| 13 | materials provided with the distribution. | |
| 14 | ||
| 15 | * Neither the name of the University of Tromsø nor the names of its contributors may | |
| 16 | be used to endorse or promote products derived from this software without specific | |
| 17 | prior written permission. | |
| 18 | ||
| 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY | |
| 20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
| 21 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT | |
| 22 | SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
| 23 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED | |
| 24 | TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR | |
| 25 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 26 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
| 27 | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | |
| 28 | DAMAGE. | |
| 29 | */ | |
| 30 | ||
| 31 | package argos.logging; | |
| 32 | ||
| 33 | import java.text.FieldPosition; | |
| 34 | import java.text.SimpleDateFormat; | |
| 35 | import java.util.Date; | |
| 36 | import java.util.Locale; | |
| 37 | import java.util.logging.Formatter; | |
| 38 | import java.util.logging.Level; | |
| 39 | import java.util.logging.LogRecord; | |
| 40 | ||
| 41 | /** | |
| 42 | * Created on 25. juni 2006, 00:02 | |
| 43 | * | |
| 44 | * @author Dan Peder Eriksen | |
| 45 | */ | |
| 46 | public class ConsoleFormatter extends Formatter { | |
| 47 | private SimpleDateFormat dateFormat; | |
| 48 | private FieldPosition pos; | |
| 49 | ||
| 50 | public ConsoleFormatter() { | |
| 51 | 0 | super(); |
| 52 | 0 | dateFormat = new SimpleDateFormat("[HH:mm:ss] ", Locale.getDefault()); |
| 53 | 0 | pos = new FieldPosition(0); |
| 54 | 0 | } |
| 55 | ||
| 56 | @Override | |
| 57 | public String format(LogRecord record) { | |
| 58 | 0 | StringBuffer buffer = new StringBuffer(); |
| 59 | 0 | dateFormat.format(new Date(record.getMillis()), buffer, pos); |
| 60 | ||
| 61 | 0 | buffer.append('['); |
| 62 | 0 | String temp = record.getSourceClassName(); |
| 63 | 0 | temp = temp.substring(temp.lastIndexOf('.') + 1); |
| 64 | 0 | buffer.append(temp); |
| 65 | 0 | buffer.append("] "); |
| 66 | 0 | if(record.getLevel() == Level.WARNING) { |
| 67 | 0 | buffer.append("Warning: "); |
| 68 | } | |
| 69 | 0 | else if(record.getLevel() == Level.SEVERE) { |
| 70 | 0 | buffer.append("Severe: "); |
| 71 | } | |
| 72 | 0 | else if(record.getLevel() == Level.INFO) { |
| 73 | 0 | buffer.append("Info: "); |
| 74 | } | |
| 75 | ||
| 76 | 0 | buffer.append(record.getMessage()); |
| 77 | 0 | buffer.append("\r\n"); |
| 78 | ||
| 79 | 0 | if(record.getThrown() != null) { |
| 80 | 0 | printStackTrace(buffer, record.getThrown()); |
| 81 | } | |
| 82 | ||
| 83 | 0 | return buffer.toString(); |
| 84 | } | |
| 85 | ||
| 86 | private void printStackTrace(StringBuffer sb, Throwable t) { | |
| 87 | 0 | StackTraceElement[] trace = t.getStackTrace(); |
| 88 | 0 | for (int i=0; i < trace.length; i++) { |
| 89 | 0 | sb.append("\tat " + trace[i]); |
| 90 | 0 | sb.append("\r\n"); |
| 91 | } | |
| 92 | ||
| 93 | 0 | Throwable cause = t.getCause(); |
| 94 | 0 | if (cause != null) { |
| 95 | 0 | sb.append("Caused by: "); |
| 96 | 0 | sb.append(cause.getClass().getName()); |
| 97 | 0 | sb.append(": "); |
| 98 | 0 | sb.append(cause.getMessage()); |
| 99 | 0 | sb.append("\r\n"); |
| 100 | 0 | printStackTrace(sb, cause); |
| 101 | } | |
| 102 | 0 | } |
| 103 | } |