Author: ffang
Date: Tue Jul  9 08:23:17 2013
New Revision: 1501123

URL: http://svn.apache.org/r1501123
Log:
[KARAF-2146]Add a log command to log from shell to the log

Added:
    
karaf/trunk/log/command/src/main/java/org/apache/karaf/log/command/LogEntry.java
Modified:
    karaf/trunk/log/command/src/main/resources/OSGI-INF/blueprint/shell-log.xml

Added: 
karaf/trunk/log/command/src/main/java/org/apache/karaf/log/command/LogEntry.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/log/command/src/main/java/org/apache/karaf/log/command/LogEntry.java?rev=1501123&view=auto
==============================================================================
--- 
karaf/trunk/log/command/src/main/java/org/apache/karaf/log/command/LogEntry.java
 (added)
+++ 
karaf/trunk/log/command/src/main/java/org/apache/karaf/log/command/LogEntry.java
 Tue Jul  9 08:23:17 2013
@@ -0,0 +1,49 @@
+package org.apache.karaf.log.command;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
+import org.apache.karaf.shell.console.AbstractAction;
+import org.osgi.service.log.LogService;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@Command(scope = "log", name = "log", description = "Log a message.")
+public class LogEntry extends AbstractAction {
+
+    @Argument(index = 0, name = "message", description = "The message to log", 
required = true, multiValued = false)
+    private String message;
+
+    @Option(name = "--level", aliases = {"-l"}, description = "The level the 
message will be logged at", required = false, multiValued = false)
+    private String level = "INFO";
+
+    private LogService logService;
+
+    private final Map<String,Integer> mappings = new HashMap<String,Integer>();
+
+    public LogEntry(LogService logService) {
+        this.logService = logService;
+
+        mappings.put("ERROR",1);
+        mappings.put("WARNING",2);
+        mappings.put("INFO",3);
+        mappings.put("DEBUG",4);
+    }
+
+    @Override
+    protected Object doExecute() throws Exception {
+        logService.log(toLevel(level.toUpperCase()), message);
+
+        return null;
+    }
+
+    private int toLevel(String logLevel) {
+        Integer level =  mappings.get(logLevel);
+        if(level == null) {
+            level = 3;
+        }
+        return level;
+    }
+
+}

Modified: 
karaf/trunk/log/command/src/main/resources/OSGI-INF/blueprint/shell-log.xml
URL: 
http://svn.apache.org/viewvc/karaf/trunk/log/command/src/main/resources/OSGI-INF/blueprint/shell-log.xml?rev=1501123&r1=1501122&r2=1501123&view=diff
==============================================================================
--- karaf/trunk/log/command/src/main/resources/OSGI-INF/blueprint/shell-log.xml 
(original)
+++ karaf/trunk/log/command/src/main/resources/OSGI-INF/blueprint/shell-log.xml 
Tue Jul  9 08:23:17 2013
@@ -58,10 +58,31 @@
                 <property name="formatter" ref="formatter"/>
             </action>
         </command>
+        <command>
+            <action class="org.apache.karaf.log.command.LogEntry">
+                <argument ref="osgiLogService" />
+            </action>
+            <optional-completers>
+                <entry key="--level" value-ref="osgiLogLevelsCompleter"/>
+            </optional-completers>
+        </command>
     </command-bundle>
+
+    <bean id="osgiLogLevelsCompleter" 
class="org.apache.karaf.shell.console.completer.StringsCompleter">
+        <argument type="java.util.Collection" >
+            <list>
+                <value>WARNING</value>
+                <value>DEBUG</value>
+                <value>INFO</value>
+                <value>ERROR</value>
+            </list>
+        </argument>
+        <argument value="false" />
+    </bean>
     
     <reference id="formatter" 
interface="org.apache.karaf.log.core.LogEventFormatter"/>
     <reference id="logService" 
interface="org.apache.karaf.log.core.LogService"/>
+    <reference id="osgiLogService" 
interface="org.osgi.service.log.LogService"/>
 
     <bean id="logLevelCompleter" 
class="org.apache.karaf.log.command.completers.LogLevelCompleter"/>
 


Reply via email to