59 |
60 |
60 public static void main(String[] args) throws IOException { |
61 public static void main(String[] args) throws IOException { |
61 Path initialProps = SRC_DIR.resolve(CONFIG_FILE); |
62 Path initialProps = SRC_DIR.resolve(CONFIG_FILE); |
62 Path loggingProps = USER_DIR.resolve(CONFIG_FILE); |
63 Path loggingProps = USER_DIR.resolve(CONFIG_FILE); |
63 System.setProperty("java.util.logging.config.file", loggingProps.toString()); |
64 System.setProperty("java.util.logging.config.file", loggingProps.toString()); |
64 Files.copy(initialProps, loggingProps); |
65 Files.copy(initialProps, loggingProps, StandardCopyOption.REPLACE_EXISTING); |
65 System.out.println("Root level is: " + Logger.getLogger("").getLevel()); |
66 System.out.println("Root level is: " + Logger.getLogger("").getLevel()); |
66 if (Logger.getLogger("").getLevel() != Level.INFO) { |
67 if (Logger.getLogger("").getLevel() != Level.INFO) { |
67 throw new RuntimeException("Expected root level INFO, got: " |
68 throw new RuntimeException("Expected root level INFO, got: " |
68 + Logger.getLogger("").getLevel()); |
69 + Logger.getLogger("").getLevel()); |
69 } |
70 } |
71 // handlers=custom.Handler, the other with |
72 // handlers=custom.Handler, the other with |
72 // .handlers=custom.DotHandler |
73 // .handlers=custom.DotHandler |
73 // Verify that exactly one of the two handlers is a custom.Handler |
74 // Verify that exactly one of the two handlers is a custom.Handler |
74 // Verify that exactly one of the two handlers is a custom.DotHandler |
75 // Verify that exactly one of the two handlers is a custom.DotHandler |
75 // Verify that the two handlers has an id of '1' |
76 // Verify that the two handlers has an id of '1' |
76 checkHandlers(Logger.getLogger("").getHandlers(), |
77 checkHandlers(Logger.getLogger(""), |
|
78 Logger.getLogger("").getHandlers(), |
77 1L, |
79 1L, |
78 custom.Handler.class, |
80 custom.Handler.class, |
79 custom.DotHandler.class); |
81 custom.DotHandler.class); |
|
82 checkHandlers(Logger.getLogger("global"), |
|
83 Logger.getGlobal().getHandlers(), |
|
84 1L, |
|
85 custom.GlobalHandler.class); |
80 |
86 |
81 // The log message "hi" should appear twice on the console. |
87 // The log message "hi" should appear twice on the console. |
82 // We don't check that. This is just for log analysis in case |
88 // We don't check that. This is just for log analysis in case |
83 // of test failure. |
89 // of test failure. |
84 Logger.getAnonymousLogger().info("hi"); |
90 Logger.getAnonymousLogger().info("hi"); |
100 // Verify that we have now only one handler, configured with |
106 // Verify that we have now only one handler, configured with |
101 // handlers=custom.Handler, and that the other configured with |
107 // handlers=custom.Handler, and that the other configured with |
102 // .handlers=custom.DotHandler was ignored. |
108 // .handlers=custom.DotHandler was ignored. |
103 // Verify that the handler is a custom.Handler |
109 // Verify that the handler is a custom.Handler |
104 // Verify that the handler has an id of '2' |
110 // Verify that the handler has an id of '2' |
105 checkHandlers(Logger.getLogger("").getHandlers(), |
111 checkHandlers(Logger.getLogger(""), |
|
112 Logger.getLogger("").getHandlers(), |
106 2L, |
113 2L, |
107 custom.Handler.class); |
114 custom.Handler.class); |
|
115 checkHandlers(Logger.getGlobal(), |
|
116 Logger.getGlobal().getHandlers(), |
|
117 1L); |
108 |
118 |
109 // The log message "there" should appear only once on the console. |
119 // The log message "there" should appear only once on the console. |
110 // We don't check that. This is just for log analysis in case |
120 // We don't check that. This is just for log analysis in case |
111 // of test failure. |
121 // of test failure. |
112 Logger.getAnonymousLogger().info("there!"); |
122 Logger.getAnonymousLogger().info("there!"); |
128 // Verify that we have only one handler, configured with |
138 // Verify that we have only one handler, configured with |
129 // handlers=custom.Handler, and that the other configured with |
139 // handlers=custom.Handler, and that the other configured with |
130 // .handlers=custom.DotHandler was ignored. |
140 // .handlers=custom.DotHandler was ignored. |
131 // Verify that the handler is a custom.Handler |
141 // Verify that the handler is a custom.Handler |
132 // Verify that the handler has an id of '3' |
142 // Verify that the handler has an id of '3' |
133 checkHandlers(Logger.getLogger("").getHandlers(), |
143 checkHandlers(Logger.getLogger(""), |
|
144 Logger.getLogger("").getHandlers(), |
134 3L, |
145 3L, |
135 custom.Handler.class); |
146 custom.Handler.class); |
|
147 checkHandlers(Logger.getGlobal(), |
|
148 Logger.getGlobal().getHandlers(), |
|
149 1L); |
|
150 |
|
151 LogManager.getLogManager().reset(); |
|
152 LogManager.getLogManager().updateConfiguration((s) -> (o,n) -> n); |
|
153 // Verify that we have only one handler, configured with |
|
154 // handlers=custom.Handler, and that the other configured with |
|
155 // .handlers=custom.DotHandler was ignored. |
|
156 // Verify that the handler is a custom.Handler |
|
157 // Verify that the handler has an id of '4' |
|
158 checkHandlers(Logger.getLogger(""), |
|
159 Logger.getLogger("").getHandlers(), |
|
160 4L, |
|
161 custom.Handler.class); |
|
162 checkHandlers(Logger.getGlobal(), |
|
163 Logger.getGlobal().getHandlers(), |
|
164 2L, |
|
165 custom.GlobalHandler.class); |
|
166 |
|
167 LogManager.getLogManager().updateConfiguration((s) -> (o,n) -> n); |
|
168 // Verify that we have only one handler, configured with |
|
169 // handlers=custom.Handler, and that the other configured with |
|
170 // .handlers=custom.DotHandler was ignored. |
|
171 // Verify that the handler is a custom.Handler |
|
172 // Verify that the handler has an id of '4' |
|
173 checkHandlers(Logger.getLogger(""), |
|
174 Logger.getLogger("").getHandlers(), |
|
175 4L, |
|
176 custom.Handler.class); |
|
177 checkHandlers(Logger.getGlobal(), |
|
178 Logger.getGlobal().getHandlers(), |
|
179 2L, |
|
180 custom.GlobalHandler.class); |
|
181 |
136 |
182 |
137 // The log message "done" should appear only once on the console. |
183 // The log message "done" should appear only once on the console. |
138 // We don't check that. This is just for log analysis in case |
184 // We don't check that. This is just for log analysis in case |
139 // of test failure. |
185 // of test failure. |
140 Logger.getAnonymousLogger().info("done!"); |
186 Logger.getAnonymousLogger().info("done!"); |
141 } |
187 } |
142 |
188 |
143 static void checkHandlers(Handler[] handlers, Long expectedID, Class<?>... clz) { |
189 static void checkHandlers(Logger logger, Handler[] handlers, Long expectedID, Class<?>... clz) { |
144 // Verify that we have the expected number of handlers. |
190 // Verify that we have the expected number of handlers. |
145 if (Stream.of(handlers).count() != clz.length) { |
191 if (Stream.of(handlers).count() != clz.length) { |
146 throw new RuntimeException("Expected " + clz.length + " handlers, got: " |
192 throw new RuntimeException("Expected " + clz.length + " handlers, got: " |
147 + List.of(Logger.getLogger("").getHandlers())); |
193 + List.of(logger.getHandlers())); |
148 } |
194 } |
149 for (Class<?> cl : clz) { |
195 for (Class<?> cl : clz) { |
150 // Verify that the handlers are of the expected class. |
196 // Verify that the handlers are of the expected class. |
151 // For each class, we should have exactly one handler |
197 // For each class, we should have exactly one handler |
152 // of that class. |
198 // of that class. |
153 if (Stream.of(handlers) |
199 if (Stream.of(handlers) |
154 .map(Object::getClass) |
200 .map(Object::getClass) |
155 .filter(cl::equals) |
201 .filter(cl::equals) |
156 .count() != 1) { |
202 .count() != 1) { |
157 throw new RuntimeException("Expected one " + cl +", got: " |
203 throw new RuntimeException("Expected one " + cl +", got: " |
158 + List.of(Logger.getLogger("").getHandlers())); |
204 + List.of(logger.getHandlers())); |
159 } |
205 } |
160 } |
206 } |
161 // Verify that all handlers have the expected ID |
207 // Verify that all handlers have the expected ID |
162 if (Stream.of(Logger.getLogger("").getHandlers()) |
208 if (Stream.of(logger.getHandlers()) |
163 .map(RootLoggerHandlers::getId) |
209 .map(RootLoggerHandlers::getId) |
164 .filter(expectedID::equals) |
210 .filter(expectedID::equals) |
165 .count() != clz.length) { |
211 .count() != clz.length) { |
166 throw new RuntimeException("Expected ids to be " + expectedID + ", got: " |
212 throw new RuntimeException("Expected ids to be " + expectedID + ", got: " |
167 + List.of(Logger.getLogger("").getHandlers())); |
213 + List.of(logger.getHandlers())); |
168 } |
214 } |
169 } |
215 } |
170 |
216 |
171 static long getId(Handler h) { |
217 static long getId(Handler h) { |
172 if (h instanceof custom.Handler) { |
218 if (h instanceof custom.Handler) { |
173 return ((custom.Handler)h).id; |
219 return ((custom.Handler)h).id; |
174 } |
220 } |
175 if (h instanceof custom.DotHandler) { |
221 if (h instanceof custom.DotHandler) { |
176 return ((custom.DotHandler)h).id; |
222 return ((custom.DotHandler)h).id; |
177 } |
223 } |
|
224 if (h instanceof custom.GlobalHandler) { |
|
225 return ((custom.GlobalHandler)h).id; |
|
226 } |
178 return -1; |
227 return -1; |
179 } |
228 } |
180 } |
229 } |