|
1 | | -// Copyright (c) 2018-2023 Broadcom. All Rights Reserved. The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. |
| 1 | +// Copyright (c) 2018-2023 Broadcom. All Rights Reserved. |
| 2 | +// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. |
2 | 3 | // |
3 | 4 | // This software, the RabbitMQ Java client library, is triple-licensed under the |
4 | 5 | // Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2 |
|
12 | 13 | // |
13 | 14 | // If you have any questions regarding licensing, please contact us at |
14 | 15 | |
15 | | - |
16 | 16 | package com.rabbitmq.perf; |
17 | 17 |
|
18 | 18 | import ch.qos.logback.classic.LoggerContext; |
19 | 19 | import ch.qos.logback.classic.joran.JoranConfigurator; |
20 | 20 | import ch.qos.logback.core.joran.spi.JoranException; |
21 | 21 | import ch.qos.logback.core.util.StatusPrinter; |
22 | | -import org.slf4j.LoggerFactory; |
23 | | - |
24 | 22 | import java.io.*; |
25 | 23 | import java.nio.charset.StandardCharsets; |
26 | 24 | import java.util.Map; |
| 25 | +import org.slf4j.LoggerFactory; |
27 | 26 |
|
28 | | -/** |
29 | | - * Configure logback. |
30 | | - */ |
| 27 | +/** Configure logback. */ |
31 | 28 | public class Log { |
32 | 29 |
|
33 | | - public static void configureLog() throws IOException { |
34 | | - if (System.getProperty("logback.configurationFile") == null) { |
35 | | - String loggers = System.getProperty("rabbitmq.perftest.loggers") == null ? |
36 | | - System.getenv("RABBITMQ_PERF_TEST_LOGGERS") : |
37 | | - System.getProperty("rabbitmq.perftest.loggers"); |
38 | | - LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); |
39 | | - InputStream configurationFile = PerfTest.class.getResourceAsStream("/logback-perf-test.xml"); |
40 | | - try { |
41 | | - String configuration = processConfigurationFile(configurationFile, PerfTest.convertKeyValuePairs(loggers)); |
42 | | - JoranConfigurator configurator = new JoranConfigurator(); |
43 | | - configurator.setContext(context); |
44 | | - context.reset(); |
45 | | - configurator.doConfigure(new ByteArrayInputStream( |
46 | | - configuration.getBytes(StandardCharsets.UTF_8) |
47 | | - )); |
48 | | - } catch (JoranException je) { |
49 | | - // StatusPrinter will handle this |
50 | | - } finally { |
51 | | - configurationFile.close(); |
52 | | - } |
53 | | - StatusPrinter.printInCaseOfErrorsOrWarnings(context); |
54 | | - } |
| 30 | + public static void configureLog() throws IOException { |
| 31 | + if (System.getProperty("logback.configurationFile") == null) { |
| 32 | + String loggers = |
| 33 | + System.getProperty("rabbitmq.perftest.loggers") == null |
| 34 | + ? System.getenv("RABBITMQ_PERF_TEST_LOGGERS") |
| 35 | + : System.getProperty("rabbitmq.perftest.loggers"); |
| 36 | + LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); |
| 37 | + InputStream configurationFile = PerfTest.class.getResourceAsStream("/logback-perf-test.xml"); |
| 38 | + try { |
| 39 | + String configuration = |
| 40 | + processConfigurationFile(configurationFile, PerfTest.convertKeyValuePairs(loggers)); |
| 41 | + JoranConfigurator configurator = new JoranConfigurator(); |
| 42 | + configurator.setContext(context); |
| 43 | + context.reset(); |
| 44 | + configurator.doConfigure( |
| 45 | + new ByteArrayInputStream(configuration.getBytes(StandardCharsets.UTF_8))); |
| 46 | + } catch (JoranException je) { |
| 47 | + // StatusPrinter will handle this |
| 48 | + } finally { |
| 49 | + configurationFile.close(); |
| 50 | + } |
| 51 | + StatusPrinter.printInCaseOfErrorsOrWarnings(context); |
55 | 52 | } |
| 53 | + } |
56 | 54 |
|
57 | | - /** |
58 | | - * @param configurationFile |
59 | | - * @param loggers |
60 | | - * @return |
61 | | - * @throws IOException |
62 | | - * @since 2.11.0 |
63 | | - */ |
64 | | - static String processConfigurationFile(InputStream configurationFile, Map<String, Object> loggers) throws IOException { |
65 | | - StringBuilder loggersConfiguration = new StringBuilder(); |
66 | | - if (loggers != null) { |
67 | | - for (Map.Entry<String, Object> logger : loggers.entrySet()) { |
68 | | - loggersConfiguration.append(String.format( |
69 | | - "\t<logger name=\"%s\" level=\"%s\" />%s", |
70 | | - logger.getKey(), logger.getValue().toString(), System.getProperty("line.separator") |
71 | | - )); |
72 | | - } |
73 | | - } |
74 | | - |
75 | | - BufferedReader in = new BufferedReader(new InputStreamReader(configurationFile)); |
76 | | - final int bufferSize = 1024; |
77 | | - final char[] buffer = new char[bufferSize]; |
78 | | - StringBuilder builder = new StringBuilder(); |
79 | | - int charsRead; |
80 | | - while ((charsRead = in.read(buffer, 0, buffer.length)) > 0) { |
81 | | - builder.append(buffer, 0, charsRead); |
82 | | - } |
83 | | - |
84 | | - return builder.toString().replace("${loggers}", loggersConfiguration); |
| 55 | + /** |
| 56 | + * @param configurationFile |
| 57 | + * @param loggers |
| 58 | + * @return |
| 59 | + * @throws IOException |
| 60 | + * @since 2.11.0 |
| 61 | + */ |
| 62 | + static String processConfigurationFile(InputStream configurationFile, Map<String, Object> loggers) |
| 63 | + throws IOException { |
| 64 | + StringBuilder loggersConfiguration = new StringBuilder(); |
| 65 | + if (loggers != null) { |
| 66 | + for (Map.Entry<String, Object> logger : loggers.entrySet()) { |
| 67 | + loggersConfiguration.append( |
| 68 | + String.format( |
| 69 | + "\t<logger name=\"%s\" level=\"%s\" />%s", |
| 70 | + logger.getKey(), |
| 71 | + logger.getValue().toString(), |
| 72 | + System.getProperty("line.separator"))); |
| 73 | + } |
85 | 74 | } |
86 | 75 |
|
| 76 | + BufferedReader in = new BufferedReader(new InputStreamReader(configurationFile)); |
| 77 | + final int bufferSize = 1024; |
| 78 | + final char[] buffer = new char[bufferSize]; |
| 79 | + StringBuilder builder = new StringBuilder(); |
| 80 | + int charsRead; |
| 81 | + while ((charsRead = in.read(buffer, 0, buffer.length)) > 0) { |
| 82 | + builder.append(buffer, 0, charsRead); |
| 83 | + } |
87 | 84 |
|
| 85 | + return builder.toString().replace("${loggers}", loggersConfiguration); |
| 86 | + } |
88 | 87 | } |
0 commit comments