karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject [2/2] karaf git commit: [KARAF-4879] The log:get command should display all loggers by default [KARAF-4828] Support OFF log level in log:set console command
Date Tue, 06 Dec 2016 08:08:33 GMT
[KARAF-4879] The log:get command should display all loggers by default
[KARAF-4828] Support OFF log level in log:set console command

Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/1ff5aa1a
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/1ff5aa1a
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/1ff5aa1a

Branch: refs/heads/master
Commit: 1ff5aa1aa008538e0de18328c7e032a2683833ec
Parents: b0c227c
Author: Guillaume Nodet <gnodet@apache.org>
Authored: Tue Dec 6 09:08:17 2016 +0100
Committer: Guillaume Nodet <gnodet@apache.org>
Committed: Tue Dec 6 09:08:17 2016 +0100

----------------------------------------------------------------------
 log/pom.xml                                     |  1 +
 .../apache/karaf/log/command/GetLogLevel.java   | 26 ++++---
 .../apache/karaf/log/command/SetLogLevel.java   |  2 +-
 .../java/org/apache/karaf/log/core/Level.java   |  1 +
 .../log/core/internal/LogServiceInternal.java   | 11 ---
 .../log/core/internal/LogServiceLog4j2Impl.java | 77 +++++++++-----------
 6 files changed, 52 insertions(+), 66 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/1ff5aa1a/log/pom.xml
----------------------------------------------------------------------
diff --git a/log/pom.xml b/log/pom.xml
index e4e10f5..2cb4adf 100644
--- a/log/pom.xml
+++ b/log/pom.xml
@@ -109,6 +109,7 @@
                             org.apache.karaf.log.core.internal,
                             org.apache.karaf.log.core.internal.layout,
                             org.apache.karaf.log.core.internal.osgi,
+                            org.apache.felix.utils.collections
                         </Private-Package>
                     </instructions>
                 </configuration>

http://git-wip-us.apache.org/repos/asf/karaf/blob/1ff5aa1a/log/src/main/java/org/apache/karaf/log/command/GetLogLevel.java
----------------------------------------------------------------------
diff --git a/log/src/main/java/org/apache/karaf/log/command/GetLogLevel.java b/log/src/main/java/org/apache/karaf/log/command/GetLogLevel.java
index 83e5003..f9b0cf7 100644
--- a/log/src/main/java/org/apache/karaf/log/command/GetLogLevel.java
+++ b/log/src/main/java/org/apache/karaf/log/command/GetLogLevel.java
@@ -34,7 +34,7 @@ import org.apache.karaf.shell.support.table.ShellTable;
 @Service
 public class GetLogLevel implements Action {
 
-    @Argument(index = 0, name = "logger", description = "The name of the logger, ALL or ROOT
(default)", required = false, multiValued = false)
+    @Argument(index = 0, name = "logger", description = "The name of the logger or ALL (default)",
required = false, multiValued = false)
     String logger;
 
     @Option(name = "--no-format", description = "Disable table rendered output", required
= false, multiValued = false)
@@ -45,18 +45,20 @@ public class GetLogLevel implements Action {
 
     @Override
     public Object execute() throws Exception {
-        Map<String, String> loggers = logService.getLevel(logger);
-
-        ShellTable table = new ShellTable();
-        table.column("Logger");
-        table.column("Level");
-
-        for (String logger : loggers.keySet()) {
-            table.addRow().addContent(logger, loggers.get(logger));
+        if (logger == null) {
+            Map<String, String> loggers = logService.getLevel("ALL");
+            ShellTable table = new ShellTable();
+            table.column("Logger");
+            table.column("Level");
+            loggers.forEach((n, l) -> table.addRow().addContent(n, l));
+            table.print(System.out, !noFormat);
+        }
+        else
+        {
+            Map<String, String> loggers = logService.getLevel( logger );
+            String level = loggers.get( logger );
+            System.out.println( level );
         }
-
-        table.print(System.out, !noFormat);
-
         return null;
     }
 

http://git-wip-us.apache.org/repos/asf/karaf/blob/1ff5aa1a/log/src/main/java/org/apache/karaf/log/command/SetLogLevel.java
----------------------------------------------------------------------
diff --git a/log/src/main/java/org/apache/karaf/log/command/SetLogLevel.java b/log/src/main/java/org/apache/karaf/log/command/SetLogLevel.java
index b1145b4..3d7393a 100644
--- a/log/src/main/java/org/apache/karaf/log/command/SetLogLevel.java
+++ b/log/src/main/java/org/apache/karaf/log/command/SetLogLevel.java
@@ -34,7 +34,7 @@ import org.apache.karaf.shell.support.completers.StringsCompleter;
 public class SetLogLevel implements Action {
     
     @Argument(index = 0, name = "level", description = "The log level to set (TRACE, DEBUG,
INFO, WARN, ERROR) or DEFAULT to unset", required = true, multiValued = false)
-    @Completion(value = StringsCompleter.class, values = { "TRACE", "DEBUG", "INFO", "WARN",
"ERROR", "DEFAULT" })
+    @Completion(value = StringsCompleter.class, values = { "OFF", "TRACE", "DEBUG", "INFO",
"WARN", "ERROR", "DEFAULT" })
     String level;
 
     @Argument(index = 1, name = "logger", description = "Logger name or ROOT (default)",
required = false, multiValued = false)

http://git-wip-us.apache.org/repos/asf/karaf/blob/1ff5aa1a/log/src/main/java/org/apache/karaf/log/core/Level.java
----------------------------------------------------------------------
diff --git a/log/src/main/java/org/apache/karaf/log/core/Level.java b/log/src/main/java/org/apache/karaf/log/core/Level.java
index a83396f..fcde028 100644
--- a/log/src/main/java/org/apache/karaf/log/core/Level.java
+++ b/log/src/main/java/org/apache/karaf/log/core/Level.java
@@ -27,6 +27,7 @@ public enum Level {
     INFO,
     WARN,
     ERROR,
+    OFF,
     DEFAULT;
     
     /**

http://git-wip-us.apache.org/repos/asf/karaf/blob/1ff5aa1a/log/src/main/java/org/apache/karaf/log/core/internal/LogServiceInternal.java
----------------------------------------------------------------------
diff --git a/log/src/main/java/org/apache/karaf/log/core/internal/LogServiceInternal.java
b/log/src/main/java/org/apache/karaf/log/core/internal/LogServiceInternal.java
index d7e06b4..184278e 100644
--- a/log/src/main/java/org/apache/karaf/log/core/internal/LogServiceInternal.java
+++ b/log/src/main/java/org/apache/karaf/log/core/internal/LogServiceInternal.java
@@ -16,18 +16,7 @@
  */
 package org.apache.karaf.log.core.internal;
 
-import java.io.IOException;
-import java.util.Dictionary;
-import java.util.Enumeration;
 import java.util.Map;
-import java.util.TreeMap;
-
-import org.apache.karaf.log.core.Level;
-import org.apache.karaf.log.core.LogService;
-import org.ops4j.pax.logging.spi.PaxAppender;
-import org.ops4j.pax.logging.spi.PaxLoggingEvent;
-import org.osgi.service.cm.Configuration;
-import org.osgi.service.cm.ConfigurationAdmin;
 
 public interface LogServiceInternal {
 

http://git-wip-us.apache.org/repos/asf/karaf/blob/1ff5aa1a/log/src/main/java/org/apache/karaf/log/core/internal/LogServiceLog4j2Impl.java
----------------------------------------------------------------------
diff --git a/log/src/main/java/org/apache/karaf/log/core/internal/LogServiceLog4j2Impl.java
b/log/src/main/java/org/apache/karaf/log/core/internal/LogServiceLog4j2Impl.java
index 229d5ce..555e8ec 100644
--- a/log/src/main/java/org/apache/karaf/log/core/internal/LogServiceLog4j2Impl.java
+++ b/log/src/main/java/org/apache/karaf/log/core/internal/LogServiceLog4j2Impl.java
@@ -21,21 +21,23 @@ import java.util.Dictionary;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.TreeMap;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
+import org.apache.felix.utils.collections.DictionaryAsMap;
 import org.apache.karaf.log.core.Level;
 
 public class LogServiceLog4j2Impl implements LogServiceInternal {
 
     static final String ROOT_LOGGER_LEVEL = "log4j2.rootLogger.level";
-    static final String LOGGERS = "log4j2.loggers";
     static final String LOGGER_PREFIX = "log4j2.logger.";
     static final String NAME_SUFFIX = ".name";
     static final String LEVEL_SUFFIX = ".level";
 
-    private final Dictionary<String, Object> config;
+    private final Map<String, Object> config;
 
     public LogServiceLog4j2Impl(Dictionary<String, Object> config) {
-        this.config = config;
+        this.config = new DictionaryAsMap<>(config);
     }
 
     public Map<String, String> getLevel(String logger) {
@@ -47,19 +49,21 @@ public class LogServiceLog4j2Impl implements LogServiceInternal {
             return loggers;
         }
 
-        String ids = (String) config.get(LOGGERS);
-        if (ids != null) {
-            for (String id : ids.split(",")) {
-                id = id.trim();
-                if (!id.equalsIgnoreCase(ROOT_LOGGER)) {
-                    String name = (String) config.get(name(id));
-                    String level = (String) config.get(level(id));
-                    if (name != null && level != null) {
-                        loggers.put(name, level);
-                    }
-                }
+        Map<String, String> names = new HashMap<>();
+        Map<String, String> levels = new HashMap<>();
+        for (String key : config.keySet()) {
+            Matcher matcher = Pattern.compile("log4j2\\.logger\\.([a-zA-Z_]+)\\.name").matcher(key);
+            if (matcher.matches()) {
+                names.put(matcher.group(1), config.get(key).toString());
+            }
+            matcher = Pattern.compile("log4j2\\.logger\\.([a-zA-Z_]+)\\.level").matcher(key);
+            if (matcher.matches()) {
+                levels.put(matcher.group(1), config.get(key).toString());
             }
         }
+        for (Map.Entry<String, String> e : names.entrySet()) {
+            loggers.put(e.getValue(), levels.get(e.getKey()));
+        }
         if (ALL_LOGGER.equalsIgnoreCase(logger)) {
             return loggers;
         }
@@ -84,39 +88,28 @@ public class LogServiceLog4j2Impl implements LogServiceInternal {
         if (logger == null || LogServiceInternal.ROOT_LOGGER.equalsIgnoreCase(logger)) {
             config.put(ROOT_LOGGER_LEVEL, level);
         } else {
-            Map<String, String> names = new HashMap<>();
-            String ids = (String) config.get(LOGGERS);
-            if (ids != null) {
-                for (String id : ids.split(",")) {
-                    id = id.trim();
-                    if (!id.equalsIgnoreCase(ROOT_LOGGER)) {
-                        String name = (String) config.get(name(id));
-                        if (name != null) {
-                            names.put(name, id);
-                        }
+            String loggerKey = null;
+            for (String key : config.keySet()) {
+                Matcher matcher = Pattern.compile("\\Q" + LOGGER_PREFIX + "\\E([a-zA-Z_]+)\\Q"
+ NAME_SUFFIX + "\\E").matcher(key);
+                if (matcher.matches()) {
+                    String name = config.get(key).toString();
+                    if (name.matches(logger)) {
+                        loggerKey = matcher.group(1);
+                        break;
                     }
                 }
             }
 
-            if (Level.isDefault(level)) {
-                if (names.containsKey(logger)) {
-                    config.remove(level(names.get(logger)));
-                }
-            }
-            else {
-                if (names.containsKey(logger)) {
-                    config.put(level(names.get(logger)), level);
-                }
-                else {
-                    String id = logger.toLowerCase().replace('.', '_').replace('$', '_');
-                    config.put(name(id), logger);
-                    if (ids != null) {
-                        config.put(LOGGERS, ids + "," + id);
-                    } else {
-                        config.put(LOGGERS, id);
-                    }
-                    config.put(level(id), level);
+            if (loggerKey != null) {
+                if (Level.isDefault(level)) {
+                    config.remove(level(loggerKey));
+                } else {
+                    config.put(level(loggerKey), level);
                 }
+            } else {
+                loggerKey = logger.replace('.', '_').toLowerCase();
+                config.put(name(loggerKey), logger);
+                config.put(level(loggerKey), level);
             }
         }
     }


Mime
View raw message