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;
\r
14 import java.util.Properties;
\r
16 import org.simantics.internal.Activator;
\r
20 public class Logger {
\r
21 public static final boolean ECHO = true;
\r
22 public static final Properties defaultProperties = new Properties();
\r
24 defaultProperties.put("log4j.rootCategory", "INFO, default");
\r
25 defaultProperties.put("log4j.appender.default", "org.apache.log4j.FileAppender");
\r
26 defaultProperties.put("log4j.appender.default.File", Activator.LOG_FILE_NAME);
\r
27 defaultProperties.put("log4j.appender.default.append", "false");
\r
28 defaultProperties.put("log4j.appender.default.layout", "org.apache.log4j.PatternLayout");
\r
29 defaultProperties.put("log4j.appender.default.layout.ConversionPattern", "%d{ISO8601} %-6r [%15.15t] %-5p %30.30c - %m%n");
\r
31 private static LogManager defaultLogManager = new LogManager(defaultProperties);
\r
32 private static final Logger defaultErrorLogger = new Logger(LogManager.class);
\r
33 private org.apache.log4j.Logger logger;
\r
34 Logger(Class<?> clazz) {
\r
35 logger = defaultLogManager.getLogger(clazz);
\r
39 * Log a trace event.
\r
41 * @param message message of the trace
\r
42 * @param exception the exception, or <code>null</code>
\r
44 public void logTrace(String message, Throwable exception) {
\r
45 // Errors are much more useful with a stack trace!
\r
46 if (exception == null) {
\r
47 exception = new RuntimeException();
\r
49 logger.trace(message, exception);
\r
53 * Log an info event.
\r
55 * @param message message of the info
\r
56 * @param exception the exception, or <code>null</code>
\r
58 public void logInfo(String message, Throwable exception) {
\r
59 // Errors are much more useful with a stack trace!
\r
60 if (exception == null) {
\r
61 exception = new RuntimeException();
\r
63 logger.info(message, exception);
\r
67 * Log a warning event.
\r
69 * @param message message of the warning
\r
70 * @param exception the exception, or <code>null</code>
\r
72 public void logWarning(String message, Throwable exception) {
\r
73 // Errors are much more useful with a stack trace!
\r
74 if (exception == null) {
\r
75 exception = new RuntimeException();
\r
77 logger.warn(message, exception);
\r
81 * Log an error event.
\r
83 * @param message message of the error
\r
84 * @param exception the exception, or <code>null</code>
\r
86 public void logError(String message, Throwable exception) {
\r
87 // Errors are much more useful with a stack trace!
\r
88 if (exception == null) {
\r
89 exception = new RuntimeException();
\r
91 logger.error(message, exception);
\r
94 public static Logger getDefault() {
\r
95 return defaultErrorLogger;
\r
98 public static LogManager getDefaultLogManager() {
\r
99 return defaultLogManager;
\r
101 public static void defaultLogError(Throwable exception) {
\r
102 getDefault().logError(exception.getLocalizedMessage(), exception);
\r
103 if(ECHO) exception.printStackTrace();
\r
105 public static void defaultLogError(String message) {
\r
106 getDefault().logError(message, null);
\r
108 System.err.println(message);
\r
110 public static void defaultLogError(String message, Throwable exception) {
\r
111 getDefault().logError(message, exception);
\r
113 System.err.println(message);
\r
115 public static void defaultLogInfo(String message) {
\r
116 getDefault().logInfo(message, null);
\r
118 System.err.println(message);
\r
120 public static void defaultLogTrace(String message) {
\r
121 getDefault().logTrace(message, null);
\r
123 System.err.println(message);
\r