Author: [email protected]
Date: Tue Jun 12 08:49:08 2012
New Revision: 2459

Log:
[AMDATUAUTH-166] Implemented customizable loglevels per package

Added:
   
branches/amdatu-auth-0.2.3/tools/loghandler/src/main/java/org/amdatu/auth/tools/loghandler/service/JdkLogMessage.java
   
branches/amdatu-auth-0.2.3/tools/loghandler/src/main/java/org/amdatu/auth/tools/loghandler/service/MultiLogLevel.java
Modified:
   branches/amdatu-auth-0.2.3/tools/loghandler/pom.xml
   
branches/amdatu-auth-0.2.3/tools/loghandler/src/main/java/org/amdatu/auth/tools/loghandler/service/ConsoleLogHandler.java
   
branches/amdatu-auth-0.2.3/tools/loghandler/src/main/java/org/amdatu/auth/tools/loghandler/service/JdkLogForwarder.java

Modified: branches/amdatu-auth-0.2.3/tools/loghandler/pom.xml
==============================================================================
--- branches/amdatu-auth-0.2.3/tools/loghandler/pom.xml (original)
+++ branches/amdatu-auth-0.2.3/tools/loghandler/pom.xml Tue Jun 12 08:49:08 2012
@@ -47,12 +47,12 @@
             
<Bundle-SymbolicName>org.amdatu.auth.tools.loghandler</Bundle-SymbolicName>
             <Embed-Dependency>*;scope=compile</Embed-Dependency>
             <Embed-Transitive>true</Embed-Transitive>
-            <DynamicImport-Package>
-              javax.servlet,
-              org.apache.log,
-              org.apache.log4j,
-              org.apache.avalon.framework.logger
-            </DynamicImport-Package>
+            <Import-Package>
+              !org.apache.avalon.framework.logger,
+              !org.apache.log,
+              !org.apache.log4j,
+              *
+            </Import-Package>
           </instructions>
         </configuration>
       </plugin>

Modified: 
branches/amdatu-auth-0.2.3/tools/loghandler/src/main/java/org/amdatu/auth/tools/loghandler/service/ConsoleLogHandler.java
==============================================================================
--- 
branches/amdatu-auth-0.2.3/tools/loghandler/src/main/java/org/amdatu/auth/tools/loghandler/service/ConsoleLogHandler.java
   (original)
+++ 
branches/amdatu-auth-0.2.3/tools/loghandler/src/main/java/org/amdatu/auth/tools/loghandler/service/ConsoleLogHandler.java
   Tue Jun 12 08:49:08 2012
@@ -15,14 +15,17 @@
  */
 package org.amdatu.auth.tools.loghandler.service;
 
-import java.util.Calendar;
 import java.util.Dictionary;
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.logging.Formatter;
 import java.util.logging.Level;
 import java.util.logging.LogManager;
+import java.util.logging.LogRecord;
 import java.util.logging.Logger;
+import java.util.logging.SimpleFormatter;
+import java.util.logging.XMLFormatter;
 
 import org.osgi.service.cm.ConfigurationException;
 import org.osgi.service.cm.ManagedService;
@@ -36,18 +39,23 @@
  * 
  * @author <a href="mailto:[email protected]";>Amdatu Project 
Team</a>
  */
