001/* 002 * CDDL HEADER START 003 * 004 * The contents of this file are subject to the terms of the 005 * Common Development and Distribution License, Version 1.0 only 006 * (the "License"). You may not use this file except in compliance 007 * with the License. 008 * 009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt 010 * or http://forgerock.org/license/CDDLv1.0.html. 011 * See the License for the specific language governing permissions 012 * and limitations under the License. 013 * 014 * When distributing Covered Code, include this CDDL HEADER in each 015 * file and include the License file at legal-notices/CDDLv1_0.txt. 016 * If applicable, add the following below this CDDL HEADER, with the 017 * fields enclosed by brackets "[]" replaced with your own identifying 018 * information: 019 * Portions Copyright [yyyy] [name of copyright owner] 020 * 021 * CDDL HEADER END 022 * 023 * 024 * Copyright 2014 ForgeRock AS. 025 */ 026package org.opends.server.loggers; 027 028import java.text.DateFormat; 029import java.text.SimpleDateFormat; 030import java.util.Date; 031import java.util.logging.Formatter; 032import java.util.logging.LogRecord; 033 034import org.opends.server.util.StaticUtils; 035 036/** 037 * A formatter to replace default format of java.util.logging loggers. 038 * <p> 039 * With JDK 7+, it is possible to pass in the format from the 040 * "java.util.logging.SimpleFormatter.format" parameter to the JVM. Use the 041 * parameter instead of this class when JDK6 is not supported any more. 042 */ 043public final class JDKLoggingFormater extends Formatter 044{ 045 046 /** Use one formatter per thread as DateFormat is not thread-safe. */ 047 private static final ThreadLocal<DateFormat> DATE_FORMAT = 048 new ThreadLocal<DateFormat>() 049 { 050 @Override 051 protected DateFormat initialValue() 052 { 053 return new SimpleDateFormat("[dd/MM/yyyy:HH:mm:ss Z]"); 054 } 055 }; 056 057 /** {@inheritDoc} */ 058 @Override 059 public String format(LogRecord record) 060 { 061 StringBuilder b = new StringBuilder(); 062 b.append(DATE_FORMAT.get().format(new Date(record.getMillis()))); 063 b.append(" category=").append(LoggingCategoryNames.getCategoryName(record.getLoggerName())); 064 b.append(" seq=").append(record.getSequenceNumber()); 065 b.append(" severity=").append(record.getLevel()); 066 b.append(" msg=").append(record.getMessage()); 067 if (record.getThrown() != null) 068 { 069 b.append(" exception=").append( 070 StaticUtils.stackTraceToSingleLineString(record.getThrown())); 071 } 072 b.append("\n"); 073 return b.toString(); 074 } 075 076}