1 10 11 package org.mule.impl.internal.admin; 12 13 import org.apache.commons.collections.MapUtils; 14 import org.apache.commons.lang.StringUtils; 15 import org.apache.log4j.Appender; 16 import org.apache.log4j.Level; 17 import org.apache.log4j.Logger; 18 import org.apache.log4j.PatternLayout; 19 import org.apache.log4j.PropertyConfigurator; 20 import org.apache.log4j.RollingFileAppender; 21 import org.apache.log4j.net.SocketAppender; 22 import org.apache.log4j.xml.DOMConfigurator; 23 import org.mule.config.i18n.Message; 24 import org.mule.config.i18n.Messages; 25 import org.mule.umo.lifecycle.InitialisationException; 26 import org.mule.umo.manager.UMOServerNotification; 27 import org.mule.util.FileUtils; 28 29 import java.io.File ; 30 import java.io.IOException ; 31 import java.util.HashMap ; 32 import java.util.Map ; 33 34 39 public class Log4jNotificationLoggerAgent extends AbstractNotificationLoggerAgent 40 { 41 42 protected Logger eventLogger; 43 private String logName = Log4jNotificationLoggerAgent.class.getName(); 44 private String logFile = null; 45 private String logConfigFile = null; 46 private String chainsawHost = "localhost"; 47 private int chainsawPort = -1; 48 private Map levelMappings = new HashMap (); 49 50 55 public String getDescription() 56 { 57 StringBuffer buf = new StringBuffer (64); 58 if (StringUtils.isNotBlank(logFile)) 59 { 60 buf.append("Logging notifications to: ").append(logFile); 61 } 62 if (chainsawPort > -1) 63 { 64 buf.append(" Chainsaw: ").append(chainsawHost).append(":").append(chainsawPort); 65 } 66 if (buf.length() == 0) 67 { 68 buf.append("No logging or event forwarding is configured"); 69 } 70 return getName() + ": " + buf.toString(); 71 } 72 73 public String getLogName() 74 { 75 return logName; 76 } 77 78 public void setLogName(String logName) 79 { 80 this.logName = logName; 81 } 82 83 public void doInitialise() throws InitialisationException 84 { 85 if (logConfigFile != null) 86 { 87 if (logConfigFile.endsWith(".xml")) 88 { 89 DOMConfigurator.configure(logConfigFile); 90 } 91 else 92 { 93 PropertyConfigurator.configure(logConfigFile); 94 } 95 } 96 else 97 { 98 try 99 { 100 eventLogger = Logger.getLogger(logName); 101 if (logFile != null) 102 { 103 File f = FileUtils.newFile(logFile); 104 if (!f.exists()) 105 { 106 FileUtils.createFile(logFile); 107 } 108 Appender file = new RollingFileAppender(new PatternLayout("%5p %m%n"), logFile, true); 109 eventLogger.addAppender(file); 110 } 111 if (chainsawPort > -1) 112 { 113 Appender chainsaw = new SocketAppender(chainsawHost, chainsawPort); 114 eventLogger.addAppender(chainsaw); 115 } 116 } 117 catch (IOException e) 118 { 119 throw new InitialisationException(new Message(Messages.FAILED_LOAD_X, "Log4j configuration"), 120 e, this); 121 } 122 } 123 } 124 125 protected void logEvent(UMOServerNotification e) 126 { 127 if (eventLogger != null) 128 { 129 String actionKey = e.EVENT_NAME + "." + e.getActionName(); 130 String level = MapUtils.getString(levelMappings, actionKey, e.getType()); 131 132 eventLogger.log(Level.toLevel(level, Level.INFO), e); 133 } 134 } 135 136 public String getLogFile() 137 { 138 return logFile; 139 } 140 141 public void setLogFile(String logFile) 142 { 143 this.logFile = logFile; 144 } 145 146 public String getLogConfigFile() 147 { 148 return logConfigFile; 149 } 150 151 public void setLogConfigFile(String logConfigFile) 152 { 153 this.logConfigFile = logConfigFile; 154 } 155 156 public String getChainsawHost() 157 { 158 return chainsawHost; 159 } 160 161 public void setChainsawHost(String chainsawHost) 162 { 163 this.chainsawHost = chainsawHost; 164 } 165 166 public int getChainsawPort() 167 { 168 return chainsawPort; 169 } 170 171 public void setChainsawPort(int chainsawPort) 172 { 173 this.chainsawPort = chainsawPort; 174 } 175 176 public Map getLevelMappings() 177 { 178 return levelMappings; 179 } 180 181 public void setLevelMappings(Map levelMappings) 182 { 183 this.levelMappings.putAll(levelMappings); 184 } 185 } 186 | Popular Tags |