001 package hep.aida.ref.sql.JAS3Plugin; 002 003 // Log4J 004 import org.apache.log4j.Layout; 005 import org.apache.log4j.WriterAppender; 006 import org.apache.log4j.PropertyConfigurator; 007 import org.apache.log4j.spi.LoggingEvent; 008 import org.apache.log4j.spi.ThrowableInformation; 009 010 // FreeHEP 011 import org.freehep.jas.plugin.console.Console; 012 import org.freehep.jas.plugin.console.ConsoleService; 013 import org.freehep.application.Application; 014 import org.freehep.application.studio.Studio; 015 016 // Java 017 import java.io.PrintStream; 018 import java.net.URL; 019 020 /** <code>JAS3Log4JAppender</code> redirects Log4J messages to JAS3 021 * {@link Console}. 022 * <p><font color="#880088"> 023 * $Id: JAS3Log4JAppender.java,v 1.4 2007/05/23 16:38:44 hrivnac Exp $ 024 * <pre> 025 * $Log: JAS3Log4JAppender.java,v $ 026 * Revision 1.4 2007/05/23 16:38:44 hrivnac 027 * logical connections for Plotter; better UML 028 * 029 * Revision 1.3 2004/10/29 22:27:25 hrivnac 030 * imports corrected 031 * 032 * Revision 1.2 2004/10/27 21:21:20 hrivnac 033 * better documentation 034 * 035 * Revision 1.1 2004/10/27 16:28:34 hrivnac 036 * Log4J logging is connected to JAS3 Console 037 * 038 * </pre> 039 * </font></p> 040 * @opt attributes 041 * @opt operations 042 * @opt types 043 * @opt visibility 044 * @version $Id: JAS3Log4JAppender.java,v 1.4 2007/05/23 16:38:44 hrivnac Exp $ 045 * @author <a href="mailto:Julius.Hrivnac@cern.ch">J.Hrivnac</a> */ 046 public class JAS3Log4JAppender extends WriterAppender { 047 048 /** Configure and connect to JAS3. 049 * @param o The Object used to locate log4j configuration file. 050 * @param app The JAS3 {@link Studio} to display all messages. */ 051 static void init(Object o, 052 Studio app) { 053 _app = app; 054 setConsole(); 055 URL url = o.getClass().getClassLoader().getResource("hep/aida/ref/sql/JAS3Plugin/log4j.properties"); 056 if (url != null) { 057 PropertyConfigurator.configure(url); 058 } 059 } 060 061 /** Append message to output {@link Console}. 062 * Show eventual {@link Throwable} in {@link Application} popup window. 063 * @param event The {@link LoggingEvent} to be displayed. */ 064 public void append(LoggingEvent event) { 065 // Show message 066 setConsole(); 067 _out.println(layout.format(event)); 068 // Popup Throwable 069 ThrowableInformation ti = event.getThrowableInformation(); 070 if (ti != null) { 071 Application.getApplication().error("SQLTuple Error: " + layout.format(event), ti.getThrowable()); 072 } 073 } 074 075 /** Requires a layout. 076 * @return true */ 077 public boolean requiresLayout() { 078 return true; 079 } 080 081 /** Override the parent {@link WriterAppender#closeWriter} 082 * implementation to dispose {@link Console}.. */ 083 protected final void closeWriter() { 084 super.closeWriter(); 085 if (_console != null) { 086 _console.dispose(); 087 } 088 } 089 090 /** Set {@link Layout}. 091 * @param layout The {@link Layout} to be set. */ 092 public void setLayout(Layout layout) { 093 super.setLayout(layout); 094 } 095 096 /** Create {@link Console} if it doesn't exist. */ 097 private static void setConsole() { 098 if (_console == null) { 099 ConsoleService cs = (ConsoleService)_app.getLookup().lookup(ConsoleService.class); 100 _console = cs.getConsole("SQLTuple"); 101 if (_console == null) { 102 _console = cs.createConsole("SQLTuple", null); 103 } 104 _out = new PrintStream(_console.getOutputStream(null, true)); 105 } 106 } 107 108 private static Studio _app; 109 110 private static Console _console; 111 112 private static PrintStream _out; 113 114 }