]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/logging/Logger.java
Sync git svn branch with SVN repository r33382.
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / logging / 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.utils.logging;\r
13 \r
14 import java.util.Properties;\r
15 \r
16 abstract public class Logger {\r
17 \r
18         public static final boolean ECHO = false;\r
19     \r
20     protected static final Properties defaultProperties(String ... keyValuePairs) {\r
21 \r
22         assert(keyValuePairs.length % 2 == 0);\r
23         \r
24         Properties defaultProperties = new Properties();\r
25         defaultProperties.put("log4j.rootCategory", "ERROR, default");\r
26         defaultProperties.put("log4j.appender.default", "org.apache.log4j.FileAppender");\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", "%-6r [%15.15t] %-5p %30.30c - %m%n");\r
30         \r
31         for(int i=0;i<keyValuePairs.length;) {\r
32                 String key = keyValuePairs[i++];\r
33                 String value = keyValuePairs[i++];\r
34                 defaultProperties.put(key, value);\r
35         }\r
36         \r
37         return defaultProperties; \r
38         \r
39     }\r
40     \r
41     final protected org.apache.log4j.Logger logger;\r
42     \r
43     protected Logger(org.apache.log4j.Logger logger) {\r
44         this.logger = logger;\r
45     }\r
46 \r
47     final protected org.apache.log4j.Logger getLogger() {\r
48         return logger;\r
49     }\r
50     \r
51     /**\r
52      * Log a trace event.\r
53      * \r
54      * @param message message of the trace\r
55      * @param exception the exception, or <code>null</code>\r
56      */\r
57     public void logTrace(String message, Throwable exception) {\r
58         // Errors are much more useful with a stack trace!\r
59         if (exception == null) {\r
60             exception = new RuntimeException();\r
61         }\r
62         getLogger().trace(message, exception);\r
63     }\r
64 \r
65     /**\r
66      * Log an info event.\r
67      * \r
68      * @param message message of the info\r
69      * @param exception the exception, or <code>null</code>\r
70      */\r
71     public void logInfo(String message, Throwable exception) {\r
72         // Errors are much more useful with a stack trace!\r
73         if (exception == null) {\r
74             exception = new RuntimeException();\r
75         }\r
76         getLogger().info(message, exception);\r
77     }\r
78 \r
79     /**\r
80      * Log an error event.\r
81      * \r
82      * @param message message of the error\r
83      * @param exception the exception, or <code>null</code>\r
84      */\r
85     public void logError(String message, Throwable exception) {\r
86         \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         getLogger().error(message, exception);\r
92     }\r
93     \r
94     /**\r
95      * Log an error event.\r
96      * \r
97      * @param message message of the error\r
98      * @param exception the exception, or <code>null</code>\r
99      */\r
100     public void logWarning(String message, Throwable exception) {\r
101         // Errors are much more useful with a stack trace!\r
102         if (exception == null) {\r
103             exception = new RuntimeException();\r
104         }\r
105         getLogger().error(message, exception);\r
106     }\r
107     \r
108 }\r