Martin Peřina has uploaded a new change for review.

Change subject: tools: Adds helpers to configure java logging in tools
......................................................................

tools: Adds helpers to configure java logging in tools

Adds helper classes to configure java.util.logging in tools using
command line parameters.

Change-Id: Ie1c46ca552888b347a4ff810003e0fcc818f832f
Bug-Url: https://bugzilla.redhat.com/1109871
Signed-off-by: Martin Perina <[email protected]>
---
A 
backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/log/JavaLoggingFormatter.java
A 
backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/log/JavaLoggingUtils.java
2 files changed, 190 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/52/32852/1

diff --git 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/log/JavaLoggingFormatter.java
 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/log/JavaLoggingFormatter.java
new file mode 100644
index 0000000..9826d0a
--- /dev/null
+++ 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/log/JavaLoggingFormatter.java
@@ -0,0 +1,65 @@
+package org.ovirt.engine.core.utils.log;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.Date;
+import java.util.logging.Formatter;
+import java.util.logging.LogRecord;
+
+/**
+ * Java logging formatter to achieve same output as from log4j formatter
+ */
+public class JavaLoggingFormatter extends Formatter {
+    private final Date date;
+    private final String format;
+
+    public JavaLoggingFormatter() {
+        this(null);
+    }
+
+    public JavaLoggingFormatter(String format) {
+        if (format == null) {
+            this.format = 
System.getProperty("java.util.logging.SimpleFormatter.format");
+        } else {
+            this.format = format;
+        }
+
+        date = new Date();
+    }
+
+    @Override
+    public synchronized String format(LogRecord record) {
+        date.setTime(record.getMillis());
+
+        StringBuilder source = new StringBuilder();
+        if (record.getSourceClassName() != null) {
+            source.append(record.getSourceClassName());
+            if (record.getSourceMethodName() != null) {
+                source.append('.');
+                source.append(record.getSourceMethodName());
+            }
+        } else {
+            source.append(record.getLoggerName());
+        }
+
+        String message = formatMessage(record);
+
+        String throwable = "";
+        if (record.getThrown() != null) {
+            StringWriter sw = new StringWriter();
+            PrintWriter pw = new PrintWriter(sw);
+            pw.println();
+            record.getThrown().printStackTrace(pw);
+            pw.close();
+            throwable = sw.toString();
+        }
+
+        return String.format(format,
+                date,
+                source.toString(),
+                record.getLoggerName(),
+                JavaLoggingUtils.formatLevel(record.getLevel()),
+                message,
+                throwable);
+    }
+}
diff --git 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/log/JavaLoggingUtils.java
 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/log/JavaLoggingUtils.java
new file mode 100644
index 0000000..86b341a
--- /dev/null
+++ 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/log/JavaLoggingUtils.java
@@ -0,0 +1,125 @@
+package org.ovirt.engine.core.utils.log;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.logging.FileHandler;
+import java.util.logging.Level;
+import java.util.logging.LogManager;
+import java.util.logging.Logger;
+
+/**
+ * Contains methods for runtime java.util.logging setup
+ */
+public class JavaLoggingUtils {
+    /**
+     * Resets logging configuration and tries to configure logging using 
system property
+     * {@code java.util.logging.config.file}
+     */
+    public static void setupLogging(String configFile) {
+        if (configFile == null) {
+            // configure default formatter
+            System.setProperty(
+                    "java.util.logging.SimpleFormatter.format",
+                    "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS,%1$tL %4$-5s [%2$s] 
%5$s%6$s%n");
+
+            // reset config and set level to ALL, so we can set desired level 
on handler
+            LogManager.getLogManager().reset();
+            Logger.getLogger("").setLevel(Level.ALL);
+        } else {
+            try {
+                LogManager.getLogManager().readConfiguration(
+                        new FileInputStream(configFile));
+            } catch (IOException ex) {
+                throw new RuntimeException("Cannot configure logging: " + 
ex.getMessage(), ex);
+            }
+        }
+    }
+
+    /**
+     * Parses logging level from case insensitive string. Level name can be 
specified in log4j or java.util.logging
+     * format:
+     *     log4j        java.util.logging
+     *
+     *     ALL          ALL
+     *                  FINEST
+     *     TRACE        FINER
+     *     DEBUG        FINE
+     *                  CONFIG
+     *     INFO         INFO
+     *     WARN         WARNING
+     *     ERROR        SEVERE
+     *     FATAL        SEVERE
+     *     OFF          OFF
+     *
+     * @param levelName
+     *            specified level name
+     * @exception java.lang.IllegalArgumentException
+     *                if unknown level is specified
+     */
+    public static Level parseLevel(String levelName) {
+        if ("ALL".equalsIgnoreCase(levelName)) {
+            return Level.ALL;
+        } else if ("FINEST".equalsIgnoreCase(levelName)) {
+            return Level.FINEST;
+        } else if ("TRACE".equalsIgnoreCase(levelName) || 
"FINER".equalsIgnoreCase(levelName)) {
+            return Level.FINER;
+        } else if ("DEBUG".equalsIgnoreCase(levelName) || 
"FINE".equalsIgnoreCase(levelName)) {
+            return Level.FINE;
+        } else if ("CONFIG".equalsIgnoreCase(levelName)) {
+            return Level.CONFIG;
+        } else if ("INFO".equalsIgnoreCase(levelName)) {
+            return Level.INFO;
+        } else if ("WARN".equalsIgnoreCase(levelName) || 
"WARNING".equalsIgnoreCase(levelName)) {
+            return Level.WARNING;
+        } else if ("ERROR".equalsIgnoreCase(levelName) ||
+                "FATAL".equalsIgnoreCase(levelName) ||
+                "SEVERE".equalsIgnoreCase(levelName)) {
+            return Level.SEVERE;
+        } else if ("OFF".equalsIgnoreCase(levelName)) {
+            return Level.OFF;
+        }
+
+        // unknown level
+        throw new IllegalArgumentException(String.format("Invalid log level 
value: '%s'", levelName));
+    }
+
+    /**
+     * Converts Level to string compatible with log4j standard
+     */
+    public static String formatLevel(Level level) {
+        if (level == null) {
+            return "";
+        } else if (Level.FINER.equals(level)) {
+            return "TRACE";
+        } else if (Level.FINE.equals(level)) {
+            return "DEBUG";
+        } else if (Level.WARNING.equals(level)) {
+            return "WARN";
+        } else if (Level.SEVERE.equals(level)) {
+            return "ERROR";
+        } else {
+            return level.getName();
+        }
+    }
+
+    /**
+     * Adds file handler with specified file and log level to global logger
+     *
+     * @param fileName
+     *            file name to log into
+     * @param levelName
+     *            log level to use
+     */
+    public static void addFileHandler(String fileName, String levelName) {
+        try {
+            FileHandler fh = new FileHandler(fileName);
+            fh.setLevel(parseLevel(levelName));
+            fh.setFormatter(new JavaLoggingFormatter());
+            Logger.getLogger("org.ovirt").addHandler(fh);
+        } catch (SecurityException | IOException ex) {
+            throw new IllegalArgumentException(
+                    String.format("Error accessing log file '%s': '%s'", 
fileName, ex.getMessage()),
+                    ex);
+        }
+    }
+}


-- 
To view, visit http://gerrit.ovirt.org/32852
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie1c46ca552888b347a4ff810003e0fcc818f832f
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Martin Peřina <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to