SimpleFileLogFormatter.java revision 1190
1062N/A/*
1062N/A * CDDL HEADER START
1062N/A *
1062N/A * The contents of this file are subject to the terms of the
1062N/A * Common Development and Distribution License (the "License").
1062N/A * You may not use this file except in compliance with the License.
1062N/A *
1062N/A * See LICENSE.txt included in this distribution for the specific
1062N/A * language governing permissions and limitations under the License.
1062N/A *
1062N/A * When distributing Covered Code, include this CDDL HEADER in each
1062N/A * file and include the License file at LICENSE.txt.
1062N/A * If applicable, add the following below this CDDL HEADER, with the
1062N/A * fields enclosed by brackets "[]" replaced with your own identifying
1062N/A * information: Portions Copyright [yyyy] [name of copyright owner]
1062N/A *
1062N/A * CDDL HEADER END
1062N/A */
1062N/A
1062N/A/*
1062N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
1062N/A */
1062N/A
1062N/Apackage org.opensolaris.opengrok;
1062N/A
1062N/Aimport java.util.Date;
1062N/Aimport java.util.logging.Formatter;
1178N/Aimport java.util.logging.Level;
1062N/Aimport java.util.logging.LogRecord;
1062N/A
1062N/A/**
1062N/A * Opengrok logfile formatter
1062N/A * Creates a logentry in the logfile on the following format
1062N/A * YYYY-MM-DD HH:MM:ss.SSSZ <loglevel> t<threadnumber> <Class.method>: <logmessage>
1062N/A * @author Lubos Kosco
1062N/A */
1062N/Afinal public class SimpleFileLogFormatter extends Formatter {
1190N/A
1062N/A private final java.text.SimpleDateFormat formatter =
1062N/A new java.text.SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss.SSSZ");
1062N/A private final static String lineSeparator = System.
1062N/A getProperty("line.separator");
1062N/A Date dat = new Date();
1190N/A
1062N/A private String ts(Date date) {
1062N/A return formatter.format(date);
1062N/A }
1190N/A
1062N/A private String classNameOnly(String name) {
1062N/A int index = name.lastIndexOf('.') + 1;
1062N/A return name.substring(index);
1062N/A }
1190N/A
1062N/A @Override
1062N/A public String format(LogRecord record) {
1062N/A StringBuilder sb = new StringBuilder();
1062N/A dat.setTime(record.getMillis());
1062N/A sb.append(ts(dat));
1062N/A sb.append(" ");
1062N/A String loglevel = record.getLevel().getName();
1062N/A sb.append(loglevel);
1062N/A sb.append(" ");
1062N/A sb.append("t");
1062N/A sb.append(record.getThreadID());
1062N/A sb.append(" ");
1062N/A sb.append(classNameOnly(record.getSourceClassName()));
1062N/A sb.append('.');
1062N/A sb.append(record.getSourceMethodName());
1062N/A sb.append(": ");
1062N/A sb.append(formatMessage(record));
1062N/A Throwable thrown = record.getThrown();
1178N/A if (null != thrown && record.getLevel().intValue() < Level.CONFIG.intValue()) {
1062N/A sb.append(lineSeparator);
1062N/A java.io.ByteArrayOutputStream ba=new java.io.ByteArrayOutputStream();
1062N/A thrown.printStackTrace(new java.io.PrintStream(ba, true));
1062N/A sb.append(ba.toString());
1190N/A }
1062N/A sb.append(lineSeparator);
1062N/A return sb.toString();
1062N/A }
1062N/A}