001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2006-2008 Sun Microsystems, Inc.
015 * Portions Copyright 2013-2016 ForgeRock AS.
016 */
017package org.opends.server.loggers;
018
019
020import org.forgerock.i18n.LocalizableMessage;
021import org.opends.messages.Severity;
022import org.forgerock.opendj.server.config.server.ErrorLogPublisherCfg;
023import org.forgerock.opendj.config.server.ConfigException;
024import org.opends.server.core.ServerContext;
025import org.forgerock.opendj.ldap.DN;
026import org.opends.server.types.InitializationException;
027import org.opends.server.util.StaticUtils;
028import org.opends.server.util.TimeThread;
029
030/**
031 * This class provides an implementation of an error logger where only messages
032 * generated by a specified thread is actually logged.
033 */
034public class ThreadFilterTextErrorLogPublisher
035    extends ErrorLogPublisher<ErrorLogPublisherCfg>
036{
037  private Thread thread;
038
039  private TextWriter writer;
040
041  /**
042   * Construct a new instance with the provided settings.
043   *
044   * @param thread The thread to log from.
045   * @param writer The writer used to write the messages.
046   */
047  public ThreadFilterTextErrorLogPublisher(Thread thread,
048                                           TextWriter writer)
049  {
050    this.thread = thread;
051    this.writer = writer;
052  }
053
054  @Override
055  public void initializeLogPublisher(ErrorLogPublisherCfg config, ServerContext serverContext)
056      throws ConfigException, InitializationException
057  {
058    // This class should only be used internally in the server and not be
059    // configurable via the admin framework.
060  }
061
062  @Override
063  public void close()
064  {
065    writer.shutdown();
066  }
067
068  @Override
069  public void log(String category, Severity severity,
070      LocalizableMessage message, Throwable exception)
071  {
072    if (message != null) {
073      Thread currentThread = Thread.currentThread();
074      if(this.thread.equals(currentThread) ||
075          this.thread.getThreadGroup().equals(currentThread.getThreadGroup()))
076      {
077        StringBuilder sb = new StringBuilder();
078        sb.append("[");
079        sb.append(TimeThread.getLocalTime());
080        sb.append("] category=").append(category).
081        append(" severity=").append(severity).
082        append(" msgID=").append(message.resourceName()).
083        append("-").append(message.ordinal()).
084        append(" msg=").append(message);
085        if (exception != null)
086        {
087          sb.append(" exception=").append(
088              StaticUtils.stackTraceToSingleLineString(exception));
089        }
090
091        this.writer.writeRecord(sb.toString());
092      }
093    }
094  }
095
096  @Override
097  public boolean isEnabledFor(String category, Severity severity)
098  {
099    Thread currentThread = Thread.currentThread();
100    return this.thread.equals(currentThread)
101        || this.thread.getThreadGroup().equals(currentThread.getThreadGroup());
102  }
103
104  @Override
105  public DN getDN()
106  {
107    // This class should only be used internally in the server and not be
108    // configurable via the admin framework.
109    return null;
110  }
111}