d460d7f9e4ebd7ca5b7a46487fd0eda0cf7760a7
[packages/trusty/mysql-wsrep-5.6.git] /
1 /*
2    Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
16 */
17
18 package com.mysql.clusterj.core.util;
19
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.logging.LogManager;
23
24 public class JDK14LoggerFactoryImpl implements LoggerFactory {
25
26     /** The root logger name */
27     public static final String CLUSTERJ_LOGGER = "com.mysql.clusterj.core";
28
29     /** The metadata logger name */
30     public static final String CLUSTERJ_METADATA_LOGGER = "com.mysql.clusterj.core.metadata";
31
32     /** The util logger name */
33     public static final String CLUSTERJ_UTIL_LOGGER = "com.mysql.clusterj.core.util";
34
35     /** The query logger name */
36     public static final String CLUSTERJ_QUERY_LOGGER = "com.mysql.clusterj.core.query";
37
38     /** The global JDK14 LogManager */
39     static final LogManager logManager = LogManager.getLogManager();
40
41     /** The loggers in a map */
42     static final Map<String, Logger> loggerMap = new HashMap<String, Logger>();
43
44     /** The constructor */
45     public JDK14LoggerFactoryImpl() {
46         // configureJDK14Logger();
47         // create all the known loggers for the core project
48         registerLogger(CLUSTERJ_LOGGER);
49         registerLogger(CLUSTERJ_METADATA_LOGGER);
50         registerLogger(CLUSTERJ_QUERY_LOGGER);
51         registerLogger(CLUSTERJ_UTIL_LOGGER);
52     }
53
54     public Logger registerLogger(String loggerName) {
55         java.util.logging.Logger logger = java.util.logging.Logger.getLogger(loggerName);
56         Logger result = new JDK14LoggerImpl(logger);
57         loggerMap.put(loggerName, result);
58         return result;
59     }
60
61     @SuppressWarnings("unchecked")
62     public Logger getInstance(Class cls) {
63         String loggerName = getPackageName(cls);
64         return getInstance(loggerName);
65     }
66
67     public synchronized Logger getInstance(String loggerName) {
68         Logger result = loggerMap.get(loggerName);
69         if (result == null) {
70             result = registerLogger(loggerName);
71         }
72         return result;
73     }
74
75     /**  
76      * Returns the package portion of the specified class.
77      * @param cls the class from which to extract the 
78      * package 
79      * @return package portion of the specified class
80      */   
81     final private static String getPackageName(Class<?> cls)
82     {
83         String className = cls.getName();
84         int index = className.lastIndexOf('.');
85         return ((index != -1) ? className.substring(0, index) : ""); // NOI18N
86     }
87
88 }