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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.common.utils;
\r
14 import java.util.Properties;
\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
24 * This class encapsulates a Log4J Hierarchy and centralizes all Logger access.
\r
26 public class LogManager {
\r
28 private Hierarchy hierarchy;
\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
36 * @param properties log configuration properties
\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
44 * Checks if this PluginLogManager is disabled for this level.
\r
46 * @param level level value
\r
47 * @return boolean true if it is disabled
\r
49 public boolean isDisabled(int level) {
\r
50 return this.hierarchy.isDisabled(level);
\r
54 * Enable logging for logging requests with level l or higher. By default
\r
55 * all levels are enabled.
\r
57 * @param level level object
\r
59 public void setThreshold(Level level) {
\r
60 this.hierarchy.setThreshold(level);
\r
64 * The string version of setThreshold(Level level)
\r
66 * @param level level string
\r
68 public void setThreshold(String level) {
\r
69 this.hierarchy.setThreshold(level);
\r
73 * Get the repository-wide threshold.
\r
77 public Level getThreshold() {
\r
78 return this.hierarchy.getThreshold();
\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
87 * @param clazz the class to get the logger for
\r
90 public Logger getLogger(Class<?> clazz) {
\r
91 return this.hierarchy.getLogger(clazz.getName());
\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
100 * @param name logger name
\r
103 public Logger getLogger(String name) {
\r
104 return this.hierarchy.getLogger(name);
\r
108 * The same as getLogger(String name) but using a factory instance instead
\r
109 * of a default factory.
\r
111 * @param name logger name
\r
112 * @param factory factory instance
\r
115 public Logger getLogger(String name, LoggerFactory factory) {
\r
116 return this.hierarchy.getLogger(name, factory);
\r
120 * Returns the root of this hierarchy.
\r
124 public Logger getRootLogger() {
\r
125 return this.hierarchy.getRootLogger();
\r
129 * Checks if this logger exists.
\r
133 public Logger exists(String name) {
\r
134 return this.hierarchy.exists(name);
\r
138 * Disposes the logger hierarchy
\r
140 public void shutdown() {
\r
141 this.hierarchy.shutdown();
\r
145 * Resets configuration values to its defaults.
\r
147 public void resetConfiguration() {
\r
148 this.hierarchy.resetConfiguration();
\r