001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.studio.apacheds;
021
022
023 import org.apache.log4j.Appender;
024 import org.apache.log4j.AppenderSkeleton;
025 import org.apache.log4j.Level;
026 import org.apache.log4j.PatternLayout;
027 import org.apache.log4j.spi.LoggingEvent;
028 import org.eclipse.jface.util.IPropertyChangeListener;
029 import org.eclipse.jface.util.PropertyChangeEvent;
030 import org.eclipse.swt.widgets.Display;
031
032
033 /**
034 * This class implements an {@link Appender} for the Console.
035 *
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 * @version $Rev$, $Date$
038 */
039 public class StudioConsoleAppender extends AppenderSkeleton
040 {
041 /** The id of the server */
042 private String serverId;
043
044
045 /**
046 * Creates a new instance of StudioConsoleAppender.
047 */
048 public StudioConsoleAppender()
049 {
050 super();
051 // We need to set the layout asynchronously to avoid UI thread exception
052 Display.getDefault().asyncExec( new Runnable()
053 {
054 public void run()
055 {
056 setLayout( new PatternLayout( ApacheDsPluginUtils.getServerLogsPattern() ) );
057 }
058 } );
059 ApacheDsPlugin.getDefault().getPreferenceStore().addPropertyChangeListener( new IPropertyChangeListener()
060 {
061 public void propertyChange( PropertyChangeEvent event )
062 {
063 if ( ApacheDsPluginConstants.PREFS_SERVER_LOGS_PATTERN.equals( event.getProperty() ) )
064 {
065 // We need to set the layout asynchronously to avoid UI thread exception
066 Display.getDefault().asyncExec( new Runnable()
067 {
068 public void run()
069 {
070 setLayout( new PatternLayout( ApacheDsPluginUtils.getServerLogsPattern() ) );
071 }
072 } );
073 }
074 }
075 } );
076 }
077
078
079 /* (non-Javadoc)
080 * @see org.apache.log4j.AppenderSkeleton#append(org.apache.log4j.spi.LoggingEvent)
081 */
082 protected void append( LoggingEvent event )
083 {
084 final LoggingEvent logEvent = event;
085
086 // We need to print the message on console asynchronously to avoid UI thread exception
087 Display.getDefault().asyncExec( new Runnable()
088 {
089 public void run()
090 {
091 LogMessageConsole console = ConsolesHandler.getDefault().getLogMessageConsole( serverId );
092 if ( console != null )
093 {
094 // Formatting the message with the layout
095 String message = layout.format( logEvent );
096
097 // Switching dependening on the level
098 Level level = logEvent.getLevel();
099 if ( level == Level.INFO )
100 {
101 console.getInfoConsoleMessageStream().print( message );
102 }
103 else if ( level == Level.DEBUG )
104 {
105 console.getDebugConsoleMessageStream().print( message );
106 }
107 else if ( level == Level.WARN )
108 {
109 console.getWarnConsoleMessageStream().print( message );
110 }
111 else if ( level == Level.ERROR )
112 {
113 console.getErrorConsoleMessageStream().print( message );
114 }
115 else if ( level == Level.FATAL )
116 {
117 console.getFatalConsoleMessageStream().print( message );
118 }
119 }
120 }
121 } );
122 }
123
124
125 /* (non-Javadoc)
126 * @see org.apache.log4j.AppenderSkeleton#close()
127 */
128 public void close()
129 {
130 // Nothing to do
131 }
132
133
134 /* (non-Javadoc)
135 * @see org.apache.log4j.AppenderSkeleton#requiresLayout()
136 */
137 public boolean requiresLayout()
138 {
139 return false;
140 }
141
142
143 /**
144 * Gets the id of the server.
145 *
146 * @return
147 * the id of the server
148 */
149 public String getServerId()
150 {
151 return serverId;
152 }
153
154
155 /**
156 * Sets the id of the server.
157 *
158 * @param serverId
159 * the id of the server
160 */
161 public void setServerId( String serverId )
162 {
163 this.serverId = serverId;
164 }
165 }