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, 61 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/18/26818/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..24e0145
--- /dev/null
+++ 
b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/log/Log4jUtils.java
@@ -0,0 +1,61 @@
+package org.ovirt.engine.core.utils.log;
+
+import java.io.IOException;
+import java.net.URL;
+
+import javax.xml.parsers.FactoryConfigurationError;
+
+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 url
+     *            URL with log4j.xml
+     */
+    public static void setupLogging(URL url) {
+        try {
+            LogManager.resetConfiguration();
+            DOMConfigurator.configure(url);
+        } catch (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 addFileAppender(String fileName, String levelName) {
+        try {
+            Level level = Level.INFO;
+            if (levelName != null) {
+                level = Level.toLevel(levelName, null);
+                if (level == null) {
+                    throw new IllegalArgumentException(String.format("Invalid 
log level value: '%s'", levelName));
+                }
+            }
+
+            FileAppender fa = new FileAppender(new PatternLayout("%d %-5p [%c] 
%m%n"), fileName, true);
+            fa.setThreshold(level);
+            Logger.getRootLogger().addAppender(fa);
+        } 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/26818
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic00ea0af643e8c978fd67eb8c56e2329d43e776d
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: ovirt-engine-3.4
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