38 import java.lang.System.Logger;
39 import java.util.function.Predicate;
40 import java.util.function.Supplier;
41 import sun.security.action.GetPropertyAction;
42 import sun.util.logging.PlatformLogger;
43 import sun.util.logging.PlatformLogger.ConfigurableBridge.LoggerConfiguration;
44
45 /**
46 * A simple console logger to emulate the behavior of JUL loggers when
47 * in the default configuration. SimpleConsoleLoggers are also used when
48 * JUL is not present and no DefaultLoggerFinder is installed.
49 */
50 public class SimpleConsoleLogger extends LoggerConfiguration
51 implements Logger, PlatformLogger.Bridge, PlatformLogger.ConfigurableBridge {
52
53 static final Level DEFAULT_LEVEL = getDefaultLevel();
54 static final PlatformLogger.Level DEFAULT_PLATFORM_LEVEL =
55 PlatformLogger.toPlatformLevel(DEFAULT_LEVEL);
56
57 static Level getDefaultLevel() {
58 String levelName = AccessController.doPrivileged(
59 new GetPropertyAction("jdk.system.logger.level", "INFO"));
60 try {
61 return Level.valueOf(levelName);
62 } catch (IllegalArgumentException iae) {
63 return Level.INFO;
64 }
65 }
66
67 final String name;
68 volatile PlatformLogger.Level level;
69 final boolean usePlatformLevel;
70 SimpleConsoleLogger(String name, boolean usePlatformLevel) {
71 this.name = name;
72 this.usePlatformLevel = usePlatformLevel;
73 }
74
75 String getSimpleFormatString() {
76 return Formatting.SIMPLE_CONSOLE_LOGGER_FORMAT;
77 }
78
79 PlatformLogger.Level defaultPlatformLevel() {
408 // when java.logging is not present. This is used to control the formatting
409 // of the SimpleConsoleLogger.
410 static final String DEFAULT_FORMAT_PROP_KEY =
411 "jdk.system.logger.format";
412
413 // The system property key that allows to change the default log format
414 // when java.logging is present. This is used to control the formatting
415 // of the SurrogateLogger (used before java.util.logging.LogManager is
416 // initialized) and the java.util.logging.SimpleFormatter (used after
417 // java.util.logging.LogManager is initialized).
418 static final String JUL_FORMAT_PROP_KEY =
419 "java.util.logging.SimpleFormatter.format";
420
421 // The simple console logger format string
422 static final String SIMPLE_CONSOLE_LOGGER_FORMAT =
423 getSimpleFormat(DEFAULT_FORMAT_PROP_KEY, null);
424
425 // Make it easier to wrap Logger...
426 static private final String[] skips;
427 static {
428 String additionalPkgs = AccessController.doPrivileged(
429 new GetPropertyAction("jdk.logger.packages"));
430 skips = additionalPkgs == null ? new String[0] : additionalPkgs.split(",");
431 }
432
433 static boolean isFilteredFrame(StackFrame st) {
434 // skip logging/logger infrastructure
435 if (System.Logger.class.isAssignableFrom(st.getDeclaringClass())) {
436 return true;
437 }
438
439 // fast escape path: all the prefixes below start with 's' or 'j' and
440 // have more than 12 characters.
441 final String cname = st.getClassName();
442 char c = cname.length() < 12 ? 0 : cname.charAt(0);
443 if (c == 's') {
444 // skip internal machinery classes
445 if (cname.startsWith("sun.util.logging.")) return true;
446 if (cname.startsWith("sun.rmi.runtime.Log")) return true;
447 } else if (c == 'j') {
448 // Message delayed at Bootstrap: no need to go further up.
449 if (cname.startsWith("jdk.internal.logger.BootstrapLogger$LogEvent")) return false;
468
469 static String getSimpleFormat(String key, Function<String, String> defaultPropertyGetter) {
470 // Double check that 'key' is one of the expected property names:
471 // - DEFAULT_FORMAT_PROP_KEY is used to control the
472 // SimpleConsoleLogger format when java.logging is
473 // not present.
474 // - JUL_FORMAT_PROP_KEY is used when this method is called
475 // from the SurrogateLogger subclass. It is used to control the
476 // SurrogateLogger format and java.util.logging.SimpleFormatter
477 // format when java.logging is present.
478 // This method should not be called with any other key.
479 if (!DEFAULT_FORMAT_PROP_KEY.equals(key)
480 && !JUL_FORMAT_PROP_KEY.equals(key)) {
481 throw new IllegalArgumentException("Invalid property name: " + key);
482 }
483
484 // Do not use any lambda in this method. Using a lambda here causes
485 // jdk/test/java/lang/invoke/lambda/LogGeneratedClassesTest.java
486 // to fail - because that test has a testcase which somehow references
487 // PlatformLogger and counts the number of generated lambda classes.
488 String format = AccessController.doPrivileged(new GetPropertyAction(key));
489
490 if (format == null && defaultPropertyGetter != null) {
491 format = defaultPropertyGetter.apply(key);
492 }
493 if (format != null) {
494 try {
495 // validate the user-defined format string
496 String.format(format, ZonedDateTime.now(), "", "", "", "", "");
497 } catch (IllegalArgumentException e) {
498 // illegal syntax; fall back to the default format
499 format = DEFAULT_FORMAT;
500 }
501 } else {
502 format = DEFAULT_FORMAT;
503 }
504 return format;
505 }
506
507
508 // Copied from java.util.logging.Formatter.formatMessage
|
38 import java.lang.System.Logger;
39 import java.util.function.Predicate;
40 import java.util.function.Supplier;
41 import sun.security.action.GetPropertyAction;
42 import sun.util.logging.PlatformLogger;
43 import sun.util.logging.PlatformLogger.ConfigurableBridge.LoggerConfiguration;
44
45 /**
46 * A simple console logger to emulate the behavior of JUL loggers when
47 * in the default configuration. SimpleConsoleLoggers are also used when
48 * JUL is not present and no DefaultLoggerFinder is installed.
49 */
50 public class SimpleConsoleLogger extends LoggerConfiguration
51 implements Logger, PlatformLogger.Bridge, PlatformLogger.ConfigurableBridge {
52
53 static final Level DEFAULT_LEVEL = getDefaultLevel();
54 static final PlatformLogger.Level DEFAULT_PLATFORM_LEVEL =
55 PlatformLogger.toPlatformLevel(DEFAULT_LEVEL);
56
57 static Level getDefaultLevel() {
58 String levelName = GetPropertyAction
59 .getProperty("jdk.system.logger.level", "INFO");
60 try {
61 return Level.valueOf(levelName);
62 } catch (IllegalArgumentException iae) {
63 return Level.INFO;
64 }
65 }
66
67 final String name;
68 volatile PlatformLogger.Level level;
69 final boolean usePlatformLevel;
70 SimpleConsoleLogger(String name, boolean usePlatformLevel) {
71 this.name = name;
72 this.usePlatformLevel = usePlatformLevel;
73 }
74
75 String getSimpleFormatString() {
76 return Formatting.SIMPLE_CONSOLE_LOGGER_FORMAT;
77 }
78
79 PlatformLogger.Level defaultPlatformLevel() {
408 // when java.logging is not present. This is used to control the formatting
409 // of the SimpleConsoleLogger.
410 static final String DEFAULT_FORMAT_PROP_KEY =
411 "jdk.system.logger.format";
412
413 // The system property key that allows to change the default log format
414 // when java.logging is present. This is used to control the formatting
415 // of the SurrogateLogger (used before java.util.logging.LogManager is
416 // initialized) and the java.util.logging.SimpleFormatter (used after
417 // java.util.logging.LogManager is initialized).
418 static final String JUL_FORMAT_PROP_KEY =
419 "java.util.logging.SimpleFormatter.format";
420
421 // The simple console logger format string
422 static final String SIMPLE_CONSOLE_LOGGER_FORMAT =
423 getSimpleFormat(DEFAULT_FORMAT_PROP_KEY, null);
424
425 // Make it easier to wrap Logger...
426 static private final String[] skips;
427 static {
428 String additionalPkgs =
429 GetPropertyAction.getProperty("jdk.logger.packages");
430 skips = additionalPkgs == null ? new String[0] : additionalPkgs.split(",");
431 }
432
433 static boolean isFilteredFrame(StackFrame st) {
434 // skip logging/logger infrastructure
435 if (System.Logger.class.isAssignableFrom(st.getDeclaringClass())) {
436 return true;
437 }
438
439 // fast escape path: all the prefixes below start with 's' or 'j' and
440 // have more than 12 characters.
441 final String cname = st.getClassName();
442 char c = cname.length() < 12 ? 0 : cname.charAt(0);
443 if (c == 's') {
444 // skip internal machinery classes
445 if (cname.startsWith("sun.util.logging.")) return true;
446 if (cname.startsWith("sun.rmi.runtime.Log")) return true;
447 } else if (c == 'j') {
448 // Message delayed at Bootstrap: no need to go further up.
449 if (cname.startsWith("jdk.internal.logger.BootstrapLogger$LogEvent")) return false;
468
469 static String getSimpleFormat(String key, Function<String, String> defaultPropertyGetter) {
470 // Double check that 'key' is one of the expected property names:
471 // - DEFAULT_FORMAT_PROP_KEY is used to control the
472 // SimpleConsoleLogger format when java.logging is
473 // not present.
474 // - JUL_FORMAT_PROP_KEY is used when this method is called
475 // from the SurrogateLogger subclass. It is used to control the
476 // SurrogateLogger format and java.util.logging.SimpleFormatter
477 // format when java.logging is present.
478 // This method should not be called with any other key.
479 if (!DEFAULT_FORMAT_PROP_KEY.equals(key)
480 && !JUL_FORMAT_PROP_KEY.equals(key)) {
481 throw new IllegalArgumentException("Invalid property name: " + key);
482 }
483
484 // Do not use any lambda in this method. Using a lambda here causes
485 // jdk/test/java/lang/invoke/lambda/LogGeneratedClassesTest.java
486 // to fail - because that test has a testcase which somehow references
487 // PlatformLogger and counts the number of generated lambda classes.
488 String format = GetPropertyAction.getProperty(key);
489
490 if (format == null && defaultPropertyGetter != null) {
491 format = defaultPropertyGetter.apply(key);
492 }
493 if (format != null) {
494 try {
495 // validate the user-defined format string
496 String.format(format, ZonedDateTime.now(), "", "", "", "", "");
497 } catch (IllegalArgumentException e) {
498 // illegal syntax; fall back to the default format
499 format = DEFAULT_FORMAT;
500 }
501 } else {
502 format = DEFAULT_FORMAT;
503 }
504 return format;
505 }
506
507
508 // Copied from java.util.logging.Formatter.formatMessage
|