< prev index next >

src/java.base/share/classes/jdk/internal/logger/SimpleConsoleLogger.java

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this --- 1,7 ---- /* ! * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this
*** 34,45 **** --- 34,47 ---- import java.time.ZonedDateTime; import java.util.Optional; import java.util.ResourceBundle; import java.util.function.Function; import java.lang.System.Logger; + import java.util.PropertyPermission; import java.util.function.Predicate; import java.util.function.Supplier; + import sun.security.action.GetPropertyAction; import sun.util.logging.PlatformLogger; import sun.util.logging.PlatformLogger.ConfigurableBridge.LoggerConfiguration; /** * A simple console logger to emulate the behavior of JUL loggers when
*** 47,68 **** * JUL is not present and no DefaultLoggerFinder is installed. */ public class SimpleConsoleLogger extends LoggerConfiguration implements Logger, PlatformLogger.Bridge, PlatformLogger.ConfigurableBridge { ! static final PlatformLogger.Level DEFAULT_LEVEL = PlatformLogger.Level.INFO; final String name; volatile PlatformLogger.Level level; final boolean usePlatformLevel; SimpleConsoleLogger(String name, boolean usePlatformLevel) { this.name = name; this.usePlatformLevel = usePlatformLevel; } @Override ! public String getName() { return name; } private Enum<?> logLevel(PlatformLogger.Level level) { return usePlatformLevel ? level : level.systemLevel(); --- 49,90 ---- * JUL is not present and no DefaultLoggerFinder is installed. */ public class SimpleConsoleLogger extends LoggerConfiguration implements Logger, PlatformLogger.Bridge, PlatformLogger.ConfigurableBridge { ! static final Level DEFAULT_LEVEL = getDefaultLevel(); ! static final PlatformLogger.Level DEFAULT_PLATFORM_LEVEL = ! PlatformLogger.toPlatformLevel(DEFAULT_LEVEL); ! ! static Level getDefaultLevel() { ! String levelName = AccessController.doPrivileged( ! new GetPropertyAction("jdk.system.logger.level", "INFO")); ! try { ! return Level.valueOf(levelName); ! } catch (IllegalArgumentException iae) { ! return Level.INFO; ! } ! } final String name; volatile PlatformLogger.Level level; final boolean usePlatformLevel; SimpleConsoleLogger(String name, boolean usePlatformLevel) { this.name = name; this.usePlatformLevel = usePlatformLevel; } + String getSimpleFormatString() { + return Formatting.SIMPLE_CONSOLE_LOGGER_FORMAT; + } + + PlatformLogger.Level defaultPlatformLevel() { + return DEFAULT_PLATFORM_LEVEL; + } + @Override ! public final String getName() { return name; } private Enum<?> logLevel(PlatformLogger.Level level) { return usePlatformLevel ? level : level.systemLevel();
*** 75,100 **** // --------------------------------------------------- // From Logger // --------------------------------------------------- @Override ! public boolean isLoggable(Level level) { return isLoggable(PlatformLogger.toPlatformLevel(level)); } @Override ! public void log(Level level, ResourceBundle bundle, String key, Throwable thrown) { if (isLoggable(level)) { if (bundle != null) { key = bundle.getString(key); } publish(getCallerInfo(), logLevel(level), key, thrown); } } @Override ! public void log(Level level, ResourceBundle bundle, String format, Object... params) { if (isLoggable(level)) { if (bundle != null) { format = bundle.getString(format); } publish(getCallerInfo(), logLevel(level), format, params); --- 97,122 ---- // --------------------------------------------------- // From Logger // --------------------------------------------------- @Override ! public final boolean isLoggable(Level level) { return isLoggable(PlatformLogger.toPlatformLevel(level)); } @Override ! public final void log(Level level, ResourceBundle bundle, String key, Throwable thrown) { if (isLoggable(level)) { if (bundle != null) { key = bundle.getString(key); } publish(getCallerInfo(), logLevel(level), key, thrown); } } @Override ! public final void log(Level level, ResourceBundle bundle, String format, Object... params) { if (isLoggable(level)) { if (bundle != null) { format = bundle.getString(format); } publish(getCallerInfo(), logLevel(level), format, params);
*** 104,162 **** // --------------------------------------------------- // From PlatformLogger.Bridge // --------------------------------------------------- @Override ! public boolean isLoggable(PlatformLogger.Level level) { final PlatformLogger.Level effectiveLevel = effectiveLevel(); return level != PlatformLogger.Level.OFF && level.ordinal() >= effectiveLevel.ordinal(); } @Override ! public boolean isEnabled() { return level != PlatformLogger.Level.OFF; } @Override ! public void log(PlatformLogger.Level level, String msg) { if (isLoggable(level)) { publish(getCallerInfo(), logLevel(level), msg); } } @Override ! public void log(PlatformLogger.Level level, String msg, Throwable thrown) { if (isLoggable(level)) { publish(getCallerInfo(), logLevel(level), msg, thrown); } } @Override ! public void log(PlatformLogger.Level level, String msg, Object... params) { if (isLoggable(level)) { publish(getCallerInfo(), logLevel(level), msg, params); } } private PlatformLogger.Level effectiveLevel() { ! if (level == null) return DEFAULT_LEVEL; return level; } @Override ! public PlatformLogger.Level getPlatformLevel() { return level; } @Override ! public void setPlatformLevel(PlatformLogger.Level newLevel) { level = newLevel; } @Override ! public LoggerConfiguration getLoggerConfiguration() { return this; } /** * Default platform logging support - output messages to System.err - --- 126,184 ---- // --------------------------------------------------- // From PlatformLogger.Bridge // --------------------------------------------------- @Override ! public final boolean isLoggable(PlatformLogger.Level level) { final PlatformLogger.Level effectiveLevel = effectiveLevel(); return level != PlatformLogger.Level.OFF && level.ordinal() >= effectiveLevel.ordinal(); } @Override ! public final boolean isEnabled() { return level != PlatformLogger.Level.OFF; } @Override ! public final void log(PlatformLogger.Level level, String msg) { if (isLoggable(level)) { publish(getCallerInfo(), logLevel(level), msg); } } @Override ! public final void log(PlatformLogger.Level level, String msg, Throwable thrown) { if (isLoggable(level)) { publish(getCallerInfo(), logLevel(level), msg, thrown); } } @Override ! public final void log(PlatformLogger.Level level, String msg, Object... params) { if (isLoggable(level)) { publish(getCallerInfo(), logLevel(level), msg, params); } } private PlatformLogger.Level effectiveLevel() { ! if (level == null) return defaultPlatformLevel(); return level; } @Override ! public final PlatformLogger.Level getPlatformLevel() { return level; } @Override ! public final void setPlatformLevel(PlatformLogger.Level newLevel) { level = newLevel; } @Override ! public final LoggerConfiguration getLoggerConfiguration() { return this; } /** * Default platform logging support - output messages to System.err -
*** 220,230 **** lookingForLogger = !isLoggerImplFrame(cname); return false; } // Continue walking until we've found the relevant calling frame. // Skips logging/logger infrastructure. ! return !isFilteredFrame(t); } private boolean isLoggerImplFrame(String cname) { return (cname.equals("sun.util.logging.PlatformLogger") || cname.equals("jdk.internal.logger.SimpleConsoleLogger")); --- 242,252 ---- lookingForLogger = !isLoggerImplFrame(cname); return false; } // Continue walking until we've found the relevant calling frame. // Skips logging/logger infrastructure. ! return !Formatting.isFilteredFrame(t); } private boolean isLoggerImplFrame(String cname) { return (cname.equals("sun.util.logging.PlatformLogger") || cname.equals("jdk.internal.logger.SimpleConsoleLogger"));
*** 254,264 **** String msg, Throwable thrown, String callerInfo) { ZonedDateTime zdt = ZonedDateTime.now(); String throwable = toString(thrown); ! return String.format(Formatting.formatString, zdt, callerInfo, name, level.name(), msg, --- 276,286 ---- String msg, Throwable thrown, String callerInfo) { ZonedDateTime zdt = ZonedDateTime.now(); String throwable = toString(thrown); ! return String.format(getSimpleFormatString(), zdt, callerInfo, name, level.name(), msg,
*** 278,409 **** msg = params == null || params.length == 0 ? msg : Formatting.formatMessage(msg, params); outputStream().print(format(level, msg, null, callerInfo)); } - public static SimpleConsoleLogger makeSimpleLogger(String name, boolean usePlatformLevel) { - return new SimpleConsoleLogger(name, usePlatformLevel); - } - public static SimpleConsoleLogger makeSimpleLogger(String name) { return new SimpleConsoleLogger(name, false); } - public static String getSimpleFormat(Function<String, String> defaultPropertyGetter) { - return Formatting.getSimpleFormat(defaultPropertyGetter); - } - - public static boolean isFilteredFrame(StackFrame st) { - return Formatting.isFilteredFrame(st); - } - @Override ! public void log(PlatformLogger.Level level, Supplier<String> msgSupplier) { if (isLoggable(level)) { publish(getCallerInfo(), logLevel(level), msgSupplier.get()); } } @Override ! public void log(PlatformLogger.Level level, Throwable thrown, Supplier<String> msgSupplier) { if (isLoggable(level)) { publish(getCallerInfo(), logLevel(level), msgSupplier.get(), thrown); } } @Override ! public void logp(PlatformLogger.Level level, String sourceClass, String sourceMethod, String msg) { if (isLoggable(level)) { publish(getCallerInfo(sourceClass, sourceMethod), logLevel(level), msg); } } @Override ! public void logp(PlatformLogger.Level level, String sourceClass, String sourceMethod, Supplier<String> msgSupplier) { if (isLoggable(level)) { publish(getCallerInfo(sourceClass, sourceMethod), logLevel(level), msgSupplier.get()); } } @Override ! public void logp(PlatformLogger.Level level, String sourceClass, String sourceMethod, String msg, Object... params) { if (isLoggable(level)) { publish(getCallerInfo(sourceClass, sourceMethod), logLevel(level), msg, params); } } @Override ! public void logp(PlatformLogger.Level level, String sourceClass, String sourceMethod, String msg, Throwable thrown) { if (isLoggable(level)) { publish(getCallerInfo(sourceClass, sourceMethod), logLevel(level), msg, thrown); } } @Override ! public void logp(PlatformLogger.Level level, String sourceClass, String sourceMethod, Throwable thrown, Supplier<String> msgSupplier) { if (isLoggable(level)) { publish(getCallerInfo(sourceClass, sourceMethod), logLevel(level), msgSupplier.get(), thrown); } } @Override ! public void logrb(PlatformLogger.Level level, String sourceClass, String sourceMethod, ResourceBundle bundle, String key, Object... params) { if (isLoggable(level)) { String msg = bundle == null ? key : bundle.getString(key); publish(getCallerInfo(sourceClass, sourceMethod), logLevel(level), msg, params); } } @Override ! public void logrb(PlatformLogger.Level level, String sourceClass, String sourceMethod, ResourceBundle bundle, String key, Throwable thrown) { if (isLoggable(level)) { String msg = bundle == null ? key : bundle.getString(key); publish(getCallerInfo(sourceClass, sourceMethod), logLevel(level), msg, thrown); } } @Override ! public void logrb(PlatformLogger.Level level, ResourceBundle bundle, String key, Object... params) { if (isLoggable(level)) { String msg = bundle == null ? key : bundle.getString(key); publish(getCallerInfo(), logLevel(level), msg, params); } } @Override ! public void logrb(PlatformLogger.Level level, ResourceBundle bundle, String key, Throwable thrown) { if (isLoggable(level)) { String msg = bundle == null ? key : bundle.getString(key); publish(getCallerInfo(), logLevel(level), msg, thrown); } } ! private static final class Formatting { static final String DEFAULT_FORMAT = "%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s%n%4$s: %5$s%6$s%n"; ! static final String FORMAT_PROP_KEY = "java.util.logging.SimpleFormatter.format"; ! static final String formatString = getSimpleFormat(null); // Make it easier to wrap Logger... static private final String[] skips; static { String additionalPkgs = AccessController.doPrivileged( ! (PrivilegedAction<String>) ! () -> System.getProperty("jdk.logger.packages")); skips = additionalPkgs == null ? new String[0] : additionalPkgs.split(","); - } static boolean isFilteredFrame(StackFrame st) { // skip logging/logger infrastructure if (System.Logger.class.isAssignableFrom(st.getDeclaringClass())) { --- 300,436 ---- msg = params == null || params.length == 0 ? msg : Formatting.formatMessage(msg, params); outputStream().print(format(level, msg, null, callerInfo)); } public static SimpleConsoleLogger makeSimpleLogger(String name) { return new SimpleConsoleLogger(name, false); } @Override ! public final void log(PlatformLogger.Level level, Supplier<String> msgSupplier) { if (isLoggable(level)) { publish(getCallerInfo(), logLevel(level), msgSupplier.get()); } } @Override ! public final void log(PlatformLogger.Level level, Throwable thrown, Supplier<String> msgSupplier) { if (isLoggable(level)) { publish(getCallerInfo(), logLevel(level), msgSupplier.get(), thrown); } } @Override ! public final void logp(PlatformLogger.Level level, String sourceClass, String sourceMethod, String msg) { if (isLoggable(level)) { publish(getCallerInfo(sourceClass, sourceMethod), logLevel(level), msg); } } @Override ! public final void logp(PlatformLogger.Level level, String sourceClass, String sourceMethod, Supplier<String> msgSupplier) { if (isLoggable(level)) { publish(getCallerInfo(sourceClass, sourceMethod), logLevel(level), msgSupplier.get()); } } @Override ! public final void logp(PlatformLogger.Level level, String sourceClass, String sourceMethod, String msg, Object... params) { if (isLoggable(level)) { publish(getCallerInfo(sourceClass, sourceMethod), logLevel(level), msg, params); } } @Override ! public final void logp(PlatformLogger.Level level, String sourceClass, String sourceMethod, String msg, Throwable thrown) { if (isLoggable(level)) { publish(getCallerInfo(sourceClass, sourceMethod), logLevel(level), msg, thrown); } } @Override ! public final void logp(PlatformLogger.Level level, String sourceClass, String sourceMethod, Throwable thrown, Supplier<String> msgSupplier) { if (isLoggable(level)) { publish(getCallerInfo(sourceClass, sourceMethod), logLevel(level), msgSupplier.get(), thrown); } } @Override ! public final void logrb(PlatformLogger.Level level, String sourceClass, String sourceMethod, ResourceBundle bundle, String key, Object... params) { if (isLoggable(level)) { String msg = bundle == null ? key : bundle.getString(key); publish(getCallerInfo(sourceClass, sourceMethod), logLevel(level), msg, params); } } @Override ! public final void logrb(PlatformLogger.Level level, String sourceClass, String sourceMethod, ResourceBundle bundle, String key, Throwable thrown) { if (isLoggable(level)) { String msg = bundle == null ? key : bundle.getString(key); publish(getCallerInfo(sourceClass, sourceMethod), logLevel(level), msg, thrown); } } @Override ! public final void logrb(PlatformLogger.Level level, ResourceBundle bundle, String key, Object... params) { if (isLoggable(level)) { String msg = bundle == null ? key : bundle.getString(key); publish(getCallerInfo(), logLevel(level), msg, params); } } @Override ! public final void logrb(PlatformLogger.Level level, ResourceBundle bundle, String key, Throwable thrown) { if (isLoggable(level)) { String msg = bundle == null ? key : bundle.getString(key); publish(getCallerInfo(), logLevel(level), msg, thrown); } } ! static final class Formatting { ! // The default simple log format string. ! // Used both by SimpleConsoleLogger when java.logging is not present, ! // and by SurrogateLogger and java.util.logging.SimpleFormatter when ! // java.logging is present. static final String DEFAULT_FORMAT = "%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s%n%4$s: %5$s%6$s%n"; ! ! // The system property key that allows to change the default log format ! // when java.logging is not present. This is used to control the formatting ! // of the SimpleConsoleLogger. ! static final String DEFAULT_FORMAT_PROP_KEY = ! "jdk.system.logger.format"; ! ! // The system property key that allows to change the default log format ! // when java.logging is present. This is used to control the formatting ! // of the SurrogateLogger (used before java.util.logging.LogManager is ! // initialized) and the java.util.logging.SimpleFormatter (used after ! // java.util.logging.LogManager is initialized). ! static final String JUL_FORMAT_PROP_KEY = "java.util.logging.SimpleFormatter.format"; ! ! // The simple console logger format string ! static final String SIMPLE_CONSOLE_LOGGER_FORMAT = ! getSimpleFormat(DEFAULT_FORMAT_PROP_KEY, null); // Make it easier to wrap Logger... static private final String[] skips; static { String additionalPkgs = AccessController.doPrivileged( ! new GetPropertyAction("jdk.logger.packages")); skips = additionalPkgs == null ? new String[0] : additionalPkgs.split(","); } static boolean isFilteredFrame(StackFrame st) { // skip logging/logger infrastructure if (System.Logger.class.isAssignableFrom(st.getDeclaringClass())) {
*** 438,462 **** } return false; } ! static String getSimpleFormat(Function<String, String> defaultPropertyGetter) { // Using a lambda here causes // jdk/test/java/lang/invoke/lambda/LogGeneratedClassesTest.java // to fail - because that test has a testcase which somehow references // PlatformLogger and counts the number of generated lambda classes // So we explicitely use new PrivilegedAction<String> here. ! String format = ! AccessController.doPrivileged(new PrivilegedAction<String>() { ! @Override ! public String run() { ! return System.getProperty(FORMAT_PROP_KEY); ! } ! }); if (format == null && defaultPropertyGetter != null) { ! format = defaultPropertyGetter.apply(FORMAT_PROP_KEY); } if (format != null) { try { // validate the user-defined format string String.format(format, ZonedDateTime.now(), "", "", "", "", ""); --- 465,494 ---- } return false; } ! static String getSimpleFormat(String key, Function<String, String> defaultPropertyGetter) { // Using a lambda here causes // jdk/test/java/lang/invoke/lambda/LogGeneratedClassesTest.java // to fail - because that test has a testcase which somehow references // PlatformLogger and counts the number of generated lambda classes // So we explicitely use new PrivilegedAction<String> here. ! String format = AccessController.doPrivileged( ! new GetPropertyAction(key), null, ! // This permission is needed when this method is called ! // from SimpleConsoleLogger. It used to control the ! // SimpleConsoleLogger format when java.logging is ! // not present. ! new PropertyPermission(DEFAULT_FORMAT_PROP_KEY, "read"), ! // This permission is needed when this method is called ! // from the SurrogateLogger subclass. It used to control the ! // SurrogateLogger format and java.util.logging.SimpleFormatter ! // format when java.logging is present. ! new PropertyPermission(JUL_FORMAT_PROP_KEY, "read")); if (format == null && defaultPropertyGetter != null) { ! format = defaultPropertyGetter.apply(key); } if (format != null) { try { // validate the user-defined format string String.format(format, ZonedDateTime.now(), "", "", "", "", "");
< prev index next >