[FELIX-4475] Improve logging in FileInstall
Allow delegating to jul instead of stdout
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1583669 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/DirectoryWatcher.java b/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/DirectoryWatcher.java
index f2bfd99..c6a8f3d 100644
--- a/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/DirectoryWatcher.java
+++ b/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/DirectoryWatcher.java
@@ -91,6 +91,7 @@
public final static String POLL = "felix.fileinstall.poll";
public final static String DIR = "felix.fileinstall.dir";
public final static String LOG_LEVEL = "felix.fileinstall.log.level";
+ public final static String LOG_DEFAULT = "felix.fileinstall.log.default";
public final static String TMPDIR = "felix.fileinstall.tmpdir";
public final static String FILTER = "felix.fileinstall.filter";
public final static String START_NEW_BUNDLES = "felix.fileinstall.bundles.new.start";
@@ -110,6 +111,9 @@
public final static String SCOPE_MANAGED = "managed";
public final static String SCOPE_ALL = "all";
+ public final static String LOG_STDOUT = "stdout";
+ public final static String LOG_JUL = "jul";
+
static final SecureRandom random = new SecureRandom();
final File javaIoTmpdir = new File(System.getProperty("java.io.tmpdir"));
diff --git a/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/Util.java b/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/Util.java
index 4e19c91..db924e5 100644
--- a/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/Util.java
+++ b/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/Util.java
@@ -30,6 +30,7 @@
import java.util.Set;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
+import java.util.logging.Level;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@@ -99,7 +100,7 @@
}
catch (Throwable t)
{
- logger = new StdOutLogger();
+ logger = new DefaultLogger(context);
}
return logger;
}
@@ -117,8 +118,21 @@
void log(int logLevel, int msgLevel, String message, Throwable throwable);
}
- static class StdOutLogger implements Logger
+ static class DefaultLogger implements Logger
{
+ protected BundleContext context;
+ private final String logDefault;
+
+ DefaultLogger(BundleContext context)
+ {
+ this.context = context;
+ String s = context.getProperty(DirectoryWatcher.LOG_DEFAULT);
+ s = (s == null)
+ ? System.getProperty(DirectoryWatcher.LOG_DEFAULT.toUpperCase().replace('.', '_'))
+ : s;
+ logDefault = (s == null) ? DirectoryWatcher.LOG_STDOUT : s;
+ }
+
public boolean isValidLogger(BundleContext context)
{
return true;
@@ -131,22 +145,37 @@
// level.
if ((logLevel > 0) && (msgLevel <= logLevel))
{
- System.out.println(message + ((throwable == null) ? "" : ": " + throwable));
- if (throwable != null)
+ if (DirectoryWatcher.LOG_JUL.equals(logDefault))
{
- throwable.printStackTrace(System.out);
+ Level lvl;
+ switch (msgLevel)
+ {
+ case 1: lvl = Level.SEVERE; break;
+ case 2: lvl = Level.WARNING; break;
+ case 3: lvl = Level.INFO; break;
+ case 4: lvl = Level.FINE; break;
+ default: lvl = Level.FINEST; break;
+ }
+ java.util.logging.Logger logger = java.util.logging.Logger.getLogger("fileinstall");
+ logger.log(lvl, message, throwable);
+ }
+ else
+ {
+ System.out.println(message + ((throwable == null) ? "" : ": " + throwable));
+ if (throwable != null)
+ {
+ throwable.printStackTrace(System.out);
+ }
}
}
}
}
- static class OsgiLogger extends StdOutLogger
+ static class OsgiLogger extends DefaultLogger
{
- private BundleContext context;
-
OsgiLogger(BundleContext context)
{
- this.context = context;
+ super(context);
// Now make sure we can access the LogService class
try
{