]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/utils/ErrorLogger.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / utils / ErrorLogger.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.db.common.utils;\r
13 \r
14 import java.util.Properties;\r
15 \r
16 import org.apache.log4j.Logger;\r
17 \r
18 /*\r
19  * \r
20  * @deprecated in favor of org.simantics.db.common.Logger. Will be removed in Simantics 1.2\r
21  * \r
22  */\r
23 @Deprecated\r
24 public class ErrorLogger {\r
25     public static final boolean ECHO = false;\r
26     public static final Properties defaultProperties = new Properties();\r
27     static {\r
28         defaultProperties.put("log4j.rootCategory", "ERROR, default");\r
29         defaultProperties.put("log4j.appender.default", "org.apache.log4j.FileAppender");\r
30         defaultProperties.put("log4j.appender.default.File", "db-client-deprecated.log");\r
31         defaultProperties.put("log4j.appender.default.append", "false");\r
32         defaultProperties.put("log4j.appender.default.layout", "org.apache.log4j.PatternLayout");\r
33         defaultProperties.put("log4j.appender.default.layout.ConversionPattern", "%-6r [%15.15t] %-5p %30.30c - %m%n");\r
34     }\r
35     private static LogManager defaultLogManager = new LogManager(defaultProperties);\r
36     private static final ErrorLogger defaultErrorLogger = new ErrorLogger(LogManager.class);\r
37     private Logger logger;\r
38     ErrorLogger(Class<?> clazz) {\r
39         logger = defaultLogManager.getLogger(clazz);\r
40     }\r
41 \r
42     /**\r
43      * Log a trace event.\r
44      * \r
45      * @param message message of the trace\r
46      * @param exception the exception, or <code>null</code>\r
47      */\r
48     public void logTrace(String message, Throwable exception) {\r
49         // Errors are much more useful with a stack trace!\r
50         if (exception == null) {\r
51             exception = new RuntimeException();\r
52         }\r
53         logger.trace(message, exception);\r
54     }\r
55 \r
56     /**\r
57      * Log an info event.\r
58      * \r
59      * @param message message of the info\r
60      * @param exception the exception, or <code>null</code>\r
61      */\r
62     public void logInfo(String message, Throwable exception) {\r
63         // Errors are much more useful with a stack trace!\r
64         if (exception == null) {\r
65             exception = new RuntimeException();\r
66         }\r
67         logger.info(message, exception);\r
68     }\r
69 \r
70     /**\r
71      * Log an error event.\r
72      * \r
73      * @param message message of the error\r
74      * @param exception the exception, or <code>null</code>\r
75      */\r
76     public void logError(String message, Throwable exception) {\r
77         // Errors are much more useful with a stack trace!\r
78         if (exception == null) {\r
79             exception = new RuntimeException();\r
80         }\r
81         logger.error(message, exception);\r
82     }\r
83 \r
84     public static ErrorLogger getDefault() {\r
85         return defaultErrorLogger;\r
86     }\r
87     \r
88     public static LogManager getDefaultLogManager() {\r
89         return defaultLogManager;\r
90     }\r
91     public static void defaultLogError(Throwable exception) {\r
92         getDefault().logError(exception.getLocalizedMessage(), exception);\r
93         if(ECHO) exception.printStackTrace();\r
94     }\r
95     public static void defaultLogError(String message) {\r
96         getDefault().logError(message, null);\r
97         if(ECHO)\r
98             System.err.println(message);\r
99     }\r
100     public static void defaultLogError(String message, Throwable exception) {\r
101         getDefault().logError(message, exception);\r
102         if(ECHO)\r
103             System.err.println(message);\r
104     }\r
105     public static void defaultLogInfo(String message) {\r
106         getDefault().logInfo(message, null);\r
107         if(ECHO)\r
108             System.err.println(message);\r
109     }\r
110     public static void defaultLogTrace(String message) {\r
111         getDefault().logTrace(message, null);\r
112         if(ECHO)\r
113             System.err.println(message);\r
114     }\r
115 }\r