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.equalsIgnoreCase( 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            LogMessageConsole console = ConsolesHandler.getDefault().getLogMessageConsole( serverId );
085            if ( console != null )
086            {
087                // Formatting the message with the layout
088                String message = layout.format( event );
089    
090                // Switching dependening on the level
091                Level level = event.getLevel();
092                if ( level == Level.INFO )
093                {
094                    console.getInfoConsoleMessageStream().print( message );
095                }
096                else if ( level == Level.DEBUG )
097                {
098                    console.getDebugConsoleMessageStream().print( message );
099                }
100                else if ( level == Level.WARN )
101                {
102                    console.getWarnConsoleMessageStream().print( message );
103                }
104                else if ( level == Level.ERROR )
105                {
106                    console.getErrorConsoleMessageStream().print( message );
107                }
108                else if ( level == Level.FATAL )
109                {
110                    console.getFatalConsoleMessageStream().print( message );
111                }
112            }
113        }
114    
115    
116        /* (non-Javadoc)
117         * @see org.apache.log4j.AppenderSkeleton#close()
118         */
119        public void close()
120        {
121            // Nothing to do
122        }
123    
124    
125        /* (non-Javadoc)
126         * @see org.apache.log4j.AppenderSkeleton#requiresLayout()
127         */
128        public boolean requiresLayout()
129        {
130            return false;
131        }
132    
133    
134        /**
135         * Gets the id of the server.
136         *
137         * @return
138         *      the id of the server
139         */
140        public String getServerId()
141        {
142            return serverId;
143        }
144    
145    
146        /**
147         * Sets the id of the server.
148         *
149         * @param serverId
150         *      the id of the server
151         */
152        public void setServerId( String serverId )
153        {
154            this.serverId = serverId;
155        }
156    }