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