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 2006-2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2013-2015 ForgeRock AS
026 */
027package org.opends.server.loggers;
028
029
030import org.forgerock.i18n.LocalizableMessage;
031import org.opends.messages.Severity;
032import org.opends.server.admin.std.server.ErrorLogPublisherCfg;
033import org.forgerock.opendj.config.server.ConfigException;
034import org.opends.server.core.ServerContext;
035import org.opends.server.types.DN;
036import org.opends.server.types.InitializationException;
037import org.opends.server.util.StaticUtils;
038import org.opends.server.util.TimeThread;
039
040/**
041 * This class provides an implementation of an error logger where only messages
042 * generated by a specified thread is actually logged.
043 */
044public class ThreadFilterTextErrorLogPublisher
045    extends ErrorLogPublisher<ErrorLogPublisherCfg>
046{
047  private Thread thread;
048
049  private TextWriter writer;
050
051  /**
052   * Construct a new instance with the provided settings.
053   *
054   * @param thread The thread to log from.
055   * @param writer The writer used to write the messages.
056   */
057  public ThreadFilterTextErrorLogPublisher(Thread thread,
058                                           TextWriter writer)
059  {
060    this.thread = thread;
061    this.writer = writer;
062  }
063
064  /** {@inheritDoc} */
065  @Override
066  public void initializeLogPublisher(ErrorLogPublisherCfg config, ServerContext serverContext)
067      throws ConfigException, InitializationException
068  {
069    // This class should only be used internally in the server and not be
070    // configurable via the admin framework.
071  }
072
073  /** {@inheritDoc} */
074  @Override
075  public void close()
076  {
077    writer.shutdown();
078  }
079
080  /** {@inheritDoc} */
081  @Override
082  public void log(String category, Severity severity,
083      LocalizableMessage message, Throwable exception)
084  {
085    if (message != null) {
086      Thread currentThread = Thread.currentThread();
087      if(this.thread.equals(currentThread) ||
088          this.thread.getThreadGroup().equals(currentThread.getThreadGroup()))
089      {
090        StringBuilder sb = new StringBuilder();
091        sb.append("[");
092        sb.append(TimeThread.getLocalTime());
093        sb.append("] category=").append(category).
094        append(" severity=").append(severity).
095        append(" msgID=").append(message.resourceName()).
096        append("-").append(message.ordinal()).
097        append(" msg=").append(message);
098        if (exception != null)
099        {
100          sb.append(" exception=").append(
101              StaticUtils.stackTraceToSingleLineString(exception));
102        }
103
104        this.writer.writeRecord(sb.toString());
105      }
106    }
107  }
108
109  /** {@inheritDoc} */
110  @Override
111  public boolean isEnabledFor(String category, Severity severity)
112  {
113    Thread currentThread = Thread.currentThread();
114    return this.thread.equals(currentThread)
115        || this.thread.getThreadGroup().equals(currentThread.getThreadGroup());
116  }
117
118  /** {@inheritDoc} */
119  @Override
120  public DN getDN()
121  {
122    // This class should only be used internally in the server and not be
123    // configurable via the admin framework.
124    return null;
125  }
126}