Index: shell/log/src/main/resources/OSGI-INF/blueprint/shell-log.xml =================================================================== --- shell/log/src/main/resources/OSGI-INF/blueprint/shell-log.xml (revision 1441320) +++ shell/log/src/main/resources/OSGI-INF/blueprint/shell-log.xml (working copy) @@ -69,6 +69,13 @@ + + + + + + + @@ -92,6 +99,8 @@ + + Index: shell/log/src/main/java/org/apache/karaf/shell/log/completers/LogEntryCompleter.java =================================================================== --- shell/log/src/main/java/org/apache/karaf/shell/log/completers/LogEntryCompleter.java (revision 0) +++ shell/log/src/main/java/org/apache/karaf/shell/log/completers/LogEntryCompleter.java (working copy) @@ -0,0 +1,31 @@ +package org.apache.karaf.shell.log.completers; + +import java.util.List; + +import org.apache.karaf.shell.console.completer.StringsCompleter; + +public class LogEntryCompleter extends StringsCompleter { + + public LogEntryCompleter() { + + super(getMessages()); + + } + + @Override + @SuppressWarnings("unchecked") + public int complete(String buffer, int cursor, List candidates) { + if (buffer == null) { + return super.complete(null, cursor, candidates); + } else { + // support completing lower case as well with the toUpperCase() call + return super.complete(buffer.toUpperCase(), cursor, candidates); + } + } + + private static String[] getMessages() { + + return new String[] {"\"Message to log\"" }; + } + +} Index: shell/log/src/main/java/org/apache/karaf/shell/log/LogEntry.java =================================================================== --- shell/log/src/main/java/org/apache/karaf/shell/log/LogEntry.java (revision 0) +++ shell/log/src/main/java/org/apache/karaf/shell/log/LogEntry.java (working copy) @@ -0,0 +1,103 @@ +package org.apache.karaf.shell.log; + +import java.io.IOException; +import java.util.Dictionary; + +import org.apache.felix.gogo.commands.Argument; +import org.apache.felix.gogo.commands.Command; +import org.apache.karaf.shell.console.OsgiCommandSupport; +import org.osgi.framework.ServiceReference; +import org.osgi.service.cm.Configuration; +import org.osgi.service.cm.ConfigurationAdmin; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Command(scope = "log", name = "log", description = "Log entry.") +public class LogEntry extends OsgiCommandSupport { + + private static final Logger log = LoggerFactory.getLogger(LogEntry.class); + + @Argument(index = 0, name = "message", description = "Message to log", required = true, multiValued = false) + String message; + + + private Level logLevel; + + private static final String ROOT_LOGGER_PREFIX = "log4j.rootLogger"; + + private static final String CONFIGURATION_PID = "org.ops4j.pax.logging"; + + private static final String DEFAULT_LOG_LEVEL = "INFO"; + + private static final String SEPARATOR_PROP = ","; + + @Override + protected Object doExecute() throws Exception { + + + Configuration cfg = getConfiguration(); + Dictionary props = cfg.getProperties(); + + String levelProp = (String) props.get(ROOT_LOGGER_PREFIX); + + try { + logLevel = Level.valueOf(getLevel(levelProp)); + } catch (IllegalArgumentException e) { + System.err + .println("level must be set to DEBUG, DEFAULT, INFO, TRACE, WARN or ERROR "); + return null; + } + + logMessage(message, logLevel); + return null; + } + + private String getLevel(String prop) { + + if (prop == null || prop.isEmpty()) { + return DEFAULT_LOG_LEVEL; + } + String levelProp = prop.trim(); + int idx = levelProp.indexOf(SEPARATOR_PROP); + return idx < 0 ? DEFAULT_LOG_LEVEL : levelProp.substring(0,idx).toUpperCase(); + + } + + private void logMessage(String message, Level logLevel) { + + switch (logLevel) { + case TRACE: + log.trace(message); + break; + case DEBUG: + log.debug(message); + break; + case INFO: + log.info(message); + break; + case WARN: + log.warn(message); + break; + case ERROR: + log.error(message); + break; + default: + log.info(message); + break; + + } + + } + + protected Configuration getConfiguration() throws IOException { + ServiceReference ref = getBundleContext().getServiceReference( + ConfigurationAdmin.class.getName()); + ConfigurationAdmin configurationAdmin = getService( + ConfigurationAdmin.class, ref); + Configuration cfg = configurationAdmin.getConfiguration( + CONFIGURATION_PID, null); + + return cfg; + } + +}