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 18package java.util.logging; 19 20import java.io.PrintWriter; 21import java.io.StringWriter; 22import java.text.MessageFormat; 23import java.util.Date; 24import libcore.io.IoUtils; 25 26/** 27 * {@code SimpleFormatter} can be used to print a summary of the information 28 * contained in a {@code LogRecord} object in a human readable format. 29 */ 30public class SimpleFormatter extends Formatter { 31 /** 32 * Constructs a new {@code SimpleFormatter}. 33 */ 34 public SimpleFormatter() { 35 } 36 37 /** 38 * Converts a {@link LogRecord} object into a human readable string 39 * representation. 40 * 41 * @param r 42 * the log record to be formatted into a string. 43 * @return the formatted string. 44 */ 45 @Override 46 public String format(LogRecord r) { 47 StringBuilder sb = new StringBuilder(); 48 sb.append(MessageFormat.format("{0, date} {0, time} ", 49 new Object[] { new Date(r.getMillis()) })); 50 sb.append(r.getSourceClassName()).append(" "); 51 sb.append(r.getSourceMethodName()).append(System.lineSeparator()); 52 sb.append(r.getLevel().getName()).append(": "); 53 sb.append(formatMessage(r)).append(System.lineSeparator()); 54 if (r.getThrown() != null) { 55 sb.append("Throwable occurred: "); 56 Throwable t = r.getThrown(); 57 PrintWriter pw = null; 58 try { 59 StringWriter sw = new StringWriter(); 60 pw = new PrintWriter(sw); 61 t.printStackTrace(pw); 62 sb.append(sw.toString()); 63 } finally { 64 IoUtils.closeQuietly(pw); 65 } 66 } 67 return sb.toString(); 68 } 69} 70