1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics;
14 import java.util.Properties;
16 import org.apache.log4j.Hierarchy;
17 import org.apache.log4j.Level;
18 import org.apache.log4j.Logger;
19 import org.apache.log4j.PropertyConfigurator;
20 import org.apache.log4j.spi.LoggerFactory;
21 import org.apache.log4j.spi.RootLogger;
24 * This class encapsulates a Log4J Hierarchy and centralizes all Logger access.
26 public class LogManager {
28 private Hierarchy hierarchy;
31 * Creates a new LogManager. Saves the log and state location.
32 * Creates a new Hierarchy and add a new EventListener to it.
33 * Configure the hierarchy with the properties passed. Add this object to
34 * the list of active log managers.
36 * @param properties log configuration properties
38 public LogManager(Properties properties) {
39 this.hierarchy = new Hierarchy(new RootLogger(Level.DEBUG));
40 new PropertyConfigurator().doConfigure(properties, this.hierarchy);
44 * Checks if this PluginLogManager is disabled for this level.
46 * @param level level value
47 * @return boolean true if it is disabled
49 public boolean isDisabled(int level) {
50 return this.hierarchy.isDisabled(level);
54 * Enable logging for logging requests with level l or higher. By default
55 * all levels are enabled.
57 * @param level level object
59 public void setThreshold(Level level) {
60 this.hierarchy.setThreshold(level);
64 * The string version of setThreshold(Level level)
66 * @param level level string
68 public void setThreshold(String level) {
69 this.hierarchy.setThreshold(level);
73 * Get the repository-wide threshold.
77 public Level getThreshold() {
78 return this.hierarchy.getThreshold();
82 * Returns a new logger instance named as the first parameter using the
83 * default factory. If a logger of that name already exists, then it will be
84 * returned. Otherwise, a new logger will be instantiated and then linked
85 * with its existing ancestors as well as children.
87 * @param clazz the class to get the logger for
90 public Logger getLogger(Class<?> clazz) {
91 return this.hierarchy.getLogger(clazz.getName());
95 * Returns a new logger instance named as the first parameter using the
96 * default factory. If a logger of that name already exists, then it will be
97 * returned. Otherwise, a new logger will be instantiated and then linked
98 * with its existing ancestors as well as children.
100 * @param name logger name
103 public Logger getLogger(String name) {
104 return this.hierarchy.getLogger(name);
108 * The same as getLogger(String name) but using a factory instance instead
109 * of a default factory.
111 * @param name logger name
112 * @param factory factory instance
115 public Logger getLogger(String name, LoggerFactory factory) {
116 return this.hierarchy.getLogger(name, factory);
120 * Returns the root of this hierarchy.
124 public Logger getRootLogger() {
125 return this.hierarchy.getRootLogger();
129 * Checks if this logger exists.
133 public Logger exists(String name) {
134 return this.hierarchy.exists(name);
138 * Disposes the logger hierarchy
140 public void shutdown() {
141 this.hierarchy.shutdown();
145 * Resets configuration values to its defaults.
147 public void resetConfiguration() {
148 this.hierarchy.resetConfiguration();