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