]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.common.utils;\r
13 \r
14 import java.util.Properties;\r
15 \r
16 import org.apache.log4j.Hierarchy;\r
17 import org.apache.log4j.Level;\r
18 import org.apache.log4j.Logger;\r
19 import org.apache.log4j.PropertyConfigurator;\r
20 import org.apache.log4j.spi.LoggerFactory;\r
21 import org.apache.log4j.spi.RootLogger;\r
22 \r
23 /**\r
24  * This class encapsulates a Log4J Hierarchy and centralizes all Logger access.\r
25  */\r
26 public class LogManager {\r
27 \r
28     private Hierarchy hierarchy;\r
29 \r
30     /**\r
31      * Creates a new LogManager. Saves the log and state location.\r
32      * Creates a new Hierarchy and add a new EventListener to it.\r
33      * Configure the hierarchy with the properties passed. Add this object to\r
34      * the list of active log managers.\r
35      * \r
36      * @param properties log configuration properties\r
37      */\r
38     public LogManager(Properties properties) {\r
39         this.hierarchy = new Hierarchy(new RootLogger(Level.DEBUG));\r
40         new PropertyConfigurator().doConfigure(properties, this.hierarchy);\r
41     }\r
42 \r
43     /**\r
44      * Checks if this PluginLogManager is disabled for this level.\r
45      * \r
46      * @param level level value\r
47      * @return boolean true if it is disabled\r
48      */\r
49     public boolean isDisabled(int level) {\r
50         return this.hierarchy.isDisabled(level);\r
51     }\r
52 \r
53     /**\r
54      * Enable logging for logging requests with level l or higher. By default\r
55      * all levels are enabled.\r
56      * \r
57      * @param level level object\r
58      */\r
59     public void setThreshold(Level level) {\r
60         this.hierarchy.setThreshold(level);\r
61     }\r
62 \r
63     /**\r
64      * The string version of setThreshold(Level level)\r
65      * \r
66      * @param level level string\r
67      */\r
68     public void setThreshold(String level) {\r
69         this.hierarchy.setThreshold(level);\r
70     }\r
71 \r
72     /**\r
73      * Get the repository-wide threshold.\r
74      * \r
75      * @return Level\r
76      */\r
77     public Level getThreshold() {\r
78         return this.hierarchy.getThreshold();\r
79     }\r
80 \r
81     /**\r
82      * Returns a new logger instance named as the first parameter using the\r
83      * default factory. If a logger of that name already exists, then it will be\r
84      * returned. Otherwise, a new logger will be instantiated and then linked\r
85      * with its existing ancestors as well as children.\r
86      * \r
87      * @param clazz the class to get the logger for\r
88      * @return Logger\r
89      */\r
90     public Logger getLogger(Class<?> clazz) {\r
91         return this.hierarchy.getLogger(clazz.getName());\r
92     }\r
93 \r
94     /**\r
95      * Returns a new logger instance named as the first parameter using the\r
96      * default factory. If a logger of that name already exists, then it will be\r
97      * returned. Otherwise, a new logger will be instantiated and then linked\r
98      * with its existing ancestors as well as children.\r
99      * \r
100      * @param name logger name\r
101      * @return Logger\r
102      */\r
103     public Logger getLogger(String name) {\r
104         return this.hierarchy.getLogger(name);\r
105     }\r
106 \r
107     /**\r
108      * The same as getLogger(String name) but using a factory instance instead\r
109      * of a default factory.\r
110      * \r
111      * @param name logger name\r
112      * @param factory factory instance\r
113      * @return Logger\r
114      */\r
115     public Logger getLogger(String name, LoggerFactory factory) {\r
116         return this.hierarchy.getLogger(name, factory);\r
117     }\r
118 \r
119     /**\r
120      * Returns the root of this hierarchy.\r
121      * \r
122      * @return Logger\r
123      */\r
124     public Logger getRootLogger() {\r
125         return this.hierarchy.getRootLogger();\r
126     }\r
127 \r
128     /**\r
129      * Checks if this logger exists.\r
130      * \r
131      * @return Logger\r
132      */\r
133     public Logger exists(String name) {\r
134         return this.hierarchy.exists(name);\r
135     }\r
136 \r
137     /**\r
138      * Disposes the logger hierarchy\r
139      */\r
140     public void shutdown() {\r
141         this.hierarchy.shutdown();\r
142     }\r
143 \r
144     /**\r
145      * Resets configuration values to its defaults.\r
146      */\r
147     public void resetConfiguration() {\r
148         this.hierarchy.resetConfiguration();\r
149     }\r
150 \r
151 }