|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Oracle and/or its affiliates. |
| 3 | + * Licensed under the Universal Permissive License v 1.0 as shown at |
| 4 | + * http://oss.oracle.com/licenses/upl. |
| 5 | + */ |
| 6 | + |
| 7 | +package com.oracle.coherence.k8s; |
| 8 | + |
| 9 | +import java.io.PrintStream; |
| 10 | +import java.util.logging.Logger; |
| 11 | + |
| 12 | +import com.tangosol.net.CacheFactory; |
| 13 | + |
| 14 | +/** |
| 15 | + * A logger for the Coherence Operator that sends messages to a |
| 16 | + * specific logger implementation. |
| 17 | + */ |
| 18 | +public interface OperatorLogger { |
| 19 | + |
| 20 | + /** |
| 21 | + * The system property to use to set the health logging should use Java logger. |
| 22 | + */ |
| 23 | + String PROP_LOGGER = "coherence.k8s.operator.health.logger"; |
| 24 | + |
| 25 | + /** |
| 26 | + * The {@link #PROP_LOGGER} value to log to std-err. |
| 27 | + */ |
| 28 | + String LOGGER_STD_ERR = "err"; |
| 29 | + |
| 30 | + /** |
| 31 | + * The {@link #PROP_LOGGER} value to log to std-out. |
| 32 | + */ |
| 33 | + String LOGGER_STD_OUT = "out"; |
| 34 | + |
| 35 | + /** |
| 36 | + * The {@link #PROP_LOGGER} value to log to a Java util logger. |
| 37 | + */ |
| 38 | + String LOGGER_JAVA = "jdk"; |
| 39 | + |
| 40 | + /** |
| 41 | + * The {@link #PROP_LOGGER} value to log to the Coherence logger. |
| 42 | + */ |
| 43 | + String LOGGER_COHERENCE = "coh"; |
| 44 | + |
| 45 | + /** |
| 46 | + * Log a debug message. |
| 47 | + * |
| 48 | + * @param msg the log message |
| 49 | + * @param args any arguments to apply to the log message using {@link String#format(String, Object...)} |
| 50 | + */ |
| 51 | + void debug(String msg, Object... args); |
| 52 | + |
| 53 | + /** |
| 54 | + * Log an info message. |
| 55 | + * |
| 56 | + * @param msg the log message |
| 57 | + * @param args any arguments to apply to the log message using {@link String#format(String, Object...)} |
| 58 | + */ |
| 59 | + void info(String msg, Object... args); |
| 60 | + |
| 61 | + /** |
| 62 | + * Log a warning message. |
| 63 | + * |
| 64 | + * @param msg the log message |
| 65 | + * @param args any arguments to apply to the log message using {@link String#format(String, Object...)} |
| 66 | + */ |
| 67 | + void warn(String msg, Object... args); |
| 68 | + |
| 69 | + /** |
| 70 | + * Log an error message. |
| 71 | + * |
| 72 | + * @param msg the log message |
| 73 | + * @param args any arguments to apply to the log message using {@link String#format(String, Object...)} |
| 74 | + */ |
| 75 | + void error(String msg, Object... args); |
| 76 | + |
| 77 | + /** |
| 78 | + * Returns the {@link OperatorLogger} to use. |
| 79 | + * |
| 80 | + * @return the {@link OperatorLogger} to use |
| 81 | + */ |
| 82 | + static OperatorLogger getLogger() { |
| 83 | + switch (System.getProperty(PROP_LOGGER, LOGGER_COHERENCE)) { |
| 84 | + case LOGGER_JAVA: |
| 85 | + return new JavaLogger(); |
| 86 | + case LOGGER_STD_ERR: |
| 87 | + return new PrintLogger(System.err); |
| 88 | + case LOGGER_STD_OUT: |
| 89 | + return new PrintLogger(System.out); |
| 90 | + case LOGGER_COHERENCE: |
| 91 | + default: |
| 92 | + try { |
| 93 | + return new CoherenceLogger(); |
| 94 | + } |
| 95 | + catch (Throwable ignored) { |
| 96 | + return new CacheFactoryLogger(); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * An {@link OperatorLogger} that logs to the Coherence logger. |
| 103 | + */ |
| 104 | + class CoherenceLogger implements OperatorLogger { |
| 105 | + |
| 106 | + public CoherenceLogger() { |
| 107 | + // will throw if Logger is not available (i.e. earlier Coherence version) |
| 108 | + com.oracle.coherence.common.base.Logger.isEnabled(com.oracle.coherence.common.base.Logger.INFO); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void debug(String msg, Object... args) { |
| 113 | + // The log method is deprecated but we need to work with earlier Coherence versions so we use it. |
| 114 | + com.oracle.coherence.common.base.Logger.fine(String.format(msg, args)); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void info(String msg, Object... args) { |
| 119 | + // The log method is deprecated but we need to work with earlier Coherence versions so we use it. |
| 120 | + com.oracle.coherence.common.base.Logger.info(String.format(msg, args)); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void warn(String msg, Object... args) { |
| 125 | + // The log method is deprecated but we need to work with earlier Coherence versions so we use it. |
| 126 | + com.oracle.coherence.common.base.Logger.warn(String.format(msg, args)); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void error(String msg, Object... args) { |
| 131 | + // The log method is deprecated but we need to work with earlier Coherence versions so we use it. |
| 132 | + com.oracle.coherence.common.base.Logger.warn(String.format(msg, args)); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * An {@link OperatorLogger} that logs to the Coherence logger. |
| 138 | + */ |
| 139 | + class CacheFactoryLogger implements OperatorLogger { |
| 140 | + @Override |
| 141 | + public void debug(String msg, Object... args) { |
| 142 | + // The log method is deprecated but we need to work with earlier Coherence versions so we use it. |
| 143 | + //noinspection deprecation |
| 144 | + CacheFactory.log(String.format(msg, args), CacheFactory.LOG_DEBUG); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public void info(String msg, Object... args) { |
| 149 | + // The log method is deprecated but we need to work with earlier Coherence versions so we use it. |
| 150 | + //noinspection deprecation |
| 151 | + CacheFactory.log(String.format(msg, args), CacheFactory.LOG_INFO); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public void warn(String msg, Object... args) { |
| 156 | + // The log method is deprecated but we need to work with earlier Coherence versions so we use it. |
| 157 | + //noinspection deprecation |
| 158 | + CacheFactory.log(String.format(msg, args), CacheFactory.LOG_WARN); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public void error(String msg, Object... args) { |
| 163 | + // The log method is deprecated but we need to work with earlier Coherence versions so we use it. |
| 164 | + //noinspection deprecation |
| 165 | + CacheFactory.log(String.format(msg, args), CacheFactory.LOG_WARN); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * An {@link OperatorLogger} that logs to the Java util logger. |
| 171 | + */ |
| 172 | + class JavaLogger implements OperatorLogger { |
| 173 | + private static final Logger LOGGER = Logger.getLogger(OperatorLogger.class.getName()); |
| 174 | + |
| 175 | + @Override |
| 176 | + public void debug(String msg, Object... args) { |
| 177 | + LOGGER.fine(String.format(msg, args)); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public void info(String msg, Object... args) { |
| 182 | + LOGGER.info(String.format(msg, args)); |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public void warn(String msg, Object... args) { |
| 187 | + LOGGER.warning(String.format(msg, args)); |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public void error(String msg, Object... args) { |
| 192 | + LOGGER.severe(String.format(msg, args)); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * An {@link OperatorLogger} that logs to a {@link PrintStream}. |
| 198 | + */ |
| 199 | + class PrintLogger implements OperatorLogger { |
| 200 | + |
| 201 | + private final PrintStream out; |
| 202 | + |
| 203 | + /** |
| 204 | + * Create a {@link PrintLogger}. |
| 205 | + * |
| 206 | + * @param out the {@link PrintStream} to log to |
| 207 | + */ |
| 208 | + PrintLogger(PrintStream out) { |
| 209 | + this.out = out; |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + public void debug(String msg, Object... args) { |
| 214 | + out.printf("[DEBUG] " + msg, args); |
| 215 | + out.println(); |
| 216 | + out.flush(); |
| 217 | + } |
| 218 | + |
| 219 | + @Override |
| 220 | + public void info(String msg, Object... args) { |
| 221 | + out.printf("[INFO] " + msg, args); |
| 222 | + out.println(); |
| 223 | + out.flush(); |
| 224 | + } |
| 225 | + |
| 226 | + @Override |
| 227 | + public void warn(String msg, Object... args) { |
| 228 | + out.printf("[WARNING] " + msg, args); |
| 229 | + out.println(); |
| 230 | + out.flush(); |
| 231 | + } |
| 232 | + |
| 233 | + @Override |
| 234 | + public void error(String msg, Object... args) { |
| 235 | + out.printf("[ERROR] " + msg, args); |
| 236 | + out.println(); |
| 237 | + out.flush(); |
| 238 | + } |
| 239 | + } |
| 240 | +} |
0 commit comments