+@SuppressWarnings("rawtypes")
 public class ConsoleLogHandler implements LogListener, ManagedService {
-    private static int DEFAULT_LOG_LEVEL = LogService.LOG_DEBUG;
-    private int m_minLogLevel = DEFAULT_LOG_LEVEL;
+    // Defaults
+    private static Formatter DEFAULT_FORMATTER = new SimpleFormatter();
 
-    private static final Map<Integer, String> LOG_LEVELS;
-    static {
-        LOG_LEVELS = new HashMap<Integer, String>();
-        LOG_LEVELS.put(LogService.LOG_DEBUG, "DEBUG");
-        LOG_LEVELS.put(LogService.LOG_INFO, "INFO");
-        LOG_LEVELS.put(LogService.LOG_WARNING, "WARNING");
-        LOG_LEVELS.put(LogService.LOG_ERROR, "ERROR");
-    }
+    // Console log handler options
+    // Minimum log level for all generic loggers
+    private MultiLogLevel m_globalMinLogLevel = MultiLogLevel.DEFAULT;
+
+    // Minimum applied log level for all generic AND custom loggers
+    private MultiLogLevel m_allMinLogLevel = MultiLogLevel.DEFAULT;
+
+    // The formatter to apply for log messages
+    private Formatter m_formatter = DEFAULT_FORMATTER;
+
+    // Map of custom log levels
+    private Map<String, Level> m_loggerLevels = new HashMap<String, Level>();
 
     /**
      * Name of the configuration PID for the loghandler config.
@@ -58,12 +66,21 @@
      * Name of the configuration property that determines the minimum log level
      * of entries to print to the console.
      */
-    private static final String MIN_LOGLEVEL_KEY = "console.mininum.loglevel";
+    private static final String MIN_LOGLEVEL_KEY = 
"java.util.logging.ConsoleHandler.level";
+
+    /**
+     * Name of the configuration property that determines the formatter to be 
applied.
+     */
+    private static final String FORMATTER_KEY = 
"java.util.logging.ConsoleHandler.formatter";
 
     public void start() {
         System.out.println("Console log handler started.");
     }
 
+    public void stop() {
+        System.out.println("Console log handler stopped.");
+    }
+
     public void logReaderAdded(LogReaderService reader) {
         reader.addLogListener(this);
     }
@@ -72,93 +89,144 @@
         reader.removeLogListener(this);
     }
 
-    public void logged(LogEntry entry) {
-        if (entry.getLevel() <= m_minLogLevel) {
-            String symbName = entry.getBundle().getSymbolicName();
-            String msg;
-            if (PID.equals(symbName)) {
-                // This is a log message logged by the JdkLogForwarder service 
from this bundle.
-                // In this case, do not append the origin bundle as this is 
already appended by
-                // the JdkLogForwarder.
-                msg = entry.getMessage();
-            }
-            else {
-                msg = entry.getMessage() + " [" + symbName + "]";
+    public void updated(Dictionary properties) throws ConfigurationException {
+        String loggerName = "";
+        if (properties != null) {
+            try {
+                StringBuffer changes = new StringBuffer();
+
+                String minLogLevel = (String) properties.get(MIN_LOGLEVEL_KEY);
+                m_globalMinLogLevel = new MultiLogLevel(minLogLevel);
+                if (m_globalMinLogLevel.getJDKLevel() == null) {
+                    // Invalid log level has been defined, switch to the 
default
+                    String msg = "Unknown 
java.util.logging.ConsoleHandler.level '" + minLogLevel
+                        + "', switching to default level '" + 
MultiLogLevel.DEFAULT + "'";
+                    log2Console(Level.WARNING, getClass().getName(), msg, 
null);
+
+                    m_globalMinLogLevel = MultiLogLevel.DEFAULT;
+                }
+                m_allMinLogLevel = m_globalMinLogLevel;
+                changes.append("Global log level -> " + 
m_globalMinLogLevel.getJDKLevel());
+
+                m_formatter = getFormatterFromProperties(properties);
+
+                // Build map of custom log levels
+                Map<String, Level> customLoggers = new HashMap<String, 
Level>();
+                Enumeration keys = properties.keys();
+                while (keys.hasMoreElements()) {
+                    Object key = keys.nextElement();
+                    if (key.toString().endsWith(".level") && 
!MIN_LOGLEVEL_KEY.equals(key.toString())) {
+                        MultiLogLevel level = new 
MultiLogLevel(properties.get(key).toString());
+                        if (level.getJDKLevel() == null) {
+                            String msg = "Level '" + level.getLevel() + "' 
does not exist for configured logger '"
+                                + key.toString() + "', custom logger ignored.";
+                            log2Console(Level.WARNING, getClass().getName(), 
msg, null);
+                        }
+                        else {
+                            if (level.getOSGiLevel() > 
m_allMinLogLevel.getOSGiLevel()) {
+                                m_allMinLogLevel = level;
+                            }
+                            String loggerPackage = key.toString().substring(0, 
key.toString().lastIndexOf(".level"));
+                            customLoggers.put(loggerPackage, 
level.getJDKLevel());
+                        }
+                    }
+                }
+
+                // Set generic log levels
+                Enumeration<String> loggerNames = 
LogManager.getLogManager().getLoggerNames();
+                while (loggerNames.hasMoreElements()) {
+                    loggerName = loggerNames.nextElement();
+                    Logger logger = 
LogManager.getLogManager().getLogger(loggerName);
+                    if (logger != null) {
+                        String customLogger = 
MultiLogLevel.findClosestLogger(customLoggers, loggerName);
+                        if (customLogger != null) {
+                            logger.setLevel(customLoggers.get(customLogger));
+                            m_loggerLevels.put(loggerName, 
customLoggers.get(customLogger));
+                            changes.append("\r\n" + loggerName + " -> " + 
customLoggers.get(customLogger));
+                        }
+                        else {
+                            logger.setLevel(m_globalMinLogLevel.getJDKLevel());
+                        }
+                    }
+                }
+
+                if (m_globalMinLogLevel.getOSGiLevel() == 
LogService.LOG_DEBUG) {
+                    System.out.println("Updated log levels. " + changes);
+                }
             }
-            String levelName = LOG_LEVELS.get(entry.getLevel());
-            System.out.println("[" + getTimestamp() + "] " + levelName + ": " 
+ msg);
-            if (entry.getException() != null) {
-                System.out.println(entry.getException().getMessage());
-                entry.getException().printStackTrace();
+            catch (Exception e) {
+                throw new ConfigurationException("unknown", "An error occurred 
while configuring ConsoleLogHandler", e);
             }
         }
     }
 
-    private String getTimestamp() {
-        Calendar cal = Calendar.getInstance();
-        String timestamp = new Integer(cal.get(Calendar.YEAR)).toString(); // 
year
-        timestamp += "-" + toTwoDigits(cal.get(Calendar.MONTH) + 1); // month
-        timestamp += "-" + toTwoDigits(cal.get(Calendar.DAY_OF_MONTH)); // day
-        timestamp += " " + toTwoDigits(cal.get(Calendar.HOUR_OF_DAY)); // hour
-        timestamp += ":" + toTwoDigits(cal.get(Calendar.MINUTE)); // minutes
-        timestamp += ":" + toTwoDigits(cal.get(Calendar.SECOND)); // seconds
-        return timestamp;
-    }
+    private Formatter getFormatterFromProperties(Dictionary dictionary) {
+        if (dictionary == null) {
+            return DEFAULT_FORMATTER;
+        }
+        String sFormatter = (String) dictionary.get(FORMATTER_KEY);
+        if (sFormatter == null) {
+            return DEFAULT_FORMATTER;
+        }
 
-    private String toTwoDigits(int dt) {
-        if (dt < 10) {
-            return "0" + new Integer(dt).toString();
+        if (SimpleFormatter.class.getCanonicalName().equals(sFormatter)) {
+            return new SimpleFormatter();
         }
-        else {
-            return new Integer(dt).toString();
+        else if (XMLFormatter.class.getCanonicalName().equals(sFormatter)) {
+            return new XMLFormatter();
         }
+        return DEFAULT_FORMATTER;
     }
 
-    public void updated(Dictionary properties) throws ConfigurationException {
-        m_minLogLevel = getLogLevelFromProperties(properties);
-        Level jdkLevel = toJDKLevel(m_minLogLevel);
-        Enumeration<String> loggerNames = 
LogManager.getLogManager().getLoggerNames();
-        while (loggerNames.hasMoreElements()) {
-            String loggerName = loggerNames.nextElement();
-            Logger logger = LogManager.getLogManager().getLogger(loggerName);
-            if (logger != null) {
-                logger.setLevel(jdkLevel);
+    public void logged(LogEntry entry) {
+        if (entry.getLevel() <= m_allMinLogLevel.getOSGiLevel()) {
+            String symbName = entry.getBundle().getSymbolicName();
+            if (PID.equals(symbName)) {
+                // This is a log message logged by the JdkLogForwarder service 
from this bundle.
+                // In this case, do not append the origin bundle as this is 
already appended by
+                // the JdkLogForwarder.
+                JdkLogMessage jdkMessage = 
JdkLogMessage.parseMessage(entry.getMessage());
+                if (jdkMessage.getLogger() == null) {
+                    jdkMessage.setLogger(symbName);
+                }
+                log2Console(jdkMessage.getLevel(), jdkMessage.getLogger(), 
jdkMessage.getMessage(),
+                    entry.getException());
+            }
+            else {
+                Level level = new 
MultiLogLevel(entry.getLevel()).getJDKLevel();
+                log2Console(level, symbName, entry.getMessage(), 
entry.getException());
             }
         }
     }
 
-    private Level toJDKLevel(int osgiLevel) {
-        if (osgiLevel == LogService.LOG_DEBUG) {
-            return Level.ALL;
+    private void log2Console(Level level, String loggerName, String msg, 
Throwable t) {
+        if (m_loggerLevels.containsKey(loggerName)) {
+            // If a custom logger is defined, verify that it meets the min 
loglevel
+            if (m_loggerLevels.get(loggerName).intValue() > level.intValue()) {
+                return;
+            }
         }
-        else if (osgiLevel == LogService.LOG_INFO) {
-            return Level.INFO;
+        else if (m_globalMinLogLevel.getJDKLevel().intValue() > 
level.intValue()) {
+            // Otherwise, verify it meets the minimum global loglevel
+            return;
         }
-        else if (osgiLevel == LogService.LOG_WARNING) {
-            return Level.WARNING;
+
+        LogRecord record = new LogRecord(new 
MultiLogLevel(level).getJDKLevel(), msg);
+        if (loggerName != null) {
+            record.setLoggerName(loggerName);
         }
         else {
-            return Level.SEVERE;
-        }
-    }
-
-    private int getLogLevelFromProperties(Dictionary dictionary) {
-        if (dictionary == null) {
-            return DEFAULT_LOG_LEVEL;
+            record.setLoggerName(this.getClass().getCanonicalName());
         }
+        record.setThrown(t);
 
-        String minLogLevel = (String) dictionary.get(MIN_LOGLEVEL_KEY);
-        if (minLogLevel == null) {
-            return DEFAULT_LOG_LEVEL;
+        // Let the formatter print the result to System.out or System.err
+        String println = m_formatter.format(record);
+        if (t == null && level.intValue() <= Level.INFO.intValue()) {
+            System.out.print(println);
         }
-
-        for (Map.Entry<Integer, String> logLevelEntry : LOG_LEVELS.entrySet()) 
{
-            if (minLogLevel.equalsIgnoreCase(logLevelEntry.getValue())) {
-                return logLevelEntry.getKey();
-            }
+        else {
+            System.err.print(println);
         }
-
-        // If we get here, there is no other way we can find the log level
-        return DEFAULT_LOG_LEVEL;
     }
 }

Modified: 
branches/amdatu-auth-0.2.3/tools/loghandler/src/main/java/org/amdatu/auth/tools/loghandler/service/JdkLogForwarder.java
==============================================================================
--- 
branches/amdatu-auth-0.2.3/tools/loghandler/src/main/java/org/amdatu/auth/tools/loghandler/service/JdkLogForwarder.java
     (original)
+++ 
branches/amdatu-auth-0.2.3/tools/loghandler/src/main/java/org/amdatu/auth/tools/loghandler/service/JdkLogForwarder.java
     Tue Jun 12 08:49:08 2012
@@ -17,9 +17,7 @@
 
 import java.util.ArrayList;
 import java.util.Enumeration;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 import java.util.logging.Handler;
 import java.util.logging.Level;
 import java.util.logging.LogManager;
@@ -35,15 +33,6 @@
  * @author <a href="mailto:[email protected]";>Amdatu Project 
Team</a>
  */
 public class JdkLogForwarder extends Handler {
-
-    private static final Map<Level, Integer> LOG_LEVEL_MAPPING = new 
HashMap<Level, Integer>();
-    static {
-        LOG_LEVEL_MAPPING.put(Level.SEVERE, LogService.LOG_ERROR);
-        LOG_LEVEL_MAPPING.put(Level.WARNING, LogService.LOG_WARNING);
-        LOG_LEVEL_MAPPING.put(Level.INFO, LogService.LOG_INFO);
-        LOG_LEVEL_MAPPING.put(Level.CONFIG, LogService.LOG_INFO);
-    }
-
     private volatile LogService m_log;
     private final List<String> m_loggerNames;
 
@@ -75,17 +64,19 @@
                 m_loggerNames.add(loggerName);
                 Logger logger = logManager.getLogger(loggerName);
                 if (logger != null) {
-                    logger.setUseParentHandlers(false);
+                    if 
(!ConsoleLogHandler.class.getName().equals(logger.getName())) {
+                        logger.setUseParentHandlers(false);
 
-                    // Remove all current log handlers, it should be only me!
-                    if (logger.getHandlers() != null) {
-                        for (Handler logHandler : logger.getHandlers()) {
-                            logger.removeHandler(logHandler);
+                        // Remove all current log handlers, it should be only 
me!
+                        if (logger.getHandlers() != null) {
+                            for (Handler logHandler : logger.getHandlers()) {
+                                logger.removeHandler(logHandler);
+                            }
                         }
-                    }
 
-                    // Add me
-                    logger.addHandler(this);
+                        // Add me
+                        logger.addHandler(this);
+                    }
                 }
             }
         }
@@ -94,38 +85,26 @@
     public void onRemoved(ServiceReference ref, Object service) {
     }
 
-    @Override
     public void publish(LogRecord record) {
         if (record.getLevel() == Level.OFF) {
             return;
         }
-        String loggername = " [" + record.getLoggerName() + "]";
+        // When JDK message are forwarded to the LogService, information about 
the logger
+        // and Log level are lost, so we append them to the message and 
extract them
+        // from the message in the ConsoleLogHandler.
+        MultiLogLevel level = new MultiLogLevel(record.getLevel()); 
+        JdkLogMessage msg = new JdkLogMessage(record);
         if (record.getThrown() != null) {
-            m_log.log(getToOsgiLogLevel(record.getLevel()),
-                record.getMessage() + loggername, record.getThrown());
+            m_log.log(level.getOSGiLevel(), msg.toString(), 
record.getThrown());
         }
         else {
-            m_log
-                .log(getToOsgiLogLevel(record.getLevel()), record
-                    .getMessage() + loggername);
+            m_log.log(level.getOSGiLevel(), msg.toString());
         }
     }
 
-    @Override
     public void close() throws SecurityException {
     }
 
-    @Override
     public void flush() {
     }
-
-    private int getToOsgiLogLevel(Level level) {
-        if (LOG_LEVEL_MAPPING.containsKey(level)) {
-            return LOG_LEVEL_MAPPING.get(level);
-        }
-        else {
-            return LogService.LOG_DEBUG;
-        }
-    }
-
 }

Added: 
branches/amdatu-auth-0.2.3/tools/loghandler/src/main/java/org/amdatu/auth/tools/loghandler/service/JdkLogMessage.java
==============================================================================
--- (empty file)
+++ 
branches/amdatu-auth-0.2.3/tools/loghandler/src/main/java/org/amdatu/auth/tools/loghandler/service/JdkLogMessage.java
       Tue Jun 12 08:49:08 2012
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2010-2012 The Amdatu Foundation
+ *
+ * Licensed 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.amdatu.auth.tools.loghandler.service;
+
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+
+/**
+ * Helper class to tunnel JDK logged messaged through the LogService without 
loosing the original information.
+ * 
+ * @author <a href="mailto:[email protected]";>Amdatu Project 
Team</a>
+ */
+public class JdkLogMessage {
+    private String m_logger;
+    private String m_message;
+    private Level m_level;
+    
+    public JdkLogMessage(LogRecord record) {
+        m_logger = record.getLoggerName();
+        m_level = record.getLevel();
+        m_message = record.getMessage();
+    }
+    
+    public JdkLogMessage(String logger, String message, Level level) {
+        m_logger = logger;
+        m_message = message;
+        m_level = level;
+    }
+    
+    public JdkLogMessage() {
+    }
+    
+    public static JdkLogMessage parseMessage(String msg) {
+        String message = msg;
+        Level level = null;
+        String logger = null;
+        if (msg != null && msg.indexOf("[level:") != -1 &&  
msg.trim().endsWith("]")) {
+            String sLevel = msg.substring(msg.lastIndexOf("[level:") + 
"[level:".length(), msg.lastIndexOf("]"));
+            level = new MultiLogLevel(sLevel).getJDKLevel();
+            message = message.substring(0, message.lastIndexOf("[level:"));
+        }
+        if (message != null && message.indexOf("[logger:") != -1 &&  
message.trim().endsWith("]")) {
+            logger = message.substring(message.lastIndexOf("[logger:") + 
"[logger:".length(), message.lastIndexOf("]"));
+            message = message.substring(0, message.lastIndexOf("[logger:"));
+        }
+        return new JdkLogMessage(logger, message, level);
+    }
+
+    public String getLogger() {
+        return m_logger;
+    }
+
+    public void setLogger(String logger) {
+        m_logger = logger;
+    }
+
+    public String getMessage() {
+        return m_message;
+    }
+
+    public void setMessage(String message) {
+        m_message = message;
+    }
+
+    public Level getLevel() {
+        return m_level;
+    }
+
+    public void setLevel(Level level) {
+        m_level = level;
+    }
+    
+    public String toString() {
+        return m_message + "[logger:" + m_logger + "][level:" + 
m_level.toString() + "]";
+    }
+}

Added: 
branches/amdatu-auth-0.2.3/tools/loghandler/src/main/java/org/amdatu/auth/tools/loghandler/service/MultiLogLevel.java
==============================================================================
--- (empty file)
+++ 
branches/amdatu-auth-0.2.3/tools/loghandler/src/main/java/org/amdatu/auth/tools/loghandler/service/MultiLogLevel.java
       Tue Jun 12 08:49:08 2012
@@ -0,0 +1,129 @@
+/*
+ * Copyright (c) 2010-2012 The Amdatu Foundation
+ *
+ * Licensed 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.amdatu.auth.tools.loghandler.service;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.logging.Level;
+
+import org.osgi.service.log.LogService;
+
+/**
+ * This class represents a loglevel for multiple frameworks, at the moment for
+ * OSGi and JDK.
+ * 
+ * @author <a href="mailto:[email protected]";>Amdatu Project 
Team</a>
+ */
+public class MultiLogLevel {
+    private static final Map<String, Level> STRING2JDK;
+    static {
+        STRING2JDK = new HashMap<String, Level>();
+        STRING2JDK.put("FINEST", Level.FINEST);
+        STRING2JDK.put("FINER", Level.FINER);
+        STRING2JDK.put("FINE", Level.FINE);
+        STRING2JDK.put("CONFIG", Level.CONFIG);
+        STRING2JDK.put("INFO", Level.INFO);
+        STRING2JDK.put("WARNING", Level.WARNING);
+        STRING2JDK.put("SEVERE", Level.SEVERE);
+    }
+
+    private static final Map<Level, Integer> JDK2OSGI;
+    static {
+        JDK2OSGI = new HashMap<Level, Integer>();
+        JDK2OSGI.put(Level.FINEST, LogService.LOG_DEBUG);
+        JDK2OSGI.put(Level.FINER, LogService.LOG_DEBUG);
+        JDK2OSGI.put(Level.FINE, LogService.LOG_DEBUG);
+        JDK2OSGI.put(Level.CONFIG, LogService.LOG_DEBUG);
+        JDK2OSGI.put(Level.INFO, LogService.LOG_INFO);
+        JDK2OSGI.put(Level.WARNING, LogService.LOG_WARNING);
+        JDK2OSGI.put(Level.SEVERE, LogService.LOG_ERROR);
+    }
+
+    private static final Map<Integer, Level> OSGI2JDK_LOG_LEVELS;
+    static {
+        OSGI2JDK_LOG_LEVELS = new HashMap<Integer, Level>();
+        OSGI2JDK_LOG_LEVELS.put(LogService.LOG_DEBUG, Level.FINEST);
+        OSGI2JDK_LOG_LEVELS.put(LogService.LOG_INFO, Level.INFO);
+        OSGI2JDK_LOG_LEVELS.put(LogService.LOG_WARNING, Level.WARNING);
+        OSGI2JDK_LOG_LEVELS.put(LogService.LOG_ERROR, Level.SEVERE);
+    }
+
+    public static MultiLogLevel DEFAULT = new MultiLogLevel("INFO");
+
+    public static Level[] ALL_JDK_LEVELS = new Level[] {Level.FINEST, 
Level.FINER, Level.FINE,
+        Level.CONFIG, Level.INFO, Level.WARNING, Level.SEVERE};
+
+    private String m_sLevel;
+    private int m_osgiLevel;
+    private Level m_jdkLevel;
+
+    public MultiLogLevel(String level) {
+        m_sLevel = level;
+        if (level != null && STRING2JDK.containsKey(level)) {
+            m_jdkLevel = STRING2JDK.get(level);
+            m_osgiLevel = JDK2OSGI.get(m_jdkLevel);
+        }
+    }
+
+    public MultiLogLevel(Level level) {
+        m_sLevel = level.toString();
+        m_jdkLevel = level;
+        m_osgiLevel = JDK2OSGI.get(m_jdkLevel);
+    }
+
+    public MultiLogLevel(int level) {
+        if (level > 0 && level <= LogService.LOG_DEBUG) {
+            m_osgiLevel = level;
+            for (Level key : JDK2OSGI.keySet()) {
+                if (JDK2OSGI.get(key) == level) {
+                    m_jdkLevel = key;
+                }
+            }
+            m_sLevel = m_jdkLevel.toString();
+        }
+    }
+
+    public String getLevel() {
+        return m_sLevel;
+    }
+
+    public Level getJDKLevel() {
+        return m_jdkLevel;
+    }
+
+    public int getOSGiLevel() {
+        return m_osgiLevel;
+    }
+
+    public static String findClosestLogger(Map<String, Level> map, String 
loggerClass) {
+        if (loggerClass.lastIndexOf(".") != -1) {
+            String loggerPackage = loggerClass.substring(0, 
loggerClass.lastIndexOf("."));
+            String closestLogger = null;
+            for (String key : map.keySet()) {
+                if (loggerPackage.startsWith(key) || loggerClass.equals(key)) {
+                    if (closestLogger == null) {
+                        closestLogger = key;
+                    }
+                    else if (closestLogger.length() < key.length()) {
+                        closestLogger = key;
+                    }
+                }
+            }
+            return closestLogger;
+        }
+        return null;
+    }
+}
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to