]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/Logger.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / Logger.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram;
13
14 import java.util.Properties;
15
16 public class Logger {
17     public static final boolean ECHO = false;
18     public static final Properties defaultProperties = new Properties();
19     static {
20         defaultProperties.put("log4j.rootCategory", "ERROR, default");
21         defaultProperties.put("log4j.appender.default", "org.apache.log4j.FileAppender");
22         defaultProperties.put("log4j.appender.default.File", "diagram.log");
23         defaultProperties.put("log4j.appender.default.append", "false");
24         defaultProperties.put("log4j.appender.default.layout", "org.apache.log4j.PatternLayout");
25         defaultProperties.put("log4j.appender.default.layout.ConversionPattern", "%-6r [%15.15t] %-5p %30.30c - %m%n");
26     }
27     private static LogManager defaultLogManager = new LogManager(defaultProperties);
28     private static final Logger defaultErrorLogger = new Logger(LogManager.class);
29     private org.apache.log4j.Logger logger;
30     Logger(Class<?> clazz) {
31         logger = defaultLogManager.getLogger(clazz);
32     }
33
34     /**
35      * Log a trace event.
36      * 
37      * @param message message of the trace
38      * @param exception the exception, or <code>null</code>
39      */
40     public void logTrace(String message, Throwable exception) {
41         // Errors are much more useful with a stack trace!
42         if (exception == null) {
43             exception = new RuntimeException();
44         }
45         logger.trace(message, exception);
46     }
47
48     /**
49      * Log an info event.
50      * 
51      * @param message message of the info
52      * @param exception the exception, or <code>null</code>
53      */
54     public void logInfo(String message, Throwable exception) {
55         // Errors are much more useful with a stack trace!
56         if (exception == null) {
57             exception = new RuntimeException();
58         }
59         logger.info(message, exception);
60     }
61
62     /**
63      * Log an error event.
64      * 
65      * @param message message of the error
66      * @param exception the exception, or <code>null</code>
67      */
68     public void logError(String message, Throwable exception) {
69         // Errors are much more useful with a stack trace!
70         if (exception == null) {
71             exception = new RuntimeException();
72         }
73         logger.error(message, exception);
74     }
75
76     public static Logger getDefault() {
77         return defaultErrorLogger;
78     }
79     
80     public static LogManager getDefaultLogManager() {
81         return defaultLogManager;
82     }
83     public static void defaultLogError(Throwable exception) {
84         getDefault().logError(exception.getLocalizedMessage(), exception);
85         if(ECHO) exception.printStackTrace();
86     }
87     public static void defaultLogError(String message) {
88         getDefault().logError(message, null);
89         if(ECHO)
90             System.err.println(message);
91     }
92     public static void defaultLogError(String message, Throwable exception) {
93         getDefault().logError(message, exception);
94         if(ECHO)
95             System.err.println(message);
96     }
97     public static void defaultLogInfo(String message) {
98         getDefault().logInfo(message, null);
99         if(ECHO)
100             System.err.println(message);
101     }
102     public static void defaultLogTrace(String message) {
103         getDefault().logTrace(message, null);
104         if(ECHO)
105             System.err.println(message);
106     }
107 }