View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.control;
15  
16  import java.util.HashMap;
17  import java.util.Map;
18  
19  import ch.qos.logback.classic.Level;
20  import ch.qos.logback.core.CoreConstants;
21  
22  /**
23   * This logger context quite optimized for logger retrieval.
24   * 
25   * <p>It uses a single loggerMap where the key is the logger name and the value
26   * is the logger.
27   * 
28   * <p>This approach acts a lower limit for what is achievable for low memory
29   * usage as well as low creation/retrieval times. However, this simplicity also
30   * results in slow effective level evaluation, the most frequently exercised
31   * part of the API.
32   * 
33   * <p>This class is expected to contain correct results, and serve to verify
34   * the correctness of a more sophisticated implementation.
35   * 
36   * @author ceki
37   */
38  public class ControlLoggerContext {
39  
40      private ControlLogger root;
41      //
42      // Hashtable loggerMap = new Hashtable();
43      Map<String, ControlLogger> loggerMap = new HashMap<String, ControlLogger>();
44  
45      public ControlLoggerContext() {
46          this.root = new ControlLogger("root", null);
47          this.root.setLevel(Level.DEBUG);
48      }
49  
50      /**
51       * Return this contexts root logger
52       * 
53       * @return
54       */
55      public ControlLogger getRootLogger() {
56          return root;
57      }
58  
59      public ControlLogger exists(String name) {
60          if (name == null) {
61              throw new IllegalArgumentException("name parameter cannot be null");
62          }
63  
64          synchronized (loggerMap) {
65              return (ControlLogger) loggerMap.get(name);
66          }
67      }
68  
69      public final ControlLogger getLogger(String name) {
70          if (name == null) {
71              throw new IllegalArgumentException("name parameter cannot be null");
72          }
73  
74          synchronized (loggerMap) {
75              ControlLogger cl = (ControlLogger) loggerMap.get(name);
76              if (cl != null) {
77                  return cl;
78              }
79              ControlLogger parent = this.root;
80  
81              int i = 0;
82              while (true) {
83                  i = name.indexOf(CoreConstants.DOT, i);
84                  if (i == -1) {
85                      // System.out.println("FINAL-Creating logger named [" + name + "] with
86                      // parent " + parent.getName());
87                      cl = new ControlLogger(name, parent);
88                      loggerMap.put(name, cl);
89                      return cl;
90                  } else {
91                      String parentName = name.substring(0, i);
92                      ControlLogger p = (ControlLogger) loggerMap.get(parentName);
93                      if (p == null) {
94                          // System.out.println("INTERMEDIARY-Creating logger [" + parentName
95                          // + "] with parent " + parent.getName());
96                          p = new ControlLogger(parentName, parent);
97                          loggerMap.put(parentName, p);
98                      }
99                      parent = p;
100                 }
101                 // make i move past the last found dot.
102                 i++;
103             }
104         }
105     }
106 
107     public Map<String, ControlLogger> getLoggerMap() {
108         return loggerMap;
109     }
110 }