https://issues.apache.org/jira/browse/AMQ-3100 - refactoring to enable different logger implementations

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1051972 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Bosanac Dejan 2010-12-22 16:28:35 +00:00
parent c56d192d81
commit 10a403b6ab
4 changed files with 76 additions and 4 deletions

View File

@ -16,6 +16,8 @@
*/
package org.apache.activemq.broker.jmx;
import org.apache.activemq.broker.util.AuditLog;
import org.apache.activemq.broker.util.DefaultAuditLog;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -40,6 +42,7 @@ public class AnnotatedMBean extends StandardMBean {
private static final Log LOG = LogFactory.getLog("org.apache.activemq.audit");
private static boolean audit;
private static AuditLog auditLog;
static {
Class<?>[] p = { byte.class, short.class, int.class, long.class, float.class, double.class, char.class, boolean.class, };
@ -47,6 +50,7 @@ public class AnnotatedMBean extends StandardMBean {
primitives.put(c.getName(), c);
}
audit = "true".equalsIgnoreCase(System.getProperty("org.apache.activemq.audit"));
auditLog = DefaultAuditLog.getAuditLog();
}
@SuppressWarnings("unchecked")
@ -172,7 +176,7 @@ public class AnnotatedMBean extends StandardMBean {
caller += principal.getName() + " ";
}
}
LOG.info(caller.trim() + " called " + this.getMBeanInfo().getClassName() + "." + s + Arrays.toString(objects));
auditLog.log(caller.trim() + " called " + this.getMBeanInfo().getClassName() + "." + s + Arrays.toString(objects));
}
return super.invoke(s, objects, strings);
}

View File

@ -0,0 +1,24 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.broker.util;
public interface AuditLog {
public void log(String message);
}

View File

@ -0,0 +1,41 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.broker.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class DefaultAuditLog implements AuditLog {
private static final Log LOG = LogFactory.getLog("org.apache.activemq.audit");
public static AuditLog getAuditLog() {
String auditLogClass = System.getProperty("org.apache.activemq.audit.class", "org.apache.activemq.broker.util.DefaultAuditLog");
AuditLog log;
try {
log = (AuditLog) Class.forName(auditLogClass).newInstance();
} catch (Exception e) {
LOG.warn("Cannot instantiate audit log class '" + auditLogClass + "', using default audit log", e);
log = new DefaultAuditLog();
}
return log;
}
public void log(String message) {
LOG.info(message);
}
}

View File

@ -16,13 +16,14 @@
*/
package org.apache.activemq.web;
import org.apache.activemq.broker.util.AuditLog;
import org.apache.activemq.broker.util.DefaultAuditLog;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
public class AuditFilter implements Filter {
@ -30,9 +31,11 @@ public class AuditFilter implements Filter {
private static final Log LOG = LogFactory.getLog("org.apache.activemq.audit");
private boolean audit;
private AuditLog auditLog;
public void init(FilterConfig filterConfig) throws ServletException {
audit = "true".equalsIgnoreCase(System.getProperty("org.apache.activemq.audit"));
audit = "true".equalsIgnoreCase(System.getProperty("org.apache.activemq.audit"));
auditLog = DefaultAuditLog.getAuditLog();
}
public void destroy() {
@ -53,7 +56,7 @@ public class AuditFilter implements Filter {
if (http.getRemoteUser() != null) {
user = http.getRemoteUser();
}
LOG.info(user + " requested " + http.getRequestURI() + " [" + formattedParams + "] from " + http.getRemoteAddr());
auditLog.log(user + " requested " + http.getRequestURI() + " [" + formattedParams + "] from " + http.getRemoteAddr());
}
chain.doFilter(request, response);
}