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