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

Change subject: core: Add common methods for runtime log4j setup
......................................................................

core: Add common methods for runtime log4j setup

Adds Log4jUtils class which contains methods for runtime log4j setup
used in engine-config and engine-manage-domains.

Change-Id: Ic00ea0af643e8c978fd67eb8c56e2329d43e776d
Bug-Url: https://bugzilla.redhat.com/1063901
Bug-Url: https://bugzilla.redhat.com/1083411
Signed-off-by: Martin Perina <mper...@redhat.com>
---
A 
backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/log/Log4jUtils.java
1 file changed, 85 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/88/26588/1

diff --git 
a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/log/Log4jUtils.java
 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/log/Log4jUtils.java
new file mode 100644
index 0000000..b2c6109
--- /dev/null
+++ 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/log/Log4jUtils.java
@@ -0,0 +1,85 @@
+package org.ovirt.engine.core.utils.log;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+
+import javax.xml.parsers.FactoryConfigurationError;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.log4j.FileAppender;
+import org.apache.log4j.Level;
+import org.apache.log4j.LogManager;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PatternLayout;
+import org.apache.log4j.xml.DOMConfigurator;
+
+/**
+ * Contains methods for runtime log4j setup
+ */
+public class Log4jUtils {
+    /**
+     * Resets logging configuration and tries to configure logging using 
specified configuration.
+     *
+     * @param log4jConfig
+     *            path or URL to external log4j.xml file
+     * @param defaultUrl
+     *            URL with default log4j.xml
+     */
+    public static void setupLogging(String log4jConfig, URL defaultUrl) {
+        try {
+            URL url = defaultUrl;
+            if (!StringUtils.isBlank(log4jConfig)) {
+                File f = new File(log4jConfig);
+                if (f.exists()) {
+                    url = f.toURI().toURL();
+                } else {
+                    url = new URL(log4jConfig);
+                }
+            }
+            LogManager.resetConfiguration();
+            DOMConfigurator.configure(url);
+        } catch (IOException | FactoryConfigurationError ex) {
+            throw new RuntimeException("Cannot configure logging: " + 
ex.getMessage(), ex);
+        }
+    }
+
+    /**
+     * Adds file appender with specified file and log level to root logger
+     *
+     * @param fileName
+     *            file name to log into
+     * @param levelName
+     *            log level to use
+     */
+    public static void setupFileAppender(String fileName, String levelName) {
+        if (StringUtils.isNotBlank(fileName)) {
+            Level level;
+            if (StringUtils.isBlank(levelName)) {
+                level = Level.DEBUG;
+            } else {
+                level = Level.toLevel(levelName, null);
+                if (level == null) {
+                    throw new IllegalArgumentException(String.format("Invalid 
log level value: '%s'", levelName));
+                }
+            }
+
+            FileAppender fa = null;
+            try {
+                fa = new FileAppender(new PatternLayout("%d %-5p [%c] %m%n"), 
fileName, true);
+                fa.setThreshold(level);
+            } catch (SecurityException | IOException ex) {
+                throw new IllegalArgumentException(
+                        String.format("Error accessing log file '%s': '%s'", 
fileName, ex.getMessage()),
+                        ex);
+            }
+
+            Logger rootLogger = Logger.getRootLogger();
+            if (rootLogger.getLevel() == Level.OFF) {
+                // by default it's set to OFF, so we need to enabled it before 
adding appender
+                rootLogger.setLevel(Level.ALL);
+            }
+            rootLogger.addAppender(fa);
+        }
+    }
+}


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic00ea0af643e8c978fd67eb8c56e2329d43e776d
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Martin Peřina <mper...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to