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