001: /*
002: * $Id: Log4jNotificationLoggerAgent.java 11129 2008-02-29 15:13:29Z acooke $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.agent;
012:
013: import org.mule.api.context.notification.ServerNotification;
014: import org.mule.api.lifecycle.InitialisationException;
015: import org.mule.config.i18n.CoreMessages;
016: import org.mule.util.FileUtils;
017: import org.mule.util.MapUtils;
018: import org.mule.util.StringUtils;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: import org.apache.log4j.Appender;
026: import org.apache.log4j.Level;
027: import org.apache.log4j.Logger;
028: import org.apache.log4j.PatternLayout;
029: import org.apache.log4j.PropertyConfigurator;
030: import org.apache.log4j.RollingFileAppender;
031: import org.apache.log4j.xml.DOMConfigurator;
032:
033: /**
034: * <code>AbstractNotificationLoggerAgent</code> Receives Mule server notifications
035: * and logs them and can optionally route them to an endpoint
036: *
037: */
038: public class Log4jNotificationLoggerAgent extends
039: AbstractNotificationLoggerAgent {
040:
041: protected static final int DEFAULT_DESCRIPTION_BUFFER_SIZE = 64;
042:
043: protected Logger eventLogger;
044: private String logName = Log4jNotificationLoggerAgent.class
045: .getName();
046: private String logFile = null;
047: private String logConfigFile = null;
048: private String chainsawHost = "localhost";
049: private int chainsawPort = -1;
050: private Map levelMappings = new HashMap();
051:
052: public Log4jNotificationLoggerAgent() {
053: super ("log4j-notifications");
054: }
055:
056: /**
057: * Should be a 1 line description of the agent
058: *
059: * @return the description of this Agent
060: */
061: public String getDescription() {
062: StringBuffer buf = new StringBuffer(
063: DEFAULT_DESCRIPTION_BUFFER_SIZE);
064: if (StringUtils.isNotBlank(logFile)) {
065: buf.append("Logging notifications to: ").append(logFile);
066: }
067: if (chainsawPort > -1) {
068: buf.append(" Chainsaw: ").append(chainsawHost).append(":")
069: .append(chainsawPort);
070: }
071: if (buf.length() == 0) {
072: buf.append("No logging or event forwarding is configured");
073: }
074: return getName() + ": " + buf.toString();
075: }
076:
077: public String getLogName() {
078: return logName;
079: }
080:
081: public void setLogName(String logName) {
082: this .logName = logName;
083: }
084:
085: protected void doInitialise() throws InitialisationException {
086: if (logConfigFile != null) {
087: if (logConfigFile.endsWith(".xml")) {
088: DOMConfigurator.configure(logConfigFile);
089: } else {
090: PropertyConfigurator.configure(logConfigFile);
091: }
092: } else {
093: try {
094: eventLogger = Logger.getLogger(logName);
095: if (logFile != null) {
096: File f = FileUtils.newFile(logFile);
097: if (!f.exists()) {
098: FileUtils.createFile(logFile);
099: }
100: Appender file = new RollingFileAppender(
101: new PatternLayout("%5p %m%n"), logFile,
102: true);
103: eventLogger.addAppender(file);
104: }
105: /* Disable for now since the org.apache.log4j.net package is not
106: exported by the PAX Logging Log4J bundle.
107: if (chainsawPort > -1)
108: {
109: Appender chainsaw = new SocketAppender(chainsawHost, chainsawPort);
110: eventLogger.addAppender(chainsaw);
111: } */
112: } catch (IOException e) {
113: throw new InitialisationException(CoreMessages
114: .failedToLoad("Log4j configuration"), e, this );
115: }
116: }
117: }
118:
119: protected void logEvent(ServerNotification e) {
120: if (eventLogger != null) {
121: String actionKey = e.EVENT_NAME + "." + e.getActionName();
122: String level = MapUtils.getString(levelMappings, actionKey,
123: e.getType());
124:
125: eventLogger.log(Level.toLevel(level, Level.INFO), e);
126: }
127: }
128:
129: public String getLogFile() {
130: return logFile;
131: }
132:
133: public void setLogFile(String logFile) {
134: this .logFile = logFile;
135: }
136:
137: public String getLogConfigFile() {
138: return logConfigFile;
139: }
140:
141: public void setLogConfigFile(String logConfigFile) {
142: this .logConfigFile = logConfigFile;
143: }
144:
145: public String getChainsawHost() {
146: return chainsawHost;
147: }
148:
149: public void setChainsawHost(String chainsawHost) {
150: this .chainsawHost = chainsawHost;
151: }
152:
153: public int getChainsawPort() {
154: return chainsawPort;
155: }
156:
157: public void setChainsawPort(int chainsawPort) {
158: this .chainsawPort = chainsawPort;
159: }
160:
161: public Map getLevelMappings() {
162: return levelMappings;
163: }
164:
165: public void setLevelMappings(Map levelMappings) {
166: this.levelMappings.putAll(levelMappings);
167: }
168: }
|