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.opends.server.admin.std.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 /** {@inheritDoc} */ 055 @Override 056 public void initializeLogPublisher(ErrorLogPublisherCfg config, ServerContext serverContext) 057 throws ConfigException, InitializationException 058 { 059 // This class should only be used internally in the server and not be 060 // configurable via the admin framework. 061 } 062 063 /** {@inheritDoc} */ 064 @Override 065 public void close() 066 { 067 writer.shutdown(); 068 } 069 070 /** {@inheritDoc} */ 071 @Override 072 public void log(String category, Severity severity, 073 LocalizableMessage message, Throwable exception) 074 { 075 if (message != null) { 076 Thread currentThread = Thread.currentThread(); 077 if(this.thread.equals(currentThread) || 078 this.thread.getThreadGroup().equals(currentThread.getThreadGroup())) 079 { 080 StringBuilder sb = new StringBuilder(); 081 sb.append("["); 082 sb.append(TimeThread.getLocalTime()); 083 sb.append("] category=").append(category). 084 append(" severity=").append(severity). 085 append(" msgID=").append(message.resourceName()). 086 append("-").append(message.ordinal()). 087 append(" msg=").append(message); 088 if (exception != null) 089 { 090 sb.append(" exception=").append( 091 StaticUtils.stackTraceToSingleLineString(exception)); 092 } 093 094 this.writer.writeRecord(sb.toString()); 095 } 096 } 097 } 098 099 /** {@inheritDoc} */ 100 @Override 101 public boolean isEnabledFor(String category, Severity severity) 102 { 103 Thread currentThread = Thread.currentThread(); 104 return this.thread.equals(currentThread) 105 || this.thread.getThreadGroup().equals(currentThread.getThreadGroup()); 106 } 107 108 /** {@inheritDoc} */ 109 @Override 110 public DN getDN() 111 { 112 // This class should only be used internally in the server and not be 113 // configurable via the admin framework. 114 return null; 115 } 116}