]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.common/src/org/simantics/db/common/utils/LogManager.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / utils / LogManager.java
diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/LogManager.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/LogManager.java
new file mode 100644 (file)
index 0000000..be87899
--- /dev/null
@@ -0,0 +1,151 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.common.utils;\r
+\r
+import java.util.Properties;\r
+\r
+import org.apache.log4j.Hierarchy;\r
+import org.apache.log4j.Level;\r
+import org.apache.log4j.Logger;\r
+import org.apache.log4j.PropertyConfigurator;\r
+import org.apache.log4j.spi.LoggerFactory;\r
+import org.apache.log4j.spi.RootLogger;\r
+\r
+/**\r
+ * This class encapsulates a Log4J Hierarchy and centralizes all Logger access.\r
+ */\r
+public class LogManager {\r
+\r
+    private Hierarchy hierarchy;\r
+\r
+    /**\r
+     * Creates a new LogManager. Saves the log and state location.\r
+     * Creates a new Hierarchy and add a new EventListener to it.\r
+     * Configure the hierarchy with the properties passed. Add this object to\r
+     * the list of active log managers.\r
+     * \r
+     * @param properties log configuration properties\r
+     */\r
+    public LogManager(Properties properties) {\r
+        this.hierarchy = new Hierarchy(new RootLogger(Level.DEBUG));\r
+        new PropertyConfigurator().doConfigure(properties, this.hierarchy);\r
+    }\r
+\r
+    /**\r
+     * Checks if this PluginLogManager is disabled for this level.\r
+     * \r
+     * @param level level value\r
+     * @return boolean true if it is disabled\r
+     */\r
+    public boolean isDisabled(int level) {\r
+        return this.hierarchy.isDisabled(level);\r
+    }\r
+\r
+    /**\r
+     * Enable logging for logging requests with level l or higher. By default\r
+     * all levels are enabled.\r
+     * \r
+     * @param level level object\r
+     */\r
+    public void setThreshold(Level level) {\r
+        this.hierarchy.setThreshold(level);\r
+    }\r
+\r
+    /**\r
+     * The string version of setThreshold(Level level)\r
+     * \r
+     * @param level level string\r
+     */\r
+    public void setThreshold(String level) {\r
+        this.hierarchy.setThreshold(level);\r
+    }\r
+\r
+    /**\r
+     * Get the repository-wide threshold.\r
+     * \r
+     * @return Level\r
+     */\r
+    public Level getThreshold() {\r
+        return this.hierarchy.getThreshold();\r
+    }\r
+\r
+    /**\r
+     * Returns a new logger instance named as the first parameter using the\r
+     * default factory. If a logger of that name already exists, then it will be\r
+     * returned. Otherwise, a new logger will be instantiated and then linked\r
+     * with its existing ancestors as well as children.\r
+     * \r
+     * @param clazz the class to get the logger for\r
+     * @return Logger\r
+     */\r
+    public Logger getLogger(Class<?> clazz) {\r
+        return this.hierarchy.getLogger(clazz.getName());\r
+    }\r
+\r
+    /**\r
+     * Returns a new logger instance named as the first parameter using the\r
+     * default factory. If a logger of that name already exists, then it will be\r
+     * returned. Otherwise, a new logger will be instantiated and then linked\r
+     * with its existing ancestors as well as children.\r
+     * \r
+     * @param name logger name\r
+     * @return Logger\r
+     */\r
+    public Logger getLogger(String name) {\r
+        return this.hierarchy.getLogger(name);\r
+    }\r
+\r
+    /**\r
+     * The same as getLogger(String name) but using a factory instance instead\r
+     * of a default factory.\r
+     * \r
+     * @param name logger name\r
+     * @param factory factory instance\r
+     * @return Logger\r
+     */\r
+    public Logger getLogger(String name, LoggerFactory factory) {\r
+        return this.hierarchy.getLogger(name, factory);\r
+    }\r
+\r
+    /**\r
+     * Returns the root of this hierarchy.\r
+     * \r
+     * @return Logger\r
+     */\r
+    public Logger getRootLogger() {\r
+        return this.hierarchy.getRootLogger();\r
+    }\r
+\r
+    /**\r
+     * Checks if this logger exists.\r
+     * \r
+     * @return Logger\r
+     */\r
+    public Logger exists(String name) {\r
+        return this.hierarchy.exists(name);\r
+    }\r
+\r
+    /**\r
+     * Disposes the logger hierarchy\r
+     */\r
+    public void shutdown() {\r
+        this.hierarchy.shutdown();\r
+    }\r
+\r
+    /**\r
+     * Resets configuration values to its defaults.\r
+     */\r
+    public void resetConfiguration() {\r
+        this.hierarchy.resetConfiguration();\r
+    }\r
+\r
+}
\ No newline at end of file