SourceClassName.java revision 14606:bc3775e25b52
1/* 2 * Copyright (c) 2010, 2016, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @bug 6985460 27 * @summary Test the source class name and method output by the platform 28 * logger. 29 * 30 * @modules java.base/sun.util.logging 31 * java.logging 32 * @compile -XDignore.symbol.file SourceClassName.java 33 * @run main/othervm SourceClassName 34 */ 35 36import java.util.logging.*; 37import java.io.*; 38import sun.util.logging.PlatformLogger; 39 40public class SourceClassName { 41 public static void main(String[] args) throws Exception { 42 File dir = new File(System.getProperty("user.dir", ".")); 43 File log = new File(dir, "testlog.txt"); 44 PrintStream logps = new PrintStream(log); 45 writeLogRecords(logps); 46 checkLogRecords(log); 47 } 48 49 private static void writeLogRecords(PrintStream logps) throws Exception { 50 PrintStream err = System.err; 51 try { 52 System.setErr(logps); 53 54 Object[] params = new Object[] { new Long(1), "string"}; 55 PlatformLogger plog = PlatformLogger.getLogger("test.log.foo"); 56 plog.severe("Log message {0} {1}", (Object[]) params); 57 58 // create a java.util.logging.Logger 59 // now java.util.logging.Logger should be created for each platform 60 // logger 61 Logger logger = Logger.getLogger("test.log.bar"); 62 logger.log(Level.SEVERE, "Log message {0} {1}", params); 63 64 plog.severe("Log message {0} {1}", (Object[]) params); 65 } finally { 66 logps.flush(); 67 logps.close(); 68 System.setErr(err); 69 } 70 } 71 72 private static void checkLogRecords(File log) throws Exception { 73 System.out.println("Checking log records in file: " + log); 74 FileInputStream in = new FileInputStream(log); 75 String EXPECTED_LOG = "SEVERE: Log message 1 string"; 76 try { 77 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 78 String line; 79 String[] record = new String[2]; 80 int count = 0; 81 int i = 0; 82 while ((line = reader.readLine()) != null) { 83 line = line.trim(); 84 System.out.println(line); 85 record[i++] = line; 86 if (i == 2) { 87 i = 0; 88 // check log message 89 if (!record[1].equals(EXPECTED_LOG)) { 90 // it can sometime happen that some static initializer 91 // in the system will log an error message - due to e.g. 92 // some kind of misconfiguration or system settings. 93 // For instance - somethink like: 94 // INFO: currency.properties entry for FR ignored 95 // because the value format is not recognized. 96 // instead of failing if we get such an unexpected 97 // message, we will simply print that out. 98 System.out.println("*** WARNING: Unexpected log: " + record[1]); 99 continue; 100 } 101 count++; 102 // check source class name and method 103 String[] ss = record[0].split("\\s+"); 104 int len = ss.length; 105 if (!ss[len-2].equals("SourceClassName") || 106 !ss[len-1].equals("writeLogRecords")) { 107 throw new RuntimeException("Unexpected source: " + 108 ss[len-2] + " " + ss[len-1]); 109 } 110 111 } 112 } 113 if (count != 3) { 114 throw new RuntimeException("Unexpected number of records: " + count); 115 } 116 } finally { 117 in.close(); 118 } 119 } 120} 121