Path: blob/master/src/java.base/share/classes/jdk/internal/logger/LocalizedLoggerWrapper.java
41159 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526package jdk.internal.logger;2728import java.util.ResourceBundle;29import java.util.function.Supplier;30import java.lang.System.Logger;31import java.lang.System.Logger.Level;3233/**34* This implementation of {@link Logger} redirects all logging method35* calls to calls to {@code log(Level, String, ResourceBundle, ...)}36* methods, passing the Logger's ResourceBundle as parameter.37* So for instance a call to {@link Logger#log(Level, String)38* log(Level.INFO, msg)} will be redirected39* to a call to {@link #log(java.lang.System.Logger.Level,40* java.util.ResourceBundle, java.lang.String, java.lang.Object...)41* this.log(Level.INFO, this.bundle, msg, (Object[]) null)}.42* <p>43* Note that methods that take a {@link Supplier Supplier<String>}44* or an Object are not redirected. It is assumed that a string returned45* by a {@code Supplier<String>} is already localized, or cannot be localized.46*47* @param <L> Type of the wrapped Logger: {@code Logger} or an48* extension of the {@code Logger} interface.49*/50public class LocalizedLoggerWrapper<L extends Logger> extends LoggerWrapper<L> {5152private final ResourceBundle bundle;5354public LocalizedLoggerWrapper(L wrapped, ResourceBundle bundle) {55super(wrapped);56this.bundle = bundle;57}5859public final ResourceBundle getBundle() {60return bundle;61}6263// We assume that messages returned by Supplier<String> and Object are64// either already localized or not localizable. To be evaluated.6566// -----------------------------------------------------------------67// Generic methods taking a Level as parameter68// -----------------------------------------------------------------6970@Override71public final void log(Level level, String msg) {72log(level, bundle, msg, (Object[]) null);73}7475@Override76public final void log(Level level,77String msg, Throwable thrown) {78log(level, bundle, msg, thrown);79}8081@Override82public final void log(Level level,83String format, Object... params) {84log(level, bundle, format, params);85}8687@Override88public final void log(Level level, Object obj) {89wrapped.log(level, obj);90}9192@Override93public final void log(Level level, Supplier<String> msgSupplier) {94wrapped.log(level, msgSupplier);95}9697@Override98public final void log(Level level, Supplier<String> msgSupplier, Throwable thrown) {99wrapped.log(level, msgSupplier, thrown);100}101102@Override103public final void log(Level level, ResourceBundle bundle, String format, Object... params) {104wrapped.log(level, bundle, format, params);105}106107@Override108public final void log(Level level, ResourceBundle bundle, String key, Throwable thrown) {109wrapped.log(level, bundle, key, thrown);110}111112@Override113public final boolean isLoggable(Level level) {114return wrapped.isLoggable(level);115}116117// Override methods from PlatformLogger.Bridge that don't take a118// resource bundle...119120@Override121public final void logp(sun.util.logging.PlatformLogger.Level level, String sourceClass, String sourceMethod,122String key) {123logrb(level, sourceClass, sourceMethod, bundle, key, (Object[]) null);124}125126@Override127public final void logp(sun.util.logging.PlatformLogger.Level level, String sourceClass, String sourceMethod,128String key, Throwable thrown) {129logrb(level, sourceClass, sourceMethod, bundle, key, thrown);130}131132@Override133public final void logp(sun.util.logging.PlatformLogger.Level level, String sourceClass, String sourceMethod,134String key, Object... params) {135logrb(level, sourceClass, sourceMethod, bundle, key, params);136}137138@Override139public final void log(sun.util.logging.PlatformLogger.Level level, String msg, Throwable thrown) {140logrb(level, bundle, msg, thrown);141}142143@Override144public final void log(sun.util.logging.PlatformLogger.Level level, String msg) {145logrb(level, bundle, msg, (Object[]) null);146}147148@Override149public final void log(sun.util.logging.PlatformLogger.Level level, String format, Object... params) {150logrb(level, bundle, format, params);151}152153154}155156157