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 * See LICENSE.txt included in this distribution for the specific 9 * language governing permissions and limitations under the License. 10 * 11 * When distributing Covered Code, include this CDDL HEADER in each 12 * file and include the License file at LICENSE.txt. 13 * If applicable, add the following below this CDDL HEADER, with the 14 * fields enclosed by brackets "[]" replaced with your own identifying 15 * information: Portions Copyright [yyyy] [name of copyright owner] 16 * 17 * CDDL HEADER END 18 */ 19 20 /* 21 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. 22 */ 23 24 package org.opensolaris.opengrok; 25 26 import java.util.Date; 27 import java.util.logging.Formatter; 28 import java.util.logging.LogRecord; 29 30 /** 31 * Opengrok logfile formatter 32 * Creates a logentry in the logfile on the following format 33 * [#|YYYY-MM-DD HH:MM:ss.SSSZ |<loglevel>|<version>|OG|T=<threadnumber>| 34 * <Class.method>: <logmessage> |#] 35 * @author Jan S Berg 36 */ 37 final public class FileLogFormatter extends Formatter { 38 39 private final java.text.SimpleDateFormat formatter = 40 new java.text.SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss.SSSZ"); 41 private static final String lineSeparator = System. 42 getProperty("line.separator"); 43 ts(Date date)44 private String ts(Date date) { 45 return formatter.format(date); 46 } 47 classNameOnly(String name)48 private String classNameOnly(String name) { 49 int index = name.lastIndexOf('.') + 1; 50 return name.substring(index); 51 } 52 format(LogRecord record)53 public String format(LogRecord record) { 54 StringBuilder sb = new StringBuilder(); 55 sb.append("[#|"); 56 sb.append(ts(new Date(record.getMillis()))); 57 sb.append(" |"); 58 String loglevel = record.getLevel().getName(); 59 sb.append(loglevel); 60 sb.append("|"); 61 sb.append("V"); 62 sb.append(Info.getVersion()); 63 sb.append("|OG|"); 64 sb.append("T="); 65 sb.append(record.getThreadID()); 66 sb.append("| "); 67 sb.append(classNameOnly(record.getSourceClassName())); 68 sb.append('.'); 69 sb.append(formatMessage(record)); 70 sb.append(": "); 71 sb.append(record.getMessage()); 72 Throwable thrown = record.getThrown(); 73 if (null != thrown) { 74 sb.append(lineSeparator); 75 java.io.ByteArrayOutputStream ba=new java.io.ByteArrayOutputStream(); 76 thrown.printStackTrace(new java.io.PrintStream(ba, true)); 77 sb.append(ba.toString()); 78 } 79 sb.append(" |#]"); 80 sb.append(lineSeparator); 81 return sb.toString(); 82 } 83 } 84