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>
26   * It uses a single loggerMap where the key is the logger name and the value is
27   * the logger.
28   * 
29   * <p>
30   * This approach acts a lower limit for what is achievable for low memory usage
31   * as well as low creation/retrieval times. However, this simplicity also
32   * results in slow effective level evaluation, the most frequently exercised
33   * part of the API.
34   * 
35   * <p>
36   * This class is expected to contain correct results, and serve to verify the
37   * correctness of a more sophisticated implementation.
38   * 
39   * @author ceki
40   */
41  public class ControlLoggerContext {
42  
43      private ControlLogger root;
44      //
45      // Hashtable loggerMap = new Hashtable();
46      Map<String, ControlLogger> loggerMap = new HashMap<String, ControlLogger>();
47  
48      public ControlLoggerContext() {
49          this.root = new ControlLogger("root", null);
50          this.root.setLevel(Level.DEBUG);
51      }
52  
53      /**
54       * Return this contexts root logger
55       * 
56       * @return
57       */
58      public ControlLogger getRootLogger() {
59          return root;
60      }
61  
62      public ControlLogger exists(String name) {
63          if (name == null) {
64              throw new IllegalArgumentException("name parameter cannot be null");
65          }
66  
67          synchronized (loggerMap) {
68              return (ControlLogger) loggerMap.get(name);
69          }
70      }
71  
72      public final ControlLogger getLogger(String name) {
73          if (name == null) {
74              throw new IllegalArgumentException("name parameter cannot be null");
75          }
76  
77          synchronized (loggerMap) {
78              ControlLogger cl = (ControlLogger) loggerMap.get(name);
79              if (cl != null) {
80                  return cl;
81              }
82              ControlLogger parent = this.root;
83  
84              int i = 0;
85              while (true) {
86                  i = name.indexOf(CoreConstants.DOT, i);
87                  if (i == -1) {
88                      // System.out.println("FINAL-Creating logger named [" + name + "] with
89                      // parent " + parent.getName());
90                      cl = new ControlLogger(name, parent);
91                      loggerMap.put(name, cl);
92                      return cl;
93                  } else {
94                      String parentName = name.substring(0, i);
95                      ControlLogger p = (ControlLogger) loggerMap.get(parentName);
96                      if (p == null) {
97                          // System.out.println("INTERMEDIARY-Creating logger [" + parentName
98                          // + "] with parent " + parent.getName());
99                          p = new ControlLogger(parentName, parent);
100                         loggerMap.put(parentName, p);
101                     }
102                     parent = p;
103                 }
104                 // make i move past the last found dot.
105                 i++;
106             }
107         }
108     }
109 
110     public Map<String, ControlLogger> getLoggerMap() {
111         return loggerMap;
112     }
113 }