test/jdk/java/util/logging/LogManager/Configuration/rootLoggerHandlers/RootLoggerHandlers.java
author dfuchs
Fri, 26 Jan 2018 14:15:09 +0000
changeset 48674 e2a7856edfba
parent 48221 0ba2a82e4755
child 48770 a0475462616e
permissions -rw-r--r--
8195096: Exception printed on console with custom LogManager on starting Apache Tomcat Summary: make sure that loadLoggerHandler for ".handler" is called only from within addLogger Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48221
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
     1
/*
48674
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
     2
 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
48221
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
     4
 *
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
     8
 *
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    13
 * accompanied this code).
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    14
 *
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    18
 *
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    21
 * questions.
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    22
 */
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    23
import java.io.IOException;
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    24
import java.nio.file.Files;
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    25
import java.nio.file.Path;
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    26
import java.nio.file.Paths;
48674
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
    27
import java.nio.file.StandardCopyOption;
48221
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    28
import java.util.Collections;
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    29
import java.util.List;
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    30
import java.util.logging.Handler;
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    31
import java.util.logging.Level;
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    32
import java.util.logging.LogManager;
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    33
import java.util.logging.Logger;
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    34
import java.util.stream.Collectors;
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    35
import java.util.stream.Stream;
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    36
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    37
/**
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    38
 * @test
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    39
 * @bug 8191033
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    40
 * @build custom.DotHandler custom.Handler
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    41
 * @run main/othervm RootLoggerHandlers
48674
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
    42
 * @run main/othervm/java.security.policy==test.policy RootLoggerHandlers
48221
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    43
 * @author danielfuchs
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    44
 */
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    45
public class RootLoggerHandlers {
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    46
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    47
    public static final Path SRC_DIR =
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    48
            Paths.get(System.getProperty("test.src", "src"));
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    49
    public static final Path USER_DIR =
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    50
            Paths.get(System.getProperty("user.dir", "."));
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    51
    public static final Path CONFIG_FILE = Paths.get("logging.properties");
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    52
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    53
    // Uncomment this to run the test on Java 8. Java 8 does not have
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    54
    // List.of(...)
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    55
    //    static final class List {
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    56
    //        static <T> java.util.List<T> of(T... items) {
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    57
    //            return Collections.unmodifiableList(Arrays.asList(items));
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    58
    //        }
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    59
    //    }
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    60
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    61
    public static void main(String[] args) throws IOException {
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    62
        Path initialProps = SRC_DIR.resolve(CONFIG_FILE);
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    63
        Path loggingProps = USER_DIR.resolve(CONFIG_FILE);
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    64
        System.setProperty("java.util.logging.config.file", loggingProps.toString());
48674
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
    65
        Files.copy(initialProps, loggingProps, StandardCopyOption.REPLACE_EXISTING);
48221
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    66
        System.out.println("Root level is: " + Logger.getLogger("").getLevel());
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    67
        if (Logger.getLogger("").getLevel() != Level.INFO) {
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    68
            throw new RuntimeException("Expected root level INFO, got: "
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    69
                                        + Logger.getLogger("").getLevel());
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    70
        }
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    71
        // Verify that we have two handlers. One was configured with
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    72
        // handlers=custom.Handler, the other with
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    73
        // .handlers=custom.DotHandler
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    74
        // Verify that exactly one of the two handlers is a custom.Handler
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    75
        // Verify that exactly one of the two handlers is a custom.DotHandler
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    76
        // Verify that the two handlers has an id of '1'
48674
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
    77
        checkHandlers(Logger.getLogger(""),
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
    78
                Logger.getLogger("").getHandlers(),
48221
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    79
                1L,
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    80
                custom.Handler.class,
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    81
                custom.DotHandler.class);
48674
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
    82
        checkHandlers(Logger.getLogger("global"),
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
    83
                Logger.getGlobal().getHandlers(),
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
    84
                1L,
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
    85
                custom.GlobalHandler.class);
48221
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    86
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    87
        // The log message "hi" should appear twice on the console.
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    88
        // We don't check that. This is just for log analysis in case
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    89
        // of test failure.
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    90
        Logger.getAnonymousLogger().info("hi");
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    91
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    92
        // Change the root logger level to FINE in the properties file
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    93
        // and reload the configuration.
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    94
        Files.write(loggingProps,
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    95
                Files.lines(initialProps)
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    96
                        .map((s) -> s.replace("INFO", "FINE"))
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    97
                        .collect(Collectors.toList()));
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    98
        LogManager.getLogManager().readConfiguration();
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
    99
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   100
        System.out.println("Root level is: " + Logger.getLogger("").getLevel());
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   101
        if (Logger.getLogger("").getLevel() != Level.FINE) {
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   102
            throw new RuntimeException("Expected root level FINE, got: "
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   103
                    + Logger.getLogger("").getLevel());
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   104
        }
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   105
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   106
        // Verify that we have now only one handler, configured with
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   107
        // handlers=custom.Handler, and that the other configured with
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   108
        // .handlers=custom.DotHandler was ignored.
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   109
        // Verify that the handler is a custom.Handler
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   110
        // Verify that the handler has an id of '2'
48674
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   111
        checkHandlers(Logger.getLogger(""),
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   112
                Logger.getLogger("").getHandlers(),
48221
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   113
                2L,
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   114
                custom.Handler.class);
48674
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   115
        checkHandlers(Logger.getGlobal(),
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   116
                Logger.getGlobal().getHandlers(),
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   117
                1L);
48221
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   118
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   119
        // The log message "there" should appear only once on the console.
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   120
        // We don't check that. This is just for log analysis in case
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   121
        // of test failure.
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   122
        Logger.getAnonymousLogger().info("there!");
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   123
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   124
        // Change the root logger level to FINER in the properties file
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   125
        // and reload the configuration.
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   126
        Files.write(loggingProps,
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   127
                Files.lines(initialProps)
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   128
                        .map((s) -> s.replace("INFO", "FINER"))
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   129
                        .collect(Collectors.toList()));
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   130
        LogManager.getLogManager().readConfiguration();
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   131
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   132
        System.out.println("Root level is: " + Logger.getLogger("").getLevel());
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   133
        if (Logger.getLogger("").getLevel() != Level.FINER) {
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   134
            throw new RuntimeException("Expected root level FINER, got: "
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   135
                    + Logger.getLogger("").getLevel());
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   136
        }
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   137
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   138
        // Verify that we have only one handler, configured with
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   139
        // handlers=custom.Handler, and that the other configured with
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   140
        // .handlers=custom.DotHandler was ignored.
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   141
        // Verify that the handler is a custom.Handler
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   142
        // Verify that the handler has an id of '3'
48674
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   143
        checkHandlers(Logger.getLogger(""),
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   144
                Logger.getLogger("").getHandlers(),
48221
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   145
                3L,
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   146
                custom.Handler.class);
48674
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   147
        checkHandlers(Logger.getGlobal(),
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   148
                Logger.getGlobal().getHandlers(),
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   149
                1L);
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   150
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   151
        LogManager.getLogManager().reset();
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   152
        LogManager.getLogManager().updateConfiguration((s) -> (o,n) -> n);
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   153
        // Verify that we have only one handler, configured with
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   154
        // handlers=custom.Handler, and that the other configured with
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   155
        // .handlers=custom.DotHandler was ignored.
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   156
        // Verify that the handler is a custom.Handler
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   157
        // Verify that the handler has an id of '4'
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   158
        checkHandlers(Logger.getLogger(""),
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   159
                Logger.getLogger("").getHandlers(),
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   160
                4L,
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   161
                custom.Handler.class);
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   162
        checkHandlers(Logger.getGlobal(),
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   163
                Logger.getGlobal().getHandlers(),
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   164
                2L,
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   165
                custom.GlobalHandler.class);
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   166
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   167
        LogManager.getLogManager().updateConfiguration((s) -> (o,n) -> n);
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   168
        // Verify that we have only one handler, configured with
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   169
        // handlers=custom.Handler, and that the other configured with
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   170
        // .handlers=custom.DotHandler was ignored.
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   171
        // Verify that the handler is a custom.Handler
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   172
        // Verify that the handler has an id of '4'
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   173
        checkHandlers(Logger.getLogger(""),
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   174
                Logger.getLogger("").getHandlers(),
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   175
                4L,
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   176
                custom.Handler.class);
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   177
        checkHandlers(Logger.getGlobal(),
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   178
                Logger.getGlobal().getHandlers(),
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   179
                2L,
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   180
                custom.GlobalHandler.class);
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   181
48221
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   182
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   183
        // The log message "done" should appear only once on the console.
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   184
        // We don't check that. This is just for log analysis in case
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   185
        // of test failure.
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   186
        Logger.getAnonymousLogger().info("done!");
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   187
    }
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   188
48674
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   189
    static void checkHandlers(Logger logger, Handler[] handlers, Long expectedID, Class<?>... clz) {
48221
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   190
        // Verify that we have the expected number of handlers.
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   191
        if (Stream.of(handlers).count() != clz.length) {
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   192
            throw new RuntimeException("Expected " + clz.length + " handlers, got: "
48674
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   193
                    + List.of(logger.getHandlers()));
48221
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   194
        }
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   195
        for (Class<?> cl : clz) {
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   196
            // Verify that the handlers are of the expected class.
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   197
            // For each class, we should have exactly one handler
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   198
            // of that class.
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   199
            if (Stream.of(handlers)
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   200
                    .map(Object::getClass)
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   201
                    .filter(cl::equals)
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   202
                    .count() != 1) {
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   203
                throw new RuntimeException("Expected one " + cl +", got: "
48674
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   204
                        + List.of(logger.getHandlers()));
48221
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   205
            }
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   206
        }
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   207
        // Verify that all handlers have the expected ID
48674
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   208
        if (Stream.of(logger.getHandlers())
48221
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   209
                .map(RootLoggerHandlers::getId)
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   210
                .filter(expectedID::equals)
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   211
                .count() != clz.length) {
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   212
            throw new RuntimeException("Expected ids to be " + expectedID + ", got: "
48674
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   213
                    + List.of(logger.getHandlers()));
48221
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   214
        }
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   215
    }
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   216
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   217
    static long getId(Handler h) {
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   218
        if (h instanceof custom.Handler) {
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   219
            return ((custom.Handler)h).id;
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   220
        }
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   221
        if (h instanceof custom.DotHandler) {
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   222
            return ((custom.DotHandler)h).id;
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   223
        }
48674
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   224
        if (h instanceof custom.GlobalHandler) {
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   225
            return ((custom.GlobalHandler)h).id;
e2a7856edfba 8195096: Exception printed on console with custom LogManager on starting Apache Tomcat
dfuchs
parents: 48221
diff changeset
   226
        }
48221
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   227
        return -1;
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   228
    }
0ba2a82e4755 8191033: Regression in logging.properties: specifying .handlers= for root logger (instead of handlers=) no longer works
dfuchs
parents:
diff changeset
   229
}