View Javadoc
1   /*
2    * #%L
3    * LogFactory.java - mongodb-async-driver - Allanbank Consulting, Inc.
4    * %%
5    * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.allanbank.mongodb.util.log;
21  
22  /**
23   * LogFactory supports the creation of the Log instances.
24   * 
25   * @api.no This class is <b>NOT</b> part of the drivers API. This class may be
26   *         mutated in incompatible ways between any two releases of the driver.
27   * @copyright 2014, Allanbank Consulting, Inc., All Rights Reserved
28   */
29  public abstract class LogFactory {
30  
31      /** The {@link LogFactory} instance. */
32      private volatile static LogFactory ourInstance;
33  
34      /**
35       * Creates a {@link Log} instance for the provided class.
36       * 
37       * @param clazz
38       *            The name of the class to create a log instance for.
39       * @return The {@link Log} instance for the class.
40       */
41      public static Log getLog(final Class<?> clazz) {
42          LogFactory factory = ourInstance;
43          if (factory == null) {
44              try {
45                  factory = new Slf4jLogFactory();
46              }
47              catch (final RuntimeException e) {
48                  factory = new JulLogFactory();
49              }
50              ourInstance = factory;
51          }
52          return factory.doGetLog(clazz);
53      }
54  
55      /**
56       * Resets the logger factory being used. Provided for testing.
57       */
58      /* package */static void reset() {
59          ourInstance = null;
60      }
61  
62      /**
63       * Creates a new {@link LogFactory}.
64       */
65      protected LogFactory() {
66          super();
67      }
68  
69      /**
70       * Delegate method for the instantiated {@link LogFactory}.
71       * 
72       * @param clazz
73       *            The name of the class to create a log instance for.
74       * @return The {@link Log} instance for the class.
75       */
76      protected abstract Log doGetLog(Class<?> clazz);
77  }