| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| FileFormatter |
|
| 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 FileFormatter extends Formatter { | |
| 47 | private SimpleDateFormat dateFormat; | |
| 48 | private FieldPosition pos; | |
| 49 | ||
| 50 | public FileFormatter() { | |
| 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(20); |
| 59 | 0 | dateFormat.format(new Date(record.getMillis()), buffer, pos); |
| 60 | ||
| 61 | 0 | buffer.append(' '); |
| 62 | ||
| 63 | 0 | if(record.getLevel() == Level.WARNING) { |
| 64 | 0 | buffer.append("Warning: "); |
| 65 | } | |
| 66 | 0 | else if(record.getLevel() == Level.SEVERE) { |
| 67 | 0 | buffer.append("Error: "); |
| 68 | } | |
| 69 | 0 | else if(record.getLevel() == Level.INFO) { |
| 70 | 0 | buffer.append("Info: "); |
| 71 | } | |
| 72 | else { | |
| 73 | 0 | buffer.append("Debug: "); |
| 74 | } | |
| 75 | ||
| 76 | 0 | buffer.append(record.getSourceClassName()); |
| 77 | 0 | buffer.append('.'); |
| 78 | 0 | buffer.append(record.getSourceMethodName()); |
| 79 | 0 | buffer.append("() "); |
| 80 | ||
| 81 | 0 | buffer.append(record.getMessage()); |
| 82 | 0 | buffer.append("\r\n"); |
| 83 | 0 | Throwable e = record.getThrown(); |
| 84 | 0 | if(e != null) { |
| 85 | 0 | exception(e, buffer); |
| 86 | } | |
| 87 | ||
| 88 | 0 | return buffer.toString(); |
| 89 | } | |
| 90 | ||
| 91 | private void exception(Throwable e, StringBuffer buffer) { | |
| 92 | 0 | buffer.append(e.getClass().getName()); |
| 93 | 0 | buffer.append(" Message: "); |
| 94 | 0 | buffer.append(e.getMessage()); |
| 95 | 0 | for(StackTraceElement trace : e.getStackTrace()) { |
| 96 | 0 | buffer.append('\t'); |
| 97 | 0 | buffer.append(trace.toString()); |
| 98 | 0 | buffer.append("\r\n"); |
| 99 | } | |
| 100 | 0 | Throwable t = e.getCause(); |
| 101 | 0 | if(t != null) { |
| 102 | 0 | buffer.append("\tCaused by:\r\n"); |
| 103 | 0 | exception(t, buffer); |
| 104 | } | |
| 105 | 0 | } |
| 106 | } |