4545 */
4646public class LogSource {
4747
48+ public static final String SEPARATOR = ":" ;
49+
4850 private final LogSource parent ;
4951
5052 private final List <String > path ;
@@ -54,6 +56,8 @@ public class LogSource {
5456
5557 private String formatted = null ;
5658
59+ private Integer logLevel ;
60+
5761 private LogSource (LogSource parent , String name ) {
5862 this .parent = parent ;
5963 List <String > parentPath = parent .path ();
@@ -73,6 +77,19 @@ public static LogSource newRoot() {
7377 return new LogSource ();
7478 }
7579
80+ /**
81+ * Returns a log source with the given path.
82+ *
83+ * @param subPath Relative path to the source, divided by
84+ * {@link LogSource#SEPARATOR}.
85+ */
86+ public LogSource subSource (final String subPath ) {
87+ LogSource result = this ;
88+ for (final String name : subPath .split (SEPARATOR ))
89+ result = result .child (name );
90+ return result ;
91+ }
92+
7693 /** Returns the list of strings which is represented by this LogSource. */
7794 public List <String > path () {
7895 return path ;
@@ -84,22 +101,10 @@ public String name() {
84101 return path .get (path .size () - 1 );
85102 }
86103
87- /**
88- * Returns the LogSource which represents the path of this LogSource extended
89- * by name.
90- */
91- public LogSource subSource (String name ) {
92- LogSource child = children .get (name );
93- if (child != null ) return child ;
94- child = new LogSource (this , name );
95- children .putIfAbsent (name , child );
96- return children .get (name );
97- }
98-
99104 @ Override
100105 public String toString () {
101106 if (formatted != null ) return formatted ;
102- StringJoiner joiner = new StringJoiner (":" );
107+ StringJoiner joiner = new StringJoiner (SEPARATOR );
103108 path .forEach (s -> joiner .add (s ));
104109 formatted = joiner .toString ();
105110 return formatted ;
@@ -113,4 +118,28 @@ public boolean isRoot() {
113118 public LogSource parent () {
114119 return parent ;
115120 }
121+
122+ public void setLogLevel (int logLevel ) {
123+ this .logLevel = logLevel ;
124+ }
125+
126+ public boolean hasLogLevel () {
127+ return logLevel != null ;
128+ }
129+
130+ public int logLevel () {
131+ if (!hasLogLevel ()) throw new IllegalStateException ();
132+ return logLevel ;
133+ }
134+
135+ // -- Helper methods --
136+
137+ private LogSource child (final String name ) {
138+ if (name .isEmpty ()) return this ;
139+ LogSource child = children .get (name );
140+ if (child != null ) return child ;
141+ child = new LogSource (this , name );
142+ children .putIfAbsent (name , child );
143+ return children .get (name );
144+ }
116145}
0 commit comments