You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by re...@apache.org on 2009/05/09 11:39:51 UTC

svn commit: r773186 - in /cocoon/cocoon3/trunk: cocoon-docs/src/changes/ cocoon-monitoring/src/main/java/org/ cocoon-monitoring/src/main/java/org/apache/ cocoon-monitoring/src/main/java/org/apache/cocoon/ cocoon-monitoring/src/main/java/org/apache/coco...

Author: reinhard
Date: Sat May  9 09:39:50 2009
New Revision: 773186

URL: http://svn.apache.org/viewvc?rev=773186&view=rev
Log:
Initial work of Dariusz on supporting logging reconfiguration via JMX

Added:
    cocoon/cocoon3/trunk/cocoon-monitoring/src/main/java/org/
    cocoon/cocoon3/trunk/cocoon-monitoring/src/main/java/org/apache/
    cocoon/cocoon3/trunk/cocoon-monitoring/src/main/java/org/apache/cocoon/
    cocoon/cocoon3/trunk/cocoon-monitoring/src/main/java/org/apache/cocoon/monitoring/
    cocoon/cocoon3/trunk/cocoon-monitoring/src/main/java/org/apache/cocoon/monitoring/Log4JReconfigurator.java   (with props)
    cocoon/cocoon3/trunk/cocoon-monitoring/src/main/java/org/apache/cocoon/monitoring/LoggingConfigurationResetter.java   (with props)
    cocoon/cocoon3/trunk/cocoon-monitoring/src/main/resources/
    cocoon/cocoon3/trunk/cocoon-monitoring/src/main/resources/META-INF/
    cocoon/cocoon3/trunk/cocoon-monitoring/src/main/resources/META-INF/cocoon/
    cocoon/cocoon3/trunk/cocoon-monitoring/src/main/resources/META-INF/cocoon/spring/
    cocoon/cocoon3/trunk/cocoon-monitoring/src/main/resources/META-INF/cocoon/spring/cocoon-monitoring.xml   (with props)
Modified:
    cocoon/cocoon3/trunk/cocoon-docs/src/changes/changes.xml
    cocoon/cocoon3/trunk/cocoon-sample/rcl.properties

Modified: cocoon/cocoon3/trunk/cocoon-docs/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-docs/src/changes/changes.xml?rev=773186&r1=773185&r2=773186&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-docs/src/changes/changes.xml (original)
+++ cocoon/cocoon3/trunk/cocoon-docs/src/changes/changes.xml Sat May  9 09:39:50 2009
@@ -132,6 +132,10 @@
         annotations. 
       </action>
 
+      <action dev="reinhard" type="add" due-to="Dariusz Luksza">
+        [cocoon-monitoring] Support logging reconfiguration via JMX
+      </action>
+             
       <action dev="reinhard" type="add" issue="COCOON3-7" due-to="Simone Tripodi">
         [cocoon-optional] Add a formatting objects serializer using Apache FOP.
       </action>       

Added: cocoon/cocoon3/trunk/cocoon-monitoring/src/main/java/org/apache/cocoon/monitoring/Log4JReconfigurator.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-monitoring/src/main/java/org/apache/cocoon/monitoring/Log4JReconfigurator.java?rev=773186&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-monitoring/src/main/java/org/apache/cocoon/monitoring/Log4JReconfigurator.java (added)
+++ cocoon/cocoon3/trunk/cocoon-monitoring/src/main/java/org/apache/cocoon/monitoring/Log4JReconfigurator.java Sat May  9 09:39:50 2009
@@ -0,0 +1,97 @@
+/*
+ * 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.cocoon.monitoring;
+
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+
+import org.apache.log4j.Level;
+import org.apache.log4j.LogManager;
+import org.apache.log4j.Logger;
+import org.apache.log4j.spi.LoggerRepository;
+import org.springframework.jmx.export.annotation.ManagedAttribute;
+import org.springframework.jmx.export.annotation.ManagedOperation;
+import org.springframework.jmx.export.annotation.ManagedOperationParameter;
+import org.springframework.jmx.export.annotation.ManagedOperationParameters;
+import org.springframework.jmx.export.annotation.ManagedResource;
+
+@ManagedResource
+public class Log4JReconfigurator {
+
+    private LoggerRepository loggerRepository;
+
+    public Log4JReconfigurator() {
+        this.loggerRepository = LogManager.getLoggerRepository();
+    }
+
+    @ManagedAttribute(description = "Return a list of all configured loggers with their level.")
+    public String[] getLoggers() {
+        List<String> result = new ArrayList<String>();
+
+        @SuppressWarnings("unchecked")
+        Enumeration<Logger> currentLoggers = this.loggerRepository.getCurrentLoggers();
+        while (currentLoggers.hasMoreElements()) {
+            Logger tmpLogger = currentLoggers.nextElement();
+            if (tmpLogger.getLevel() != null) {
+                result.add(tmpLogger.getName() + ": " + tmpLogger.getLevel());
+            }
+        }
+
+        return result.toArray(new String[] {});
+    }
+
+    @ManagedOperation(description = "Sets logging level for a paticular package or a class. Returns true if operation was succesful.")
+    @ManagedOperationParameters(value = {
+            @ManagedOperationParameter(name = "category", description = "Name of the log category (usually a package or class name) whose log level should be changed."),
+            @ManagedOperationParameter(name = "newLevel", description = "New log level for that category. Avaliable log levels are: OFF, INFO, WARN, ERROR, FATAL, TRACE, DEBUG, ALL") })
+    public boolean setLoggingLevel(String category, String newLogLevel) {
+        boolean result = false;
+
+        Logger logger = this.loggerRepository.getLogger(category);
+        if (logger != null) {
+            logger.setLevel(Level.toLevel(newLogLevel.toUpperCase()));
+            result = true;
+        }
+
+        return result;
+    }
+
+    @ManagedOperation(description = "Sets new logging level for amout of time. After timeout log level is set back to old value.")
+    @ManagedOperationParameters(value = {
+            @ManagedOperationParameter(name = "category", description = "Name of the log category (usually a package or class name) whose log level should be changed."),
+            @ManagedOperationParameter(name = "temporalLevel", description = "Temporal log level for that category that should be set for specified amout of time."),
+            @ManagedOperationParameter(name = "timeOut", description = "Amount of time that temporalLevel should be active. Value of timeOut should match regex: ^[0-9.]+[dhm]?$ where 'd' means day, 'h' hours and 'm' minutes") })
+    public boolean setLoggingTempoporalLevel(String category, String temporalLogLevel, String timeOut) {
+        if (!timeOut.matches("^[0-9.]+[dhm]?$")) {
+            throw new UnsupportedOperationException("Unsupported time out format: " + timeOut);
+        }
+
+        boolean result = false;
+        Logger logger = this.loggerRepository.getLogger(category);
+        if (logger != null) {
+            Level oldLevel = logger.getLevel();
+            LoggingConfigurationResetter restoreThread = new LoggingConfigurationResetter(logger, oldLevel, timeOut
+                    .toLowerCase());
+            logger.setLevel(Level.toLevel(temporalLogLevel));
+            restoreThread.start();
+            result = true;
+        }
+
+        return result;
+    }
+}

Propchange: cocoon/cocoon3/trunk/cocoon-monitoring/src/main/java/org/apache/cocoon/monitoring/Log4JReconfigurator.java
------------------------------------------------------------------------------
    svn:mergeinfo = 

Added: cocoon/cocoon3/trunk/cocoon-monitoring/src/main/java/org/apache/cocoon/monitoring/LoggingConfigurationResetter.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-monitoring/src/main/java/org/apache/cocoon/monitoring/LoggingConfigurationResetter.java?rev=773186&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-monitoring/src/main/java/org/apache/cocoon/monitoring/LoggingConfigurationResetter.java (added)
+++ cocoon/cocoon3/trunk/cocoon-monitoring/src/main/java/org/apache/cocoon/monitoring/LoggingConfigurationResetter.java Sat May  9 09:39:50 2009
@@ -0,0 +1,74 @@
+/*
+ * 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.cocoon.monitoring;
+
+import java.util.Timer;
+import java.util.TimerTask;
+
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+
+public class LoggingConfigurationResetter {
+
+    private long delay;
+    private Logger logger;
+    private Level oldLogLevel;
+
+    public LoggingConfigurationResetter(final Logger logger, final Level oldLevel, String timeout) {
+
+        this.logger = logger;
+        this.oldLogLevel = oldLevel;
+
+        long factor;
+        char unit = timeout.charAt(timeout.length() - 1); // get last char, it should be our unit
+        switch (unit) {
+        case 'm': // minute
+            factor = 60 * 1000;
+            break;
+        case 'h': // hour
+            factor = 60 * 60 * 1000;
+            break;
+        case 'd': // day
+            factor = 24 * 60 * 60 * 1000;
+            break;
+        default:
+            throw new UnsupportedOperationException("Unsupporterd unit: " + unit);
+        }
+
+        float multipler = Float.parseFloat(timeout.substring(0, timeout.length() - 1));
+        this.delay = Math.round(multipler * factor);
+
+    }
+
+    public void start() {
+        TimerTask task = new TimerTask() {
+            @Override
+            public void run() {
+                LoggingConfigurationResetter.this.logger.setLevel(LoggingConfigurationResetter.this.oldLogLevel);
+            }
+
+            @Override
+            public boolean cancel() {
+                this.run(); // set old level on task cancel
+                return super.cancel();
+            }
+        };
+
+        Timer timer = new Timer("Restore " + this.logger.getName() + " to level" + this.oldLogLevel, true);
+        timer.schedule(task, this.delay);
+    }
+}

Propchange: cocoon/cocoon3/trunk/cocoon-monitoring/src/main/java/org/apache/cocoon/monitoring/LoggingConfigurationResetter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/cocoon3/trunk/cocoon-monitoring/src/main/java/org/apache/cocoon/monitoring/LoggingConfigurationResetter.java
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cocoon/cocoon3/trunk/cocoon-monitoring/src/main/java/org/apache/cocoon/monitoring/LoggingConfigurationResetter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: cocoon/cocoon3/trunk/cocoon-monitoring/src/main/resources/META-INF/cocoon/spring/cocoon-monitoring.xml
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-monitoring/src/main/resources/META-INF/cocoon/spring/cocoon-monitoring.xml?rev=773186&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-monitoring/src/main/resources/META-INF/cocoon/spring/cocoon-monitoring.xml (added)
+++ cocoon/cocoon3/trunk/cocoon-monitoring/src/main/resources/META-INF/cocoon/spring/cocoon-monitoring.xml Sat May  9 09:39:50 2009
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+ -->
+<beans xmlns="http://www.springframework.org/schema/beans"
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
+
+  <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
+    <property name="assembler" ref="org.apache.cocoon.monitoring.assembler" />
+    <property name="namingStrategy" ref="org.apache.cocoon.monitoring.namingStrategy" />
+    <property name="autodetect" value="true" />
+  </bean>
+
+  <bean id="org.apache.cocoon.monitoring.jmxAttributeSource"
+    class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource" />
+
+  <bean id="org.apache.cocoon.monitoring.assembler" class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
+    <property name="attributeSource" ref="org.apache.cocoon.monitoring.jmxAttributeSource" />
+  </bean>
+
+  <bean id="org.apache.cocoon.monitoring.namingStrategy" class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
+    <property name="attributeSource" ref="org.apache.cocoon.monitoring.jmxAttributeSource" />
+  </bean>
+  
+  <bean id="org.apache.cocoon.monitoring.Log4JReconfigurator"
+    class="org.apache.cocoon.monitoring.Log4JReconfigurator" scope="singleton" />
+</beans>

Propchange: cocoon/cocoon3/trunk/cocoon-monitoring/src/main/resources/META-INF/cocoon/spring/cocoon-monitoring.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/cocoon3/trunk/cocoon-monitoring/src/main/resources/META-INF/cocoon/spring/cocoon-monitoring.xml
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cocoon/cocoon3/trunk/cocoon-monitoring/src/main/resources/META-INF/cocoon/spring/cocoon-monitoring.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Modified: cocoon/cocoon3/trunk/cocoon-sample/rcl.properties
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sample/rcl.properties?rev=773186&r1=773185&r2=773186&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sample/rcl.properties (original)
+++ cocoon/cocoon3/trunk/cocoon-sample/rcl.properties Sat May  9 09:39:50 2009
@@ -15,3 +15,7 @@
 # limitations under the License.
 #
 org.apache.cocoon.sample.servlet%classes-dir=./target/classes
+
+# cocoon-monitoring
+%classes-dir=../cocoon-monitoring/target/classes
+%exclude-lib=org.apache.cocoon.monitoring:cocoon-monitoring