1 16 17 package org.apache.axis.handlers ; 18 19 import org.apache.axis.AxisFault; 20 import org.apache.axis.Message; 21 import org.apache.axis.MessageContext; 22 import org.apache.axis.components.logger.LogFactory; 23 import org.apache.axis.utils.Messages; 24 import org.apache.commons.logging.Log; 25 26 import java.io.FileWriter ; 27 import java.io.IOException ; 28 import java.io.PrintWriter ; 29 30 42 public class LogHandler extends BasicHandler { 43 protected static Log log = 44 LogFactory.getLog(LogHandler.class.getName()); 45 46 long start = -1; 47 private boolean writeToConsole = false; 48 private String filename = "axis.log"; 49 50 public void init() { 51 super.init(); 52 53 Object opt = this.getOption("LogHandler.writeToConsole"); 54 if (opt != null && opt instanceof String && 55 "true".equalsIgnoreCase((String )opt)) 56 writeToConsole = true; 57 58 opt = this.getOption("LogHandler.fileName"); 59 if (opt != null && opt instanceof String ) 60 filename = (String )opt; 61 } 62 63 public void invoke(MessageContext msgContext) throws AxisFault { 64 log.debug("Enter: LogHandler::invoke"); 65 if (msgContext.getPastPivot() == false) { 66 start = System.currentTimeMillis(); 67 } else { 68 logMessages(msgContext); 69 } 70 log.debug("Exit: LogHandler::invoke"); 71 } 72 73 private void logMessages(MessageContext msgContext) throws AxisFault { 74 try { 75 PrintWriter writer = null; 76 77 writer = getWriter(); 78 79 Message inMsg = msgContext.getRequestMessage(); 80 Message outMsg = msgContext.getResponseMessage(); 81 82 writer.println( "=======================================================" ); 83 if (start != -1) { 84 writer.println( "= " + Messages.getMessage("elapsed00", 85 "" + (System.currentTimeMillis() - start))); 86 } 87 writer.println( "= " + Messages.getMessage("inMsg00", 88 (inMsg == null ? "null" : inMsg.getSOAPPartAsString()))); 89 writer.println( "= " + Messages.getMessage("outMsg00", 90 (outMsg == null ? "null" : outMsg.getSOAPPartAsString()))); 91 writer.println( "=======================================================" ); 92 93 if (!writeToConsole) { 95 writer.close(); 96 } 97 99 } catch( Exception e ) { 100 log.error( Messages.getMessage("exception00"), e ); 101 throw AxisFault.makeFault(e); 102 } 103 } 104 105 private PrintWriter getWriter() throws IOException { 106 PrintWriter writer; 107 108 if (writeToConsole) { 110 writer = new PrintWriter (System.out); 112 } else { 113 if (filename == null) { 115 filename = "axis.log"; 116 } 117 writer = new PrintWriter (new FileWriter ( filename, true )); 118 } 119 return writer; 120 } 121 122 123 public void onFault(MessageContext msgContext) { 124 try { 125 logMessages(msgContext); 126 } catch (AxisFault axisFault) { 127 log.error(Messages.getMessage("exception00"), axisFault); 128 } 129 } 130 }; 131 | Popular Tags |