1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.util.logger; 27 28 import java.util.ResourceBundle; 29 import java.util.function.Supplier; 30 import java.lang.System.Logger; 31 import java.lang.System.Logger.Level; 32 import sun.util.logging.ConfigurableLoggerBridge; 33 import sun.util.logging.PlatformLogger; 34 import sun.util.logging.PlatformLoggerBridge; 35 36 /** 37 * An implementation of {@link System.Logger System.Logger} 38 * that redirects all calls to a wrapped instance of {@link 39 * System.Logger System.Logger} 40 * 41 * @param <L> Type of the wrapped Logger: {@code Logger} or 42 * an extension of that interface. 43 * @since 1.9 44 * 45 */ 46 abstract class AbstractLoggerWrapper<L extends Logger> 47 implements Logger, PlatformLoggerBridge, ConfigurableLoggerBridge { 48 49 AbstractLoggerWrapper() { } 50 51 abstract L wrapped(); 52 53 abstract PlatformLoggerBridge platformProxy(); 54 55 L getWrapped() { 56 return wrapped(); 57 } 58 59 @Override 60 public final String getName() { 61 return wrapped().getName(); 62 } 63 64 // ----------------------------------------------------------------- 65 // Generic methods taking a Level as parameter 66 // ----------------------------------------------------------------- 67 68 69 @Override 70 public boolean isLoggable(Level level) { 71 return wrapped().isLoggable(level); 72 } 73 74 @Override 75 public void log(Level level, String msg) { 76 wrapped().log(level, msg); 77 } 78 79 @Override 80 public void log(Level level, 81 Supplier<String> msgSupplier) { 82 wrapped().log(level, msgSupplier); 83 } 84 85 @Override 86 public void log(Level level, Object obj) { 87 wrapped().log(level, obj); 88 } 89 90 @Override 91 public void log(Level level, 92 String msg, Throwable thrown) { 93 wrapped().log(level, msg, thrown); 94 } 95 96 @Override 97 public void log(Level level, Supplier<String> msgSupplier, Throwable thrown) { 98 wrapped().log(level, msgSupplier, thrown); 99 } 100 101 @Override 102 public void log(Level level, 103 String format, Object... params) { 104 wrapped().log(level, format, params); 105 } 106 107 @Override 108 public void log(Level level, ResourceBundle bundle, 109 String key, Throwable thrown) { 110 wrapped().log(level, bundle, key, thrown); 111 } 112 113 @Override 114 public void log(Level level, ResourceBundle bundle, 115 String format, Object... params) { 116 wrapped().log(level, bundle, format, params); 117 } 118 119 // --------------------------------------------------------- 120 // Methods from PlatformLoggerBridge 121 // --------------------------------------------------------- 122 123 @Override 124 public boolean isLoggable(PlatformLogger.Level level) { 125 final PlatformLoggerBridge platformProxy = platformProxy(); 126 if (platformProxy == null) return isLoggable(level.systemLevel()); 127 else return platformProxy.isLoggable(level); 128 } 129 130 @Override 131 public boolean isEnabled() { 132 final PlatformLoggerBridge platformProxy = platformProxy(); 133 return platformProxy == null || platformProxy.isEnabled(); 134 } 135 136 @Override 137 public void log(PlatformLogger.Level level, String msg) { 138 final PlatformLoggerBridge platformProxy = platformProxy(); 139 if (platformProxy == null) { 140 wrapped().log(level.systemLevel(), msg); 141 } else { 142 platformProxy.log(level, msg); 143 } 144 } 145 146 @Override 147 public void log(PlatformLogger.Level level, String msg, Throwable thrown) { 148 final PlatformLoggerBridge platformProxy = platformProxy(); 149 if (platformProxy == null) { 150 wrapped().log(level.systemLevel(), msg, thrown); 151 } else { 152 platformProxy.log(level, msg, thrown); 153 } 154 } 155 156 @Override 157 public void log(PlatformLogger.Level level, String msg, Object... params) { 158 final PlatformLoggerBridge platformProxy = platformProxy(); 159 if (platformProxy == null) { 160 wrapped().log(level.systemLevel(), msg, params); 161 } else { 162 platformProxy.log(level, msg, params); 163 } 164 } 165 166 @Override 167 public void log(PlatformLogger.Level level, Supplier<String> msgSupplier) { 168 final PlatformLoggerBridge platformProxy = platformProxy(); 169 if (platformProxy == null) { 170 wrapped().log(level.systemLevel(),msgSupplier); 171 } else { 172 platformProxy.log(level,msgSupplier); 173 } 174 } 175 176 @Override 177 public void log(PlatformLogger.Level level, Throwable thrown, Supplier<String> msgSupplier) { 178 final PlatformLoggerBridge platformProxy = platformProxy(); 179 if (platformProxy == null) { 180 wrapped().log(level.systemLevel(), 181 msgSupplier, thrown); 182 } else { 183 platformProxy.log(level, thrown, msgSupplier); 184 } 185 } 186 187 @Override 188 public void logp(PlatformLogger.Level level, String sourceClass, String sourceMethod, String msg) { 189 final PlatformLoggerBridge platformProxy = platformProxy(); 190 if (platformProxy == null) { 191 if (sourceClass==null && sourceMethod==null) { // best effort 192 wrapped().log(level.systemLevel(), msg); 193 } else { 194 Level systemLevel = level.systemLevel(); 195 Logger wrapped = wrapped(); 196 if (wrapped.isLoggable(systemLevel)) { 197 sourceClass = sourceClass == null ? "" : sourceClass; 198 sourceMethod = sourceMethod == null ? "" : sourceMethod; 199 msg = msg == null ? "" : msg; 200 wrapped.log(systemLevel, String.format("[%s %s] %s", 201 sourceClass, sourceMethod, msg)); 202 } 203 } 204 } else { 205 platformProxy.logp(level, sourceClass, sourceMethod, msg); 206 } 207 } 208 209 @Override 210 public void logp(PlatformLogger.Level level, String sourceClass, String sourceMethod, 211 Supplier<String> msgSupplier) { 212 final PlatformLoggerBridge platformProxy = platformProxy(); 213 if (platformProxy == null) { // best effort 214 if (sourceClass==null && sourceMethod==null) { 215 wrapped().log(level.systemLevel(), msgSupplier); 216 } else { 217 Level systemLevel = level.systemLevel(); 218 Logger wrapped = wrapped(); 219 if (wrapped.isLoggable(systemLevel)) { 220 final String sClass = sourceClass == null ? "" : sourceClass; 221 final String sMethod = sourceMethod == null ? "" : sourceMethod; 222 wrapped.log(systemLevel, () -> String.format("[%s %s] %s", 223 sClass, sMethod, msgSupplier.get())); 224 } 225 } 226 } else { 227 platformProxy.logp(level, sourceClass, sourceMethod, msgSupplier); 228 } 229 } 230 231 @Override 232 public void logp(PlatformLogger.Level level, String sourceClass, String sourceMethod, 233 String msg, Object... params) { 234 final PlatformLoggerBridge platformProxy = platformProxy(); 235 if (platformProxy == null) { // best effort 236 if (sourceClass==null && sourceMethod==null) { 237 wrapped().log(level.systemLevel(), msg, params); 238 } else { 239 Level systemLevel = level.systemLevel(); 240 Logger wrapped = wrapped(); 241 if (wrapped.isLoggable(systemLevel)) { 242 sourceClass = sourceClass == null ? "" : sourceClass; 243 sourceMethod = sourceMethod == null ? "" : sourceMethod; 244 msg = msg == null ? "" : msg; 245 wrapped.log(systemLevel, String.format("[%s %s] %s", 246 sourceClass, sourceMethod, msg), params); 247 } 248 } 249 } else { 250 platformProxy.logp(level, sourceClass, sourceMethod, msg, params); 251 } 252 } 253 254 @Override 255 public void logp(PlatformLogger.Level level, String sourceClass, String sourceMethod, 256 String msg, Throwable thrown) { 257 final PlatformLoggerBridge platformProxy = platformProxy(); 258 if (platformProxy == null) { // best effort 259 if (sourceClass==null && sourceMethod==null) { 260 wrapped().log(level.systemLevel(), msg, thrown); 261 } else { 262 Level systemLevel = level.systemLevel(); 263 Logger wrapped = wrapped(); 264 if (wrapped.isLoggable(systemLevel)) { 265 sourceClass = sourceClass == null ? "" : sourceClass; 266 sourceMethod = sourceMethod == null ? "" : sourceMethod; 267 msg = msg == null ? "" : msg; 268 wrapped.log(systemLevel, String.format("[%s %s] %s", 269 sourceClass, sourceMethod, msg), thrown); 270 } 271 } 272 } else { 273 platformProxy.logp(level, sourceClass, sourceMethod, msg, thrown); 274 } 275 } 276 277 @Override 278 public void logp(PlatformLogger.Level level, String sourceClass, String sourceMethod, 279 Throwable thrown, Supplier<String> msgSupplier) { 280 final PlatformLoggerBridge platformProxy = platformProxy(); 281 if (platformProxy == null) { // best effort 282 if (sourceClass==null && sourceMethod==null) { 283 wrapped().log(level.systemLevel(), msgSupplier, thrown); 284 } else { 285 Level systemLevel = level.systemLevel(); 286 Logger wrapped = wrapped(); 287 if (wrapped.isLoggable(systemLevel)) { 288 final String sClass = sourceClass == null ? "" : sourceClass; 289 final String sMethod = sourceMethod == null ? "" : sourceMethod; 290 wrapped.log(systemLevel, () -> String.format("[%s %s] %s", 291 sClass, sMethod, msgSupplier.get()), thrown); 292 } 293 } 294 } else { 295 platformProxy.logp(level, sourceClass, sourceMethod, thrown, msgSupplier); 296 } 297 } 298 299 @Override 300 public void logrb(PlatformLogger.Level level, String sourceClass, String sourceMethod, 301 ResourceBundle bundle, String msg, Object... params) { 302 final PlatformLoggerBridge platformProxy = platformProxy(); 303 if (platformProxy == null) { // best effort 304 if (bundle != null || sourceClass == null && sourceMethod == null) { 305 wrapped().log(level.systemLevel(), 306 bundle, msg, params); 307 } else { 308 Level systemLevel = level.systemLevel(); 309 Logger wrapped = wrapped(); 310 if (wrapped.isLoggable(systemLevel)) { 311 sourceClass = sourceClass == null ? "" : sourceClass; 312 sourceMethod = sourceMethod == null ? "" : sourceMethod; 313 msg = msg == null ? "" : msg; 314 wrapped.log(systemLevel, String.format("[%s %s] %s", 315 sourceClass, sourceMethod, msg), params); 316 } 317 } 318 } else { 319 platformProxy.logrb(level, sourceClass, sourceMethod, 320 bundle, msg, params); 321 } 322 } 323 324 @Override 325 public void logrb(PlatformLogger.Level level, String sourceClass, String sourceMethod, 326 ResourceBundle bundle, String msg, Throwable thrown) { 327 final PlatformLoggerBridge platformProxy = platformProxy(); 328 if (platformProxy == null) { // best effort 329 if (bundle != null || sourceClass == null && sourceMethod == null) { 330 wrapped().log(level.systemLevel(), 331 bundle, msg, thrown); 332 } else { 333 Level systemLevel = level.systemLevel(); 334 Logger wrapped = wrapped(); 335 if (wrapped.isLoggable(systemLevel)) { 336 sourceClass = sourceClass == null ? "" : sourceClass; 337 sourceMethod = sourceMethod == null ? "" : sourceMethod; 338 msg = msg == null ? "" : msg; 339 wrapped.log(systemLevel, String.format("[%s %s] %s", 340 sourceClass, sourceMethod, msg), thrown); 341 } 342 } 343 } else { 344 platformProxy.logrb(level, sourceClass, sourceMethod, bundle, msg, thrown); 345 } 346 } 347 348 @Override 349 public void logrb(PlatformLogger.Level level, ResourceBundle bundle, String msg, 350 Throwable thrown) { 351 final PlatformLoggerBridge platformProxy = platformProxy(); 352 if (platformProxy == null) { 353 wrapped().log(level.systemLevel(), 354 bundle, msg, thrown); 355 } else { 356 platformProxy.logrb(level, bundle, msg, thrown); 357 } 358 } 359 360 @Override 361 public void logrb(PlatformLogger.Level level, ResourceBundle bundle, String msg, 362 Object... params) { 363 final PlatformLoggerBridge platformProxy = platformProxy(); 364 if (platformProxy == null) { 365 wrapped().log(level.systemLevel(), 366 bundle, msg, params); 367 } else { 368 platformProxy.logrb(level, bundle, msg, params); 369 } 370 } 371 372 373 @Override 374 public LoggerConfiguration getLoggerConfiguration() { 375 final PlatformLoggerBridge platformProxy = platformProxy(); 376 return platformProxy == null ? null 377 : ConfigurableLoggerBridge.getLoggerConfiguration(platformProxy); 378 } 379 380 }