|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package software.amazon.cloudwatchlogs.emf.annotations; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | +import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger; |
| 21 | + |
| 22 | +/** */ |
| 23 | +public class MetricAnnotationMediator { |
| 24 | + public static MetricAnnotationMediator getInstance() { |
| 25 | + return SINGLETON; |
| 26 | + } |
| 27 | + |
| 28 | + private static final MetricAnnotationMediator SINGLETON = new MetricAnnotationMediator(); |
| 29 | + |
| 30 | + private static final String defaultLoggerKey = "_defaultLogger"; |
| 31 | + |
| 32 | + // protected instead of private for testing purposes |
| 33 | + static HashMap<String, MetricsLogger> loggers; |
| 34 | + |
| 35 | + private MetricAnnotationMediator() { |
| 36 | + loggers = new HashMap<>(); |
| 37 | + loggers.put(defaultLoggerKey, new MetricsLogger()); |
| 38 | + } |
| 39 | + |
| 40 | + /** @return the default logger this singleton uses */ |
| 41 | + public static MetricsLogger getDefaultLogger() { |
| 42 | + return loggers.get(defaultLoggerKey); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @param name the name of the logger to get |
| 47 | + * @return the logger with the specified name if it exists, otherwise will return the default |
| 48 | + * logger |
| 49 | + * @see MetricAnnotationMediator#getDefaultLogger() getDefaultLogger() |
| 50 | + */ |
| 51 | + public static MetricsLogger getLogger(String name) { |
| 52 | + if (name.isEmpty()) { |
| 53 | + return getDefaultLogger(); |
| 54 | + } |
| 55 | + return loggers.getOrDefault(name, getDefaultLogger()); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Adds a logger to this singleton if no other logger has the same name. |
| 60 | + * |
| 61 | + * @param name the desired name of the logger |
| 62 | + * @param logger the logger to be added |
| 63 | + * @return true if the logger was successfully added and false if annother logger already has |
| 64 | + * the same name |
| 65 | + */ |
| 66 | + public static boolean addLogger(String name, MetricsLogger logger) { |
| 67 | + if (loggers.containsKey(name)) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + loggers.put(name, logger); |
| 72 | + return true; |
| 73 | + } |
| 74 | + |
| 75 | + /** Flushes all loggers added to this singleton */ |
| 76 | + public static void flushAll() { |
| 77 | + for (MetricsLogger logger : loggers.values()) { |
| 78 | + logger.flush(); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments