]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/utils/Logger.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / utils / 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.db.common.utils;\r
13 \r
14 import java.util.Properties;\r
15 \r
16 import org.apache.log4j.Level;\r
17 import org.simantics.databoard.Bindings;\r
18 import org.simantics.db.DevelopmentKeys;\r
19 import org.simantics.db.common.internal.config.InternalClientConfig;\r
20 import org.simantics.utils.Development;\r
21 \r
22 public class Logger {\r
23     public static final boolean ENABLED = true;\r
24     //public static final boolean ECHO = Development.DEVELOPMENT || false;\r
25     public static final Properties defaultProperties = new Properties();\r
26     static {\r
27         defaultProperties.put("log4j.rootCategory", "ERROR, default");\r
28         defaultProperties.put("log4j.appender.default", "org.apache.log4j.FileAppender");\r
29         defaultProperties.put("log4j.appender.default.File", InternalClientConfig.DB_CLIENT_LOG_FILE);\r
30         defaultProperties.put("log4j.appender.default.append", "true");\r
31         defaultProperties.put("log4j.appender.default.layout", "org.apache.log4j.PatternLayout");\r
32         defaultProperties.put("log4j.appender.default.layout.ConversionPattern", "%d{ISO8601} %-6r [%15.15t] %-5p %30.30c - %m%n");\r
33     }\r
34     private static LogManager defaultLogManager = new LogManager(defaultProperties);\r
35     private static final Logger defaultErrorLogger = new Logger(LogManager.class);\r
36     private org.apache.log4j.Logger logger;\r
37     Logger(Class<?> clazz) {\r
38         logger = defaultLogManager.getLogger(clazz);\r
39     }\r
40 \r
41     /**\r
42      * Log a trace event.\r
43      *\r
44      * @param message message of the trace\r
45      * @param exception the exception, or <code>null</code>\r
46      */\r
47     public void logTrace(String message, Throwable exception) {\r
48 \r
49         if(!logger.isTraceEnabled()) return;\r
50 \r
51         // Errors are much more useful with a stack trace!\r
52         if (exception == null) {\r
53             exception = new RuntimeException();\r
54         }\r
55         logger.trace(message, exception);\r
56 \r
57         if (Development.DEVELOPMENT) {\r
58             if(Development.<Boolean>getProperty(DevelopmentKeys.LOGGER_ECHO, Bindings.BOOLEAN)) {\r
59                 System.err.println("Logger.logTrace: " + message);\r
60             }\r
61         }\r
62 \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 \r
73         if(!logger.isInfoEnabled()) return;\r
74 \r
75         // Errors are much more useful with a stack trace!\r
76         if (exception == null) {\r
77             exception = new RuntimeException();\r
78         }\r
79         logger.info(message, exception);\r
80 \r
81         if (Development.DEVELOPMENT) {\r
82             if(Development.<Boolean>getProperty(DevelopmentKeys.LOGGER_ECHO, Bindings.BOOLEAN)) {\r
83                 System.err.println("Logger.logInfo: " + message);\r
84             }\r
85         }\r
86 \r
87     }\r
88 \r
89     /**\r
90      * Log an error event.\r
91      *\r
92      * @param message message of the error\r
93      * @param exception the exception, or <code>null</code>\r
94      */\r
95     public void logError(String message, Throwable exception) {\r
96 \r
97         if(!logger.isEnabledFor(Level.ERROR)) return;\r
98 \r
99         // Errors are much more useful with a stack trace!\r
100         if (exception == null) {\r
101             exception = new RuntimeException();\r
102         }\r
103         logger.error(message, exception);\r
104 \r
105         if (Development.DEVELOPMENT) {\r
106             if(Development.<Boolean>getProperty(DevelopmentKeys.LOGGER_ECHO, Bindings.BOOLEAN)) {\r
107                 System.err.println("Logger.logError: " + message);\r
108             }\r
109         }\r
110 \r
111     }\r
112 \r
113     /**\r
114      * Log an error event.\r
115      *\r
116      * @param message message of the error\r
117      * @param exception the exception, or <code>null</code>\r
118      */\r
119     public void logWarning(String message, Throwable exception) {\r
120 \r
121         if(!logger.isEnabledFor(Level.WARN)) return;\r
122 \r
123         // Errors are much more useful with a stack trace!\r
124         if (exception == null) {\r
125             exception = new RuntimeException();\r
126         }\r
127         logger.error(message, exception);\r
128 \r
129         if (Development.DEVELOPMENT) {\r
130             if(Development.<Boolean>getProperty(DevelopmentKeys.LOGGER_ECHO, Bindings.BOOLEAN)) {\r
131                 System.err.println("Logger.logWarning: " + message);\r
132             }\r
133         }\r
134 \r
135     }\r
136 \r
137     /**\r
138      * Log message.\r
139      *\r
140      * @param message to log.\r
141      */\r
142     public void logMessage(String message) {\r
143         Level level = logger.getLevel();\r
144         boolean toggle = !logger.isInfoEnabled();\r
145         if (toggle)\r
146             logger.setLevel((Level)Level.INFO);\r
147         logger.info(message);\r
148         if (toggle)\r
149             logger.setLevel(level);\r
150         if (Development.DEVELOPMENT) {\r
151             if(Development.<Boolean>getProperty(DevelopmentKeys.LOGGER_ECHO, Bindings.BOOLEAN)) {\r
152                 System.err.println("Logger.logMessage: " + message);\r
153             }\r
154         }\r
155     }\r
156 \r
157     public static Logger getDefault() {\r
158         return defaultErrorLogger;\r
159     }\r
160 \r
161     public static LogManager getDefaultLogManager() {\r
162         return defaultLogManager;\r
163     }\r
164     public static void defaultLogError(Throwable exception) {\r
165         if(!ENABLED) return;\r
166         getDefault().logError(exception.getLocalizedMessage(), exception);\r
167     }\r
168     public static void defaultLogError(String message) {\r
169         if(!ENABLED) return;\r
170         getDefault().logError(message, null);\r
171     }\r
172     public static void defaultLogError(String message, Throwable exception) {\r
173         if(!ENABLED) return;\r
174         getDefault().logError(message, exception);\r
175     }\r
176     public static void defaultLogInfo(String message) {\r
177         if(!ENABLED) return;\r
178         getDefault().logInfo(message, null);\r
179     }\r
180     public static void defaultLogTrace(String message) {\r
181         if(!ENABLED) return;\r
182         getDefault().logTrace(message, null);\r
183     }\r
184     public static void defaultLog(String message) {\r
185         if(!ENABLED) return;\r
186         getDefault().logMessage(message);\r
187     }\r
188 }\r