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.diagram;
\r
14 import java.util.Properties;
\r
16 public class Logger {
\r
17 public static final boolean ECHO = false;
\r
18 public static final Properties defaultProperties = new Properties();
\r
20 defaultProperties.put("log4j.rootCategory", "ERROR, default");
\r
21 defaultProperties.put("log4j.appender.default", "org.apache.log4j.FileAppender");
\r
22 defaultProperties.put("log4j.appender.default.File", "diagram.log");
\r
23 defaultProperties.put("log4j.appender.default.append", "false");
\r
24 defaultProperties.put("log4j.appender.default.layout", "org.apache.log4j.PatternLayout");
\r
25 defaultProperties.put("log4j.appender.default.layout.ConversionPattern", "%-6r [%15.15t] %-5p %30.30c - %m%n");
\r
27 private static LogManager defaultLogManager = new LogManager(defaultProperties);
\r
28 private static final Logger defaultErrorLogger = new Logger(LogManager.class);
\r
29 private org.apache.log4j.Logger logger;
\r
30 Logger(Class<?> clazz) {
\r
31 logger = defaultLogManager.getLogger(clazz);
\r
35 * Log a trace event.
\r
37 * @param message message of the trace
\r
38 * @param exception the exception, or <code>null</code>
\r
40 public void logTrace(String message, Throwable exception) {
\r
41 // Errors are much more useful with a stack trace!
\r
42 if (exception == null) {
\r
43 exception = new RuntimeException();
\r
45 logger.trace(message, exception);
\r
49 * Log an info event.
\r
51 * @param message message of the info
\r
52 * @param exception the exception, or <code>null</code>
\r
54 public void logInfo(String message, Throwable exception) {
\r
55 // Errors are much more useful with a stack trace!
\r
56 if (exception == null) {
\r
57 exception = new RuntimeException();
\r
59 logger.info(message, exception);
\r
63 * Log an error event.
\r
65 * @param message message of the error
\r
66 * @param exception the exception, or <code>null</code>
\r
68 public void logError(String message, Throwable exception) {
\r
69 // Errors are much more useful with a stack trace!
\r
70 if (exception == null) {
\r
71 exception = new RuntimeException();
\r
73 logger.error(message, exception);
\r
76 public static Logger getDefault() {
\r
77 return defaultErrorLogger;
\r
80 public static LogManager getDefaultLogManager() {
\r
81 return defaultLogManager;
\r
83 public static void defaultLogError(Throwable exception) {
\r
84 getDefault().logError(exception.getLocalizedMessage(), exception);
\r
85 if(ECHO) exception.printStackTrace();
\r
87 public static void defaultLogError(String message) {
\r
88 getDefault().logError(message, null);
\r
90 System.err.println(message);
\r
92 public static void defaultLogError(String message, Throwable exception) {
\r
93 getDefault().logError(message, exception);
\r
95 System.err.println(message);
\r
97 public static void defaultLogInfo(String message) {
\r
98 getDefault().logInfo(message, null);
\r
100 System.err.println(message);
\r
102 public static void defaultLogTrace(String message) {
\r
103 getDefault().logTrace(message, null);
\r
105 System.err.println(message);
\r