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