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;
15  
16  import ch.qos.logback.classic.Level;
17  
18  /**
19   * @author ceki
20   */
21  public class HLoggerContext {
22  
23      private HLogger root;
24      private int size;
25  
26      public HLoggerContext() {
27          this.root = new HLogger("root", null);
28          this.root.setLevel(Level.DEBUG);
29          size = 1;
30      }
31  
32      /**
33       * Return this contexts root logger
34       *
35       * @return
36       */
37      public HLogger getRootLogger() {
38          return root;
39      }
40  
41      public HLogger getLogger(final String name) {
42  
43          int i = 0;
44          HLogger HLogger = root;
45          HLogger childHLogger = null;
46          String childName;
47  
48          while (true) {
49              int h = name.indexOf('.', i);
50              if (h == -1) {
51                  childName = name.substring(i);
52              } else {
53                  childName = name.substring(i, h);
54              }
55              // move i left of the last point
56              i = h + 1;
57  
58              synchronized (HLogger) {
59                  childHLogger = HLogger.getChildBySuffix(childName);
60                  if (childHLogger == null) {
61                      childHLogger = HLogger.createChildByLastNamePart(childName);
62                      incSize();
63                  }
64              }
65              HLogger = childHLogger;
66              if (h == -1) {
67                  return childHLogger;
68              }
69          }
70      }
71  
72      private synchronized void incSize() {
73          size++;
74      }
75  
76      int size() {
77          return size;
78      }
79  
80      /**
81       * Check if the named logger exists in the hierarchy. If so return
82       * its reference, otherwise returns <code>null</code>.
83       *
84       * @param name the name of the logger to search for.
85       */
86      HLogger exists(String name) {
87          int i = 0;
88          HLogger HLogger = root;
89          HLogger childHLogger = null;
90          String childName;
91          while (true) {
92              int h = name.indexOf('.', i);
93              if (h == -1) {
94                  childName = name.substring(i);
95              } else {
96                  childName = name.substring(i, h);
97              }
98              // move i left of the last point
99              i = h + 1;
100 
101             synchronized (HLogger) {
102                 childHLogger = HLogger.getChildBySuffix(childName);
103                 if (childHLogger == null) {
104                     return null;
105                 }
106             }
107             HLogger = childHLogger;
108             if (h == -1) {
109                 if (childHLogger.getName().equals(name)) {
110                     return childHLogger;
111                 } else {
112                     return null;
113                 }
114             }
115         }
116     }
117 }