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.util;
15  
16  import ch.qos.logback.core.CoreConstants;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  /**
22   * Utility class for analysing logger names.
23   */
24  public class LoggerNameUtil {
25  
26      public static int getFirstSeparatorIndexOf(String name) {
27          return getSeparatorIndexOf(name, 0);
28      }
29  
30      /**
31       * Get the position of the separator character, if any, starting at position
32       * 'fromIndex'.
33       *
34       * @param name
35       * @param fromIndex
36       * @return
37       */
38      public static int getSeparatorIndexOf(String name, int fromIndex) {
39          int dotIndex = name.indexOf(CoreConstants.DOT, fromIndex);
40          int dollarIndex = name.indexOf(CoreConstants.DOLLAR, fromIndex);
41  
42          if (dotIndex == -1 && dollarIndex == -1)
43              return -1;
44          if (dotIndex == -1)
45              return dollarIndex;
46          if (dollarIndex == -1)
47              return dotIndex;
48  
49          return dotIndex < dollarIndex ? dotIndex : dollarIndex;
50      }
51  
52      public static List<String> computeNameParts(String loggerName) {
53          List<String> partList = new ArrayList<String>();
54  
55          int fromIndex = 0;
56          while (true) {
57              int index = getSeparatorIndexOf(loggerName, fromIndex);
58              if (index == -1) {
59                  partList.add(loggerName.substring(fromIndex));
60                  break;
61              }
62              partList.add(loggerName.substring(fromIndex, index));
63              fromIndex = index + 1;
64          }
65          return partList;
66      }
67  }