001/*
002 * Copyright 2010-2020 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2010-2020 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2010-2020 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.util;
037
038
039
040import java.io.Serializable;
041import java.text.SimpleDateFormat;
042import java.util.Date;
043import java.util.logging.Formatter;
044import java.util.logging.LogRecord;
045
046
047
048/**
049 * This class provides a log formatter for use in the Java logging framework
050 * that may be used to minimize the formatting applied to log messages.
051 */
052@NotMutable()
053@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
054public final class MinimalLogFormatter
055       extends Formatter
056       implements Serializable
057{
058  /**
059   * The default format string that will be used for generating timestamps.
060   */
061  public static final String DEFAULT_TIMESTAMP_FORMAT =
062       "'['dd/MMM/yyyy:HH:mm:ss Z']'";
063
064
065
066  /**
067   * The set of thread-local date formatters that will be used for generating
068   * message timestamps.
069   */
070  private static final ThreadLocal<SimpleDateFormat> DATE_FORMATTERS =
071       new ThreadLocal<>();
072
073
074
075  /**
076   * The set of thread-local buffers that will be used for generating the
077   * message.
078   */
079  private static final ThreadLocal<StringBuilder> BUFFERS = new ThreadLocal<>();
080
081
082
083  /**
084   * The serial version UID for this serializable class.
085   */
086  private static final long serialVersionUID = -2884878613513769233L;
087
088
089
090  // Indicates whether to include the log level in the message header.
091  private final boolean includeLevel;
092
093  // Indicates whether to include a line break after the header.
094  private final boolean lineBreakAfterHeader;
095
096  // Indicates whether to include a line break after the message.
097  private final boolean lineBreakAfterMessage;
098
099  // The format string that will be used to generate timestamps, if appropriate.
100  private final String timestampFormat;
101
102
103
104  /**
105   * Creates a new instance of this log formatter with the default settings.
106   * Generated messages will include a timestamp generated using the format
107   * string "{@code '['dd/MMM/yyyy:HH:mm:ss Z']'}", will not include the log
108   * level, and will not include a line break after the timestamp or the
109   * message.
110   */
111  public MinimalLogFormatter()
112  {
113    this(DEFAULT_TIMESTAMP_FORMAT, false, false, false);
114  }
115
116
117
118  /**
119   * Creates a new instance of this log formatter with the provided
120   * configuration.
121   *
122   * @param  timestampFormat        The format string used to generate
123   *                                timestamps.  If this is {@code null}, then
124   *                                timestamps will not be included in log
125   *                                messages.
126   * @param  includeLevel           Indicates whether to include the log level
127   *                                in the generated messages.
128   * @param  lineBreakAfterHeader   Indicates whether to insert a line break
129   *                                after the timestamp and/or log level.
130   * @param  lineBreakAfterMessage  Indicates whether to insert aline break
131   *                                after the generated message.
132   */
133  public MinimalLogFormatter(final String timestampFormat,
134                             final boolean includeLevel,
135                             final boolean lineBreakAfterHeader,
136                             final boolean lineBreakAfterMessage)
137  {
138    this.timestampFormat       = timestampFormat;
139    this.includeLevel          = includeLevel;
140    this.lineBreakAfterHeader  = lineBreakAfterHeader;
141    this.lineBreakAfterMessage = lineBreakAfterMessage;
142  }
143
144
145
146  /**
147   * Formats the provided log record.
148   *
149   * @param  record  The log record to be formatted.
150   *
151   * @return  A string containing the formatted log record.
152   */
153  @Override()
154  public String format(final LogRecord record)
155  {
156    StringBuilder b = BUFFERS.get();
157    if (b == null)
158    {
159      b = new StringBuilder();
160      BUFFERS.set(b);
161    }
162    else
163    {
164      b.setLength(0);
165    }
166
167    if (timestampFormat != null)
168    {
169      SimpleDateFormat f = DATE_FORMATTERS.get();
170      if (f == null)
171      {
172        f = new SimpleDateFormat(timestampFormat);
173        DATE_FORMATTERS.set(f);
174      }
175
176      b.append(f.format(new Date()));
177    }
178
179    if (includeLevel)
180    {
181      if (b.length() > 0)
182      {
183        b.append(' ');
184      }
185
186      b.append(record.getLevel().toString());
187    }
188
189    if (lineBreakAfterHeader)
190    {
191      b.append(StaticUtils.EOL);
192    }
193    else if (b.length() > 0)
194    {
195      b.append(' ');
196    }
197
198    b.append(formatMessage(record));
199
200    if (lineBreakAfterMessage)
201    {
202      b.append(StaticUtils.EOL);
203    }
204
205    return b.toString();
206  }
207}