001/*
002 * Copyright 2010-2022 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2010-2022 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-2022 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  @NotNull 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  @NotNull 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  @NotNull private static final ThreadLocal<StringBuilder> BUFFERS =
080       new ThreadLocal<>();
081
082
083
084  /**
085   * The serial version UID for this serializable class.
086   */
087  private static final long serialVersionUID = -2884878613513769233L;
088
089
090
091  // Indicates whether to include the log level in the message header.
092  private final boolean includeLevel;
093
094  // Indicates whether to include a line break after the header.
095  private final boolean lineBreakAfterHeader;
096
097  // Indicates whether to include a line break after the message.
098  private final boolean lineBreakAfterMessage;
099
100  // The format string that will be used to generate timestamps, if appropriate.
101  @Nullable private final String timestampFormat;
102
103
104
105  /**
106   * Creates a new instance of this log formatter with the default settings.
107   * Generated messages will include a timestamp generated using the format
108   * string "{@code '['dd/MMM/yyyy:HH:mm:ss Z']'}", will not include the log
109   * level, and will not include a line break after the timestamp or the
110   * message.
111   */
112  public MinimalLogFormatter()
113  {
114    this(DEFAULT_TIMESTAMP_FORMAT, false, false, false);
115  }
116
117
118
119  /**
120   * Creates a new instance of this log formatter with the provided
121   * configuration.
122   *
123   * @param  timestampFormat        The format string used to generate
124   *                                timestamps.  If this is {@code null}, then
125   *                                timestamps will not be included in log
126   *                                messages.
127   * @param  includeLevel           Indicates whether to include the log level
128   *                                in the generated messages.
129   * @param  lineBreakAfterHeader   Indicates whether to insert a line break
130   *                                after the timestamp and/or log level.
131   * @param  lineBreakAfterMessage  Indicates whether to insert aline break
132   *                                after the generated message.
133   */
134  public MinimalLogFormatter(@Nullable final String timestampFormat,
135                             final boolean includeLevel,
136                             final boolean lineBreakAfterHeader,
137                             final boolean lineBreakAfterMessage)
138  {
139    this.timestampFormat       = timestampFormat;
140    this.includeLevel          = includeLevel;
141    this.lineBreakAfterHeader  = lineBreakAfterHeader;
142    this.lineBreakAfterMessage = lineBreakAfterMessage;
143  }
144
145
146
147  /**
148   * Formats the provided log record.
149   *
150   * @param  record  The log record to be formatted.
151   *
152   * @return  A string containing the formatted log record.
153   */
154  @Override()
155  @NotNull()
156  public String format(@NotNull final LogRecord record)
157  {
158    StringBuilder b = BUFFERS.get();
159    if (b == null)
160    {
161      b = new StringBuilder();
162      BUFFERS.set(b);
163    }
164    else
165    {
166      b.setLength(0);
167    }
168
169    if (timestampFormat != null)
170    {
171      SimpleDateFormat f = DATE_FORMATTERS.get();
172      if (f == null)
173      {
174        f = new SimpleDateFormat(timestampFormat);
175        DATE_FORMATTERS.set(f);
176      }
177
178      b.append(f.format(new Date()));
179    }
180
181    if (includeLevel)
182    {
183      if (b.length() > 0)
184      {
185        b.append(' ');
186      }
187
188      b.append(record.getLevel().toString());
189    }
190
191    if (lineBreakAfterHeader)
192    {
193      b.append(StaticUtils.EOL);
194    }
195    else if (b.length() > 0)
196    {
197      b.append(' ');
198    }
199
200    b.append(formatMessage(record));
201
202    if (lineBreakAfterMessage)
203    {
204      b.append(StaticUtils.EOL);
205    }
206
207    return b.toString();
208  }
209}