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.db.common.utils;
14 import java.util.Properties;
16 import org.apache.log4j.Level;
17 import org.simantics.databoard.Bindings;
18 import org.simantics.db.DevelopmentKeys;
19 import org.simantics.db.common.internal.config.InternalClientConfig;
20 import org.simantics.utils.Development;
23 public static final boolean ENABLED = true;
24 //public static final boolean ECHO = Development.DEVELOPMENT || false;
25 public static final Properties defaultProperties = new Properties();
27 defaultProperties.put("log4j.rootCategory", "ERROR, default");
28 defaultProperties.put("log4j.appender.default", "org.apache.log4j.FileAppender");
29 defaultProperties.put("log4j.appender.default.File", InternalClientConfig.DB_CLIENT_LOG_FILE);
30 defaultProperties.put("log4j.appender.default.append", "true");
31 defaultProperties.put("log4j.appender.default.layout", "org.apache.log4j.PatternLayout");
32 defaultProperties.put("log4j.appender.default.layout.ConversionPattern", "%d{ISO8601} %-6r [%15.15t] %-5p %30.30c - %m%n");
34 private static LogManager defaultLogManager = new LogManager(defaultProperties);
35 private static final Logger defaultErrorLogger = new Logger(LogManager.class);
36 private org.apache.log4j.Logger logger;
37 Logger(Class<?> clazz) {
38 logger = defaultLogManager.getLogger(clazz);
44 * @param message message of the trace
45 * @param exception the exception, or <code>null</code>
47 public void logTrace(String message, Throwable exception) {
49 if(!logger.isTraceEnabled()) return;
51 // Errors are much more useful with a stack trace!
52 if (exception == null) {
53 exception = new RuntimeException();
55 logger.trace(message, exception);
57 if (Development.DEVELOPMENT) {
58 if(Development.<Boolean>getProperty(DevelopmentKeys.LOGGER_ECHO, Bindings.BOOLEAN)) {
59 System.err.println("Logger.logTrace: " + message);
68 * @param message message of the info
69 * @param exception the exception, or <code>null</code>
71 public void logInfo(String message, Throwable exception) {
73 if(!logger.isInfoEnabled()) return;
75 // Errors are much more useful with a stack trace!
76 if (exception == null) {
77 exception = new RuntimeException();
79 logger.info(message, exception);
81 if (Development.DEVELOPMENT) {
82 if(Development.<Boolean>getProperty(DevelopmentKeys.LOGGER_ECHO, Bindings.BOOLEAN)) {
83 System.err.println("Logger.logInfo: " + message);
92 * @param message message of the error
93 * @param exception the exception, or <code>null</code>
95 public void logError(String message, Throwable exception) {
97 if(!logger.isEnabledFor(Level.ERROR)) return;
99 // Errors are much more useful with a stack trace!
100 if (exception == null) {
101 exception = new RuntimeException();
103 logger.error(message, exception);
105 if (Development.DEVELOPMENT) {
106 if(Development.<Boolean>getProperty(DevelopmentKeys.LOGGER_ECHO, Bindings.BOOLEAN)) {
107 System.err.println("Logger.logError: " + message);
114 * Log an error event.
116 * @param message message of the error
117 * @param exception the exception, or <code>null</code>
119 public void logWarning(String message, Throwable exception) {
121 if(!logger.isEnabledFor(Level.WARN)) return;
123 // Errors are much more useful with a stack trace!
124 if (exception == null) {
125 exception = new RuntimeException();
127 logger.error(message, exception);
129 if (Development.DEVELOPMENT) {
130 if(Development.<Boolean>getProperty(DevelopmentKeys.LOGGER_ECHO, Bindings.BOOLEAN)) {
131 System.err.println("Logger.logWarning: " + message);
140 * @param message to log.
142 public void logMessage(String message) {
143 Level level = logger.getLevel();
144 boolean toggle = !logger.isInfoEnabled();
146 logger.setLevel((Level)Level.INFO);
147 logger.info(message);
149 logger.setLevel(level);
150 if (Development.DEVELOPMENT) {
151 if(Development.<Boolean>getProperty(DevelopmentKeys.LOGGER_ECHO, Bindings.BOOLEAN)) {
152 System.err.println("Logger.logMessage: " + message);
157 public static Logger getDefault() {
158 return defaultErrorLogger;
161 public static LogManager getDefaultLogManager() {
162 return defaultLogManager;
164 public static void defaultLogError(Throwable exception) {
166 getDefault().logError(exception.getLocalizedMessage(), exception);
168 public static void defaultLogError(String message) {
170 getDefault().logError(message, null);
172 public static void defaultLogError(String message, Throwable exception) {
174 getDefault().logError(message, exception);
176 public static void defaultLogInfo(String message) {
178 getDefault().logInfo(message, null);
180 public static void defaultLogTrace(String message) {
182 getDefault().logTrace(message, null);
184 public static void defaultLog(String message) {
186 getDefault().logMessage(message);