]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/Logger.java
Logger fixes after merge commit:fdbe8762
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / Logger.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.diagram;\r
13 \r
14 import java.util.Properties;\r
15 \r
16 public class Logger {\r
17     public static final boolean ECHO = false;\r
18     public static final Properties defaultProperties = new Properties();\r
19     static {\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
26     }\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
32     }\r
33 \r
34     /**\r
35      * Log a trace event.\r
36      * \r
37      * @param message message of the trace\r
38      * @param exception the exception, or <code>null</code>\r
39      */\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
44         }\r
45         logger.trace(message, exception);\r
46     }\r
47 \r
48     /**\r
49      * Log an info event.\r
50      * \r
51      * @param message message of the info\r
52      * @param exception the exception, or <code>null</code>\r
53      */\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
58         }\r
59         logger.info(message, exception);\r
60     }\r
61 \r
62     /**\r
63      * Log an error event.\r
64      * \r
65      * @param message message of the error\r
66      * @param exception the exception, or <code>null</code>\r
67      */\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
72         }\r
73         logger.error(message, exception);\r
74     }\r
75 \r
76     public static Logger getDefault() {\r
77         return defaultErrorLogger;\r
78     }\r
79     \r
80     public static LogManager getDefaultLogManager() {\r
81         return defaultLogManager;\r
82     }\r
83     public static void defaultLogError(Throwable exception) {\r
84         getDefault().logError(exception.getLocalizedMessage(), exception);\r
85         if(ECHO) exception.printStackTrace();\r
86     }\r
87     public static void defaultLogError(String message) {\r
88         getDefault().logError(message, null);\r
89         if(ECHO)\r
90             System.err.println(message);\r
91     }\r
92     public static void defaultLogError(String message, Throwable exception) {\r
93         getDefault().logError(message, exception);\r
94         if(ECHO)\r
95             System.err.println(message);\r
96     }\r
97     public static void defaultLogInfo(String message) {\r
98         getDefault().logInfo(message, null);\r
99         if(ECHO)\r
100             System.err.println(message);\r
101     }\r
102     public static void defaultLogTrace(String message) {\r
103         getDefault().logTrace(message, null);\r
104         if(ECHO)\r
105             System.err.println(message);\r
106     }\r
107 }\r