1 /* 2 * Copyright (c) 2011, 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 6381464 8153666 27 * @summary Test the custom simple formatter output 28 * 29 * @run main/othervm SimpleFormatterFormat 30 */ 31 32 import java.io.*; 33 import java.util.logging.*; 34 import java.util.regex.*; 35 36 public class SimpleFormatterFormat { 37 private static final String key = "java.util.logging.SimpleFormatter.format"; 38 private static final String origFormat = System.getProperty(key); 39 private static final PrintStream err = System.err; main(String[] args)40 public static void main(String[] args) throws Exception { 41 try { 42 File dir = new File(System.getProperty("user.dir", ".")); 43 File log = new File(dir, "simpleformat.txt"); 44 java.nio.file.Files.deleteIfExists(log.toPath()); 45 PrintStream logps = new PrintStream(log); 46 System.setProperty(key, "%3$s:%4$s: %5$s [%1$tc] source: %2$s%6$s%n"); 47 writeLogRecords(logps); 48 checkLogRecords(log); 49 } finally { 50 if (origFormat == null) { 51 System.clearProperty(key); 52 } else { 53 System.setProperty(key, origFormat); 54 } 55 System.setErr(err); 56 } 57 } 58 59 private static String[] loggers = new String[] { 60 "test.foo", 61 "test.foo", 62 "test.bar", 63 "test.bar", 64 "test.bar", 65 "test.bar" 66 }; 67 private static String[] messages = new String[] { 68 "severe hello world", 69 "warning lost connection", 70 "info welcome", 71 "warning beware of traps", 72 "warning { {ok7} }", 73 // keep exception logging as last test case to avoid having 74 // to skip the exception stack trace in the output 75 "warning exception thrown" 76 }; writeLogRecords(PrintStream logps)77 private static void writeLogRecords(PrintStream logps) throws Exception { 78 try { 79 System.setErr(logps); 80 81 Logger foo = Logger.getLogger("test.foo"); 82 foo.log(Level.SEVERE, "{0} {1} {2}", new Object[] {"severe", "hello", "world"}); 83 foo.warning(messages[1]); 84 85 Logger bar = Logger.getLogger("test.bar"); 86 bar.finest("Dummy message"); 87 bar.info(messages[2]); 88 bar.log(Level.WARNING, "{0}", new Object[] { messages[3] }); 89 bar.log(Level.WARNING, "warning '{' '{'{7}} }", new Object[] {"ok", "ok1", "ok2", "ok3", "ok4", "ok5", "ok6", "ok7", "ok8", "ok9", "ok10"}); 90 91 // Keep this one last - as it also prints the exception stack trace... 92 bar.log(Level.WARNING, messages[messages.length-1], new IllegalArgumentException()); 93 } finally { 94 logps.flush(); 95 logps.close(); 96 System.setErr(err); 97 } 98 } 99 checkLogRecords(File log)100 private static void checkLogRecords(File log) throws Exception { 101 System.out.println("Checking log records in file: " + log); 102 Pattern p = Pattern.compile("([\\.a-zA-Z:]+) (.*) \\[.*\\] source: (.*)"); 103 104 try (FileInputStream in = new FileInputStream(log); 105 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 106 ) { 107 String line; 108 int i = 0; 109 while (i < messages.length && 110 (line = reader.readLine()) != null) { 111 String expectedLogger = loggers[i]; 112 String expectedMsg = messages[i]; 113 i++; 114 115 line = line.trim(); 116 System.out.println(line); 117 118 Matcher m = p.matcher(line); 119 if (!m.matches()) { 120 throw new RuntimeException("Unexpected output format: " + line); 121 } 122 if (m.groupCount() != 3) { 123 throw new RuntimeException("Unexpected group count = " + 124 m.groupCount()); 125 } 126 // verify logger name and level 127 String[] ss = m.group(1).split(":"); 128 int len = ss.length; 129 if (len != 2) { 130 throw new RuntimeException("Unexpected logger name and level" + 131 m.group(1)); 132 } 133 134 verify(expectedLogger, expectedMsg, ss[0], ss[1], m.group(2), m.group(3)); 135 } 136 137 // expect IllegalArgumentException following it 138 line = reader.readLine().trim(); 139 if (!line.equals("java.lang.IllegalArgumentException")) { 140 throw new RuntimeException("Invalid line: " + line); 141 } 142 } 143 } 144 verify(String expectedLogger, String expectedMsg, String logger, String level, String msg, String source)145 private static void verify(String expectedLogger, String expectedMsg, 146 String logger, String level, 147 String msg, String source) { 148 if (!logger.equals(expectedLogger)) { 149 throw new RuntimeException("Unexpected logger: " + logger); 150 } 151 if (!msg.equals(expectedMsg)) { 152 throw new RuntimeException("Unexpected message: " + msg); 153 } 154 155 String[] ss = expectedMsg.split("\\s+"); 156 String expectedLevel = ss[0].toUpperCase(); 157 if (!level.equals(expectedLevel)) { 158 throw new RuntimeException("Unexpected level: " + level); 159 } 160 161 ss = source.split("\\s+"); 162 int len = ss.length; 163 if (!(len == 2 && 164 ss[0].equals("SimpleFormatterFormat") && 165 ss[1].equals("writeLogRecords"))) { 166 throw new RuntimeException("Unexpected source: " + source); 167 } 168 } 169 } 170