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-2009 Sun Microsystems, Inc. 015 * Portions Copyright 2013-2016 ForgeRock AS. 016 */ 017package org.opends.server.loggers; 018 019import java.io.PrintStream; 020import java.text.DateFormat; 021import java.text.SimpleDateFormat; 022 023import org.opends.server.admin.std.server.DebugLogPublisherCfg; 024import org.forgerock.opendj.config.server.ConfigException; 025import org.opends.server.core.ServerContext; 026import org.forgerock.opendj.ldap.DN; 027import org.opends.server.types.InitializationException; 028import org.opends.server.util.ServerConstants; 029 030/** 031 * The debug log publisher implementation that writes debug messages in a 032 * friendly for console output. 033 */ 034public class ConsoleDebugLogPublisher extends 035 DebugLogPublisher<DebugLogPublisherCfg> 036{ 037 /** 038 * The print stream where tracing will be sent. 039 */ 040 private PrintStream err; 041 042 /** 043 * The format used for trace timestamps. 044 */ 045 private DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS"); 046 047 /** 048 * Constructs a new ConsoleDebugLogPublisher that writes debug messages 049 * to the given PrintStream. 050 * @param err The PrintStream to write messages to. 051 */ 052 public ConsoleDebugLogPublisher(PrintStream err) 053 { 054 this.err = err; 055 } 056 057 /** {@inheritDoc} */ 058 @Override 059 public void initializeLogPublisher(DebugLogPublisherCfg config, ServerContext serverContext) 060 throws ConfigException, InitializationException { 061 // This publisher is not configurable. 062 } 063 064 /** {@inheritDoc} */ 065 @Override 066 public void trace(TraceSettings settings, 067 String signature, 068 String sourceLocation, 069 String msg, 070 StackTraceElement[] stackTrace) 071 { 072 String stack = null; 073 if(stackTrace != null) 074 { 075 stack = DebugStackTraceFormatter.formatStackTrace(stackTrace, settings.getStackDepth()); 076 } 077 publish(msg, stack); 078 } 079 080 /** {@inheritDoc} */ 081 @Override 082 public void traceException(TraceSettings settings, 083 String signature, 084 String sourceLocation, 085 String msg, 086 Throwable ex, StackTraceElement[] stackTrace) 087 { 088 String message = DebugMessageFormatter.format("%s caught={%s} %s(): %s", 089 new Object[] { msg, ex, signature, sourceLocation }); 090 091 String stack = null; 092 if (stackTrace != null) 093 { 094 stack = 095 DebugStackTraceFormatter.formatStackTrace(ex, settings 096 .getStackDepth(), settings.isIncludeCause()); 097 } 098 publish(message, stack); 099 } 100 101 /** {@inheritDoc} */ 102 @Override 103 public void close() 104 { 105 // Nothing to do. 106 } 107 108 109 /** 110 * Publishes a record, optionally performing some "special" work: 111 * - injecting a stack trace into the message 112 */ 113 private void publish(String msg, String stack) 114 { 115 StringBuilder buf = new StringBuilder(); 116 // Emit the timestamp. 117 buf.append(dateFormat.format(System.currentTimeMillis())); 118 buf.append(" "); 119 120 // Emit the debug level. 121 buf.append("trace "); 122 123 // Emit message. 124 buf.append(msg); 125 buf.append(ServerConstants.EOL); 126 127 // Emit Stack Trace. 128 if(stack != null) 129 { 130 buf.append("\nStack Trace:\n"); 131 buf.append(stack); 132 } 133 134 err.print(buf); 135 } 136 137 /** {@inheritDoc} */ 138 @Override 139 public DN getDN() 140 { 141 // There is no configuration DN associated with this publisher. 142 return DN.rootDN(); 143 } 144 145}