|
| 1 | +// Copyright (c) 2021 VMware, Inc. or its affiliates. All rights reserved. |
| 2 | +// |
| 3 | +// This software, the RabbitMQ Stream Java client library, is dual-licensed under the |
| 4 | +// Mozilla Public License 2.0 ("MPL"), and the Apache License version 2 ("ASL"). |
| 5 | +// For the MPL, please see LICENSE-MPL-RabbitMQ. For the ASL, |
| 6 | +// please see LICENSE-APACHE2. |
| 7 | +// |
| 8 | +// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, |
| 9 | +// either express or implied. See the LICENSE file for specific language governing |
| 10 | +// rights and limitations of this software. |
| 11 | +// |
| 12 | +// If you have any questions regarding licensing, please contact us at |
| 13 | + |
| 14 | +package com.rabbitmq.stream.perf; |
| 15 | + |
| 16 | +import ch.qos.logback.classic.LoggerContext; |
| 17 | +import ch.qos.logback.classic.joran.JoranConfigurator; |
| 18 | +import ch.qos.logback.core.joran.spi.JoranException; |
| 19 | +import ch.qos.logback.core.util.StatusPrinter; |
| 20 | +import java.io.BufferedReader; |
| 21 | +import java.io.ByteArrayInputStream; |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.InputStream; |
| 24 | +import java.io.InputStreamReader; |
| 25 | +import java.nio.charset.StandardCharsets; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.Map; |
| 28 | +import org.slf4j.LoggerFactory; |
| 29 | + |
| 30 | +public class LogUtils { |
| 31 | + |
| 32 | + static void configureLog() throws IOException { |
| 33 | + String loggers = |
| 34 | + System.getProperty("rabbitmq.streamperftest.loggers") == null |
| 35 | + ? System.getenv("RABBITMQ_STREAM_PERF_TEST_LOGGERS") |
| 36 | + : System.getProperty("rabbitmq.streamperftest.loggers"); |
| 37 | + LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); |
| 38 | + InputStream configurationFile = StreamPerfTest.class.getResourceAsStream("/logback.xml"); |
| 39 | + try { |
| 40 | + String configuration = |
| 41 | + processConfigurationFile(configurationFile, convertKeyValuePairs(loggers)); |
| 42 | + JoranConfigurator configurator = new JoranConfigurator(); |
| 43 | + configurator.setContext(context); |
| 44 | + context.reset(); |
| 45 | + configurator.doConfigure( |
| 46 | + new ByteArrayInputStream(configuration.getBytes(StandardCharsets.UTF_8))); |
| 47 | + } catch (JoranException je) { |
| 48 | + // StatusPrinter will handle this |
| 49 | + } finally { |
| 50 | + configurationFile.close(); |
| 51 | + } |
| 52 | + StatusPrinter.printInCaseOfErrorsOrWarnings(context); |
| 53 | + } |
| 54 | + |
| 55 | + private static Map<String, Object> convertKeyValuePairs(String arg) { |
| 56 | + if (arg == null || arg.trim().isEmpty()) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + Map<String, Object> properties = new HashMap<>(); |
| 60 | + for (String entry : arg.split(",")) { |
| 61 | + String[] keyValue = entry.split("="); |
| 62 | + try { |
| 63 | + properties.put(keyValue[0], Long.parseLong(keyValue[1])); |
| 64 | + } catch (NumberFormatException e) { |
| 65 | + properties.put(keyValue[0], keyValue[1]); |
| 66 | + } |
| 67 | + } |
| 68 | + return properties; |
| 69 | + } |
| 70 | + |
| 71 | + static String processConfigurationFile(InputStream configurationFile, Map<String, Object> loggers) |
| 72 | + throws IOException { |
| 73 | + StringBuilder loggersConfiguration = new StringBuilder(); |
| 74 | + if (loggers != null) { |
| 75 | + for (Map.Entry<String, Object> logger : loggers.entrySet()) { |
| 76 | + loggersConfiguration.append( |
| 77 | + String.format( |
| 78 | + "\t<logger name=\"%s\" level=\"%s\" />%s", |
| 79 | + logger.getKey(), |
| 80 | + logger.getValue().toString(), |
| 81 | + System.getProperty("line.separator"))); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + BufferedReader in = new BufferedReader(new InputStreamReader(configurationFile)); |
| 86 | + final int bufferSize = 1024; |
| 87 | + final char[] buffer = new char[bufferSize]; |
| 88 | + StringBuilder builder = new StringBuilder(); |
| 89 | + int charsRead; |
| 90 | + while ((charsRead = in.read(buffer, 0, buffer.length)) > 0) { |
| 91 | + builder.append(buffer, 0, charsRead); |
| 92 | + } |
| 93 | + |
| 94 | + return builder.toString().replace("${loggers}", loggersConfiguration); |
| 95 | + } |
| 96 | +} |
0 commit comments