public void testOwnFormatter() throws UnsupportedEncodingException { class MyFrmtr extends Formatter { private int cnt; @Override public String format(LogRecord record) { cnt++; return record.getMessage(); } } MyFrmtr my = new MyFrmtr(); ByteArrayOutputStream os = new ByteArrayOutputStream(); StreamHandler sh = new StreamHandler(os, NbFormatter.FORMATTER); DispatchingHandler dh = new DispatchingHandler(sh, 10); dh.setFormatter(my); dh.publish(new LogRecord(Level.WARNING, "Ahoj")); dh.flush(); String res = new String(os.toByteArray(), "UTF-8"); assertEquals("Only the message is written", "Ahoj", res); assertEquals("Called once", 1, my.cnt); }
protected static Logger createLogger() { // Initialize logging StreamHandler handler = new StreamHandler(System.out, new SimpleFormatter()); handler.setLevel(Level.ALL); Logger logger = Logger.getLogger(GeFLoRegistry.class.getName()); logger.addHandler(handler); /* * Change the level if you want to... * - disable logging to Level.OFF * - enable logging to Level.ALL */ logger.setLevel(Level.ALL); return logger; }
private void testLogger( StreamHandler afh ) { Handler[] handlers = LOGGER.getHandlers(); for ( Handler handler : handlers ) { try { LOGGER.removeHandler( handler ); } catch ( Exception e ) { e.printStackTrace(); } } afh.setFormatter( new SimpleFormatter() ); afh.setLevel( Level.FINEST ); LOGGER.addHandler( afh ); LOGGER.setLevel( Level.FINEST ); final int threadCount = 20; final int logEntryCount = 100000; generateLogs( threadCount, logEntryCount ); afh.close(); }
public void testConstructor_NoParameter_NoProperties() { assertNull(LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.level")); assertNull(LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.filter")); assertNull(LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.formatter")); assertNull(LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.encoding")); StreamHandler h = new StreamHandler(); assertSame(Level.INFO, h.getLevel()); assertTrue(h.getFormatter() instanceof SimpleFormatter); assertNull(h.getFilter()); assertNull(h.getEncoding()); }
public void testConstructor_NoParameter_ValidProperties() throws Exception { Properties p = new Properties(); p.put("java.util.logging.StreamHandler.level", "FINE"); p.put("java.util.logging.StreamHandler.filter", className + "$MockFilter"); p.put("java.util.logging.StreamHandler.formatter", className + "$MockFormatter"); p.put("java.util.logging.StreamHandler.encoding", "iso-8859-1"); LogManager.getLogManager().readConfiguration( EnvironmentHelper.PropertiesToInputStream(p)); assertEquals("FINE", LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.level")); assertEquals("iso-8859-1", LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.encoding")); StreamHandler h = new StreamHandler(); assertSame(h.getLevel(), Level.parse("FINE")); assertTrue(h.getFormatter() instanceof MockFormatter); assertTrue(h.getFilter() instanceof MockFilter); assertEquals("iso-8859-1", h.getEncoding()); }
public void testConstructor_NoParameter_InvalidProperties() throws Exception { Properties p = new Properties(); p.put("java.util.logging.StreamHandler.level", INVALID_LEVEL); p.put("java.util.logging.StreamHandler.filter", className + ""); p.put("java.util.logging.StreamHandler.formatter", className + ""); p.put("java.util.logging.StreamHandler.encoding", "XXXX"); LogManager.getLogManager().readConfiguration( EnvironmentHelper.PropertiesToInputStream(p)); assertEquals(INVALID_LEVEL, LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.level")); assertEquals("XXXX", LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.encoding")); StreamHandler h = new StreamHandler(); assertSame(Level.INFO, h.getLevel()); assertTrue(h.getFormatter() instanceof SimpleFormatter); assertNull(h.getFilter()); assertNull(h.getEncoding()); h.publish(new LogRecord(Level.SEVERE, "test")); assertTrue(CallVerificationStack.getInstance().empty()); assertNull(h.getEncoding()); }
public void testConstructor_HasParameters_NoProperties() { assertNull(LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.level")); assertNull(LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.filter")); assertNull(LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.formatter")); assertNull(LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.encoding")); StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), new MockFormatter2()); assertSame(Level.INFO, h.getLevel()); assertTrue(h.getFormatter() instanceof MockFormatter2); assertNull(h.getFilter()); assertNull(h.getEncoding()); }
public void testConstructor_HasParameters_ValidProperties() throws Exception { Properties p = new Properties(); p.put("java.util.logging.StreamHandler.level", "FINE"); p.put("java.util.logging.StreamHandler.filter", className + "$MockFilter"); p.put("java.util.logging.StreamHandler.formatter", className + "$MockFormatter"); p.put("java.util.logging.StreamHandler.encoding", "iso-8859-1"); LogManager.getLogManager().readConfiguration( EnvironmentHelper.PropertiesToInputStream(p)); assertEquals("FINE", LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.level")); assertEquals("iso-8859-1", LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.encoding")); StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), new MockFormatter2()); assertSame(h.getLevel(), Level.parse("FINE")); assertTrue(h.getFormatter() instanceof MockFormatter2); assertTrue(h.getFilter() instanceof MockFilter); assertEquals("iso-8859-1", h.getEncoding()); }
public void testConstructor_HasParameters_InvalidProperties() throws Exception { Properties p = new Properties(); p.put("java.util.logging.StreamHandler.level", INVALID_LEVEL); p.put("java.util.logging.StreamHandler.filter", className + ""); p.put("java.util.logging.StreamHandler.formatter", className + ""); p.put("java.util.logging.StreamHandler.encoding", "XXXX"); LogManager.getLogManager().readConfiguration( EnvironmentHelper.PropertiesToInputStream(p)); assertEquals(INVALID_LEVEL, LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.level")); assertEquals("XXXX", LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.encoding")); StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), new MockFormatter2()); assertSame(Level.INFO, h.getLevel()); assertTrue(h.getFormatter() instanceof MockFormatter2); assertNull(h.getFilter()); assertNull(h.getEncoding()); }
public void testConstructor_HasParameters_ValidPropertiesNullStream() throws Exception { Properties p = new Properties(); p.put("java.util.logging.StreamHandler.level", "FINE"); p.put("java.util.logging.StreamHandler.filter", className + "$MockFilter"); p.put("java.util.logging.StreamHandler.formatter", className + "$MockFormatter"); p.put("java.util.logging.StreamHandler.encoding", "iso-8859-1"); LogManager.getLogManager().readConfiguration( EnvironmentHelper.PropertiesToInputStream(p)); assertEquals("FINE", LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.level")); assertEquals("iso-8859-1", LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.encoding")); try { new StreamHandler(new ByteArrayOutputStream(), null); fail("Should throw NullPointerException!"); } catch (NullPointerException e) { // expected } }
public void testConstructor_HasParameters_ValidPropertiesNullFormatter() throws Exception { Properties p = new Properties(); p.put("java.util.logging.StreamHandler.level", "FINE"); p.put("java.util.logging.StreamHandler.filter", className + "$MockFilter"); p.put("java.util.logging.StreamHandler.formatter", className + "$MockFormatter"); p.put("java.util.logging.StreamHandler.encoding", "iso-8859-1"); LogManager.getLogManager().readConfiguration( EnvironmentHelper.PropertiesToInputStream(p)); assertEquals("FINE", LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.level")); assertEquals("iso-8859-1", LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.encoding")); try { new StreamHandler(null, new MockFormatter2()); fail("Should throw NullPointerException!"); } catch (NullPointerException e) { // expected } }
public void testIsLoggable_NoFilter() { StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), new SimpleFormatter()); LogRecord r = new LogRecord(Level.INFO, null); assertTrue(h.isLoggable(r)); h.setLevel(Level.WARNING); assertFalse(h.isLoggable(r)); h.setLevel(Level.CONFIG); assertTrue(h.isLoggable(r)); r.setLevel(Level.OFF); h.setLevel(Level.OFF); assertFalse(h.isLoggable(r)); }
public void testIsLoggable_WithFilter() { StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), new SimpleFormatter()); LogRecord r = new LogRecord(Level.INFO, null); h.setFilter(new MockFilter()); assertFalse(h.isLoggable(r)); assertSame(r, CallVerificationStack.getInstance().pop()); h.setLevel(Level.CONFIG); assertFalse(h.isLoggable(r)); assertSame(r, CallVerificationStack.getInstance().pop()); h.setLevel(Level.WARNING); assertFalse(h.isLoggable(r)); assertTrue(CallVerificationStack.getInstance().empty()); }
public void testPublish_AfterClose() throws Exception { Properties p = new Properties(); p.put("java.util.logging.StreamHandler.level", "FINE"); LogManager.getLogManager().readConfiguration( EnvironmentHelper.PropertiesToInputStream(p)); ByteArrayOutputStream aos = new ByteArrayOutputStream(); StreamHandler h = new StreamHandler(aos, new MockFormatter()); assertSame(h.getLevel(), Level.FINE); LogRecord r = new LogRecord(Level.INFO, "testPublish_NoFormatter"); assertTrue(h.isLoggable(r)); h.close(); assertFalse(h.isLoggable(r)); h.publish(r); h.flush(); assertEquals("MockFormatter_HeadMockFormatter_Tail", aos.toString()); }
/** * This function initializes and returns the logger. * * @return The logger */ protected static final Logger initializeLogger() { //get logger Logger logger=Logger.getLogger(FaxClient.class.getName()); //enable all log events (fax4j logger filters out uneeded log events) logger.setLevel(Level.ALL); logger.setFilter(null); //enable to pass log events to parent loggers logger.setUseParentHandlers(true); //create handler Formatter formatter=new SimpleFormatter(); Handler handler=new StreamHandler(System.out,formatter); //set filtering handler.setLevel(logger.getLevel()); handler.setFilter(logger.getFilter()); //add handler logger.addHandler(handler); return logger; }
/** * The main method to run from another program. * Parses the arguments, and performs the actual work * on JAR signing and verification. * If something goes wrong an exception is thrown. * * @param args - * command line with options. */ public static void run(String[] args, OutputStream out) throws Exception { // set up logging Logger logger = Logger.getLogger(JSParameters.loggerName); logger.setUseParentHandlers(false); Handler handler = new StreamHandler(out, new JSLogFormatter()); logger.addHandler(handler); // parse command line arguments JSParameters param = ArgParser.parseArgs(args, null); // print help if incorrect or no arguments if (param == null) { JSHelper.printHelp(); return; } // do the actual work if (param.isVerify()) { JSVerifier.verifyJar(param); } else { JSSigner.signJar(param); } }
public void testConstructor_NoParameter_InsufficientPrivilege() { assertNull(LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.level")); assertNull(LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.filter")); assertNull(LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.formatter")); assertNull(LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.encoding")); SecurityManager oldMan = System.getSecurityManager(); System.setSecurityManager(new MockSecurityManager()); // set a normal value try { StreamHandler h = new StreamHandler(); assertSame(Level.INFO, h.getLevel()); assertTrue(h.getFormatter() instanceof SimpleFormatter); assertNull(h.getFilter()); assertNull(h.getEncoding()); } finally { System.setSecurityManager(oldMan); } }
public void testConstructor_HasParameter_InsufficientPrivilege() { assertNull(LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.level")); assertNull(LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.filter")); assertNull(LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.formatter")); assertNull(LogManager.getLogManager().getProperty( "java.util.logging.StreamHandler.encoding")); SecurityManager oldMan = System.getSecurityManager(); System.setSecurityManager(new MockSecurityManager()); // set a normal value try { StreamHandler h = new StreamHandler(new ByteArrayOutputStream(), new MockFormatter2()); assertSame(Level.INFO, h.getLevel()); assertTrue(h.getFormatter() instanceof MockFormatter2); assertNull(h.getFilter()); assertNull(h.getEncoding()); } finally { System.setSecurityManager(oldMan); } }
/** Creates new form SudokuConsoleFrame */ public SudokuConsoleFrame() { initComponents(); PrintStream newOut = new PrintStream(new ConsoleOutputStream()); System.setOut(newOut); System.setErr(newOut); consoleHandler = new StreamHandler(newOut, new SimpleFormatter()); Logger rootLogger = Logger.getLogger(""); rootLogger.addHandler(consoleHandler); rootLogger.setLevel(Level.CONFIG); getRootPane().setDefaultButton(closeButton); }
private static StreamHandler getTracingHandler( ) { if ( tracingHandler == null ) { tracingHandler = AccessController.doPrivileged( new PrivilegedAction<StreamHandler>( ) { public StreamHandler run( ) { StreamHandler handler = new StreamHandler( System.out, new SimpleFormatter( ) ); try { tracingHandler.setLevel( Level.ALL ); } catch ( SecurityException e ) { e.printStackTrace( ); } return handler; } } ); } return tracingHandler; }
@Override protected Handler getLogHandler(final URI logFile) throws IOException { if (logFile == null) { throw new NullPointerException("The log file is null"); } final Path loggerPath = new Path(logFile); final FileSystem loggerFs = loggerPath.getFileSystem(this.conf); final Path parentPath = loggerPath.getParent(); // Create parent directory if necessary if (!loggerFs.exists(loggerPath.getParent())) { if (!loggerFs.mkdirs(loggerPath.getParent())) { throw new IOException("Unable to create directory " + parentPath + " for log file:" + logFile); } } return new StreamHandler(loggerFs.create(loggerPath), Globals.LOG_FORMATTER); }