Skip to content

Commit

Permalink
[KARAF-4879] The log:get command should display all loggers by default
Browse files Browse the repository at this point in the history
[KARAF-4828] Support OFF log level in log:set console command
  • Loading branch information
gnodet committed Dec 6, 2016
1 parent b0c227c commit 1ff5aa1
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 66 deletions.
1 change: 1 addition & 0 deletions log/pom.xml
Expand Up @@ -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>
Expand Down
26 changes: 14 additions & 12 deletions log/src/main/java/org/apache/karaf/log/command/GetLogLevel.java
Expand Up @@ -34,7 +34,7 @@
@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)
Expand All @@ -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;
}

Expand Down
Expand Up @@ -34,7 +34,7 @@
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)
Expand Down
1 change: 1 addition & 0 deletions log/src/main/java/org/apache/karaf/log/core/Level.java
Expand Up @@ -27,6 +27,7 @@ public enum Level {
INFO,
WARN,
ERROR,
OFF,
DEFAULT;

/**
Expand Down
Expand Up @@ -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 {

Expand Down
Expand Up @@ -21,21 +21,23 @@
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) {
Expand All @@ -47,19 +49,21 @@ public Map<String, String> getLevel(String logger) {
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;
}
Expand All @@ -84,39 +88,28 @@ public void setLevel(String logger, String level) {
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);
}
}
}
Expand Down

0 comments on commit 1ff5aa1

Please sign in to comment.