1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* ident "%Z%%M% %I% %E% SMI" */ 27 28 package com.sun.solaris.service.logging; 29 30 import java.io.PrintWriter; 31 import java.io.StringWriter; 32 import java.text.*; 33 import java.util.*; 34 import java.util.logging.Formatter; 35 import java.util.logging.LogRecord; 36 37 import com.sun.solaris.service.exception.SuccinctStackTraceFormatter; 38 39 /** 40 * Formats a LogRecord in a human-readable, <code>syslog</code>-like 41 * format, and is intended for use with non-syslog handlers, such as 42 * FileHandler. 43 * 44 * Multi-line messages are automatically indented by four spaces to make 45 * subsequent lines easier to differentiate from new records. 46 */ 47 public class SysloglikeFormatter extends Formatter { 48 /** 49 * The date set for each published Record. 50 */ 51 private Date date = new Date(); 52 53 /** 54 * Format string for published dates. 55 */ 56 private final static String dateFormat = 57 "MMM d kk:mm:ss"; 58 59 /** 60 * For published dates, the formatter. 61 */ 62 private DateFormat dateFormatter; 63 64 /** 65 * For published dates, the argument to date formatter. 66 */ 67 private Object args[] = { date }; 68 69 /** 70 * Line separator string. 71 */ 72 private String lineSeparator = 73 System.getProperty("line.separator"); 74 75 /** 76 * Flag to set whether log records should indicate the name of 77 * the class generating the record, if possible. (default 78 * false) 79 */ 80 private boolean useClassName = false; 81 82 /** 83 * Flag to set whether log records should indicate the record's 84 * logger, if useClassName isn't set and the class name was 85 * available. (default false) 86 */ 87 private boolean useLoggerName = false; 88 89 /** 90 * Flag to set whether log records should indicate the last 91 * component of the record's logger name, if useLoggerName isn't 92 * set. (default true) 93 */ 94 private boolean useShortLoggerName = true; 95 96 /** 97 * Flag to set whether log records should indicate the method 98 * used to invoke the logger, if available. (default false) 99 */ 100 private boolean useMethodName = false; 101 102 /** 103 * Flag to set whether each record should be split into two 104 * lines such that the severity and message are on a line by 105 * themselves. (default false) 106 */ 107 private boolean useTwoLineStyle = false; 108 109 /** 110 * Format the given LogRecord. 111 * @param record the log record to be formatted. 112 * @return a formatted log record. 113 */ format(LogRecord record)114 public synchronized String format(LogRecord record) 115 { 116 StringBuffer sb = new StringBuffer(); 117 118 date.setTime(record.getMillis()); 119 StringBuffer text = new StringBuffer(); 120 if (dateFormatter == null) 121 dateFormatter = new SimpleDateFormat(dateFormat); 122 sb.append(dateFormatter.format(date)); 123 124 if (record.getSourceClassName() != null && useClassName) { 125 sb.append(" "); 126 sb.append(record.getSourceClassName()); 127 } else if (useLoggerName) { 128 if (record.getLoggerName() != null) { 129 sb.append(" "); 130 sb.append(record.getLoggerName()); 131 } 132 } else if (useShortLoggerName) { 133 String loggerName = record.getLoggerName(); 134 135 if (loggerName != null) { 136 sb.append(" "); 137 int lastDot = loggerName.lastIndexOf('.'); 138 if (lastDot >= 0) 139 loggerName = loggerName.substring( 140 lastDot + 1); 141 sb.append(loggerName); 142 } 143 } 144 145 if (record.getSourceMethodName() != null && useMethodName) { 146 sb.append(" "); 147 sb.append(record.getSourceMethodName()); 148 } 149 if (useTwoLineStyle) 150 sb.append(lineSeparator); 151 else 152 sb.append(" "); 153 154 String message = formatMessage(record); 155 message = message.replaceAll("\n", lineSeparator + " "); 156 157 sb.append(record.getLevel()).toString(); 158 sb.append(": "); 159 sb.append(message); 160 if (record.getThrown() != null) { 161 sb.append(" "); 162 sb.append(SuccinctStackTraceFormatter 163 .formatWithDescription(record.getThrown(), 164 "with tracing information: ").toString()); 165 } 166 sb.append(lineSeparator); 167 168 return sb.toString(); 169 } 170 } 171