Java 8 StreamHandler - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 StreamHandler class.

This code example shows how to use the following methods:getLevel, setLevel, close, publish, flush
 
		// start the server to be ready to accept log messages 
		ServerThread thread = new ServerThread();
		thread.start();
		Thread.sleep(2000);
		h = new SocketHandler();
		h.setLevel(Level.FINE);
 
		assertSame(h.getLevel(), Level.FINE);
		LogRecord r = new LogRecord(Level.INFO, "testPublish_NoFormatter");
		assertTrue(h.isLoggable(r));
		h.close();
		// ensure the thread exits and the port becomes available again 
		thread.getReadString();
		// assertFalse(h.isLoggable(r)); 
		h.publish(r);
		h.flush();
		// assertEquals("MockFormatter_Head", 
		// this.errSubstituteStream.toString()); 
	} 

6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:setLevel
    public ByteArrayOutputStream baos;
    public StreamHandler streamHandler;
 
    public StubLogger() { 
        baos = new ByteArrayOutputStream(10000);
        streamHandler = new StreamHandler(baos, new SimpleFormatter());
        streamHandler.setLevel(Level.ALL);
        Logger logger = Logger.getLogger("");
        logger.addHandler(streamHandler);
    } 
 
    /** 
     * Returns any new java.util.logging messages since this method was last called,  
     * or since this object was constructed if this method has not yet been called. 
     */ 
    public String getNewLogMessages() {
        streamHandler.flush();
        String text = baos.toString();
        baos.reset();
        return text;
6
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:setFormatter, setEncoding
				pattern = pattern + ".%g"; // Esempio: "c:/dir/nome%g.log";
 
				int limit = Integer.parseInt((String) this._systemParams.get(INIT_PROP_LOG_FILE_SIZE));
				int count = Integer.parseInt((String) this._systemParams.get(INIT_PROP_LOG_FILES_COUNT));
 
				FileHandler handler = new FileHandler(pattern, limit, count);
				handler.setFormatter(new SimpleFormatter());
				handler.setEncoding("UTF-8");
				_logger.addHandler(handler);
			} 
			String levelString = (String) this._systemParams.get(INIT_PROP_LOG_LEVEL);
			logLevel = Level.parse(levelString);
			_logger.setLevel(logLevel);
		} catch (Exception e) {
			try { 
				_logger = Logger.getLogger(this.getClass().getName());
				_logger.log(Level.SEVERE, "Error detected while creating the logger: ", e);
			} catch (RuntimeException e1) {
			} 
		} 
5
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:publish, setLevel, flush
		Properties p = new Properties();
		p.put("java.util.logging.ConsoleHandler.formatter", className
				+ "$MockFormatter"); 
		LogManager.getLogManager().readConfiguration(
				EnvironmentHelper.PropertiesToInputStream(p));
		ConsoleHandler h = new ConsoleHandler();
 
		LogRecord r = new LogRecord(Level.INFO, "testPublish_NoFilter");
		h.setLevel(Level.INFO);
		h.publish(r);
		h.flush();
		assertEquals("MockFormatter_Head" + "testPublish_NoFilter",
				this.errSubstituteStream.toString());
 
		h.setLevel(Level.WARNING);
		h.publish(r);
		h.flush();
		assertEquals("MockFormatter_Head" + "testPublish_NoFilter",
				this.errSubstituteStream.toString());
 

5
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:isLoggable
 
	/* 
	 * Test isLoggable(), null log record, without output stream 
	 */ 
	public void testIsLoggable_Null_NoOutputStream() { 
		StreamHandler h = new StreamHandler();
		assertFalse(h.isLoggable(null));
	} 
 
	/* 
	 * Test publish(), use no filter, having output stream, normal log record. 
	 */ 
	public void testPublish_NoOutputStream() { 
		StreamHandler h = new StreamHandler();
		LogRecord r = new LogRecord(Level.INFO, "testPublish_NoOutputStream");
		h.publish(r);
 
		h.setLevel(Level.WARNING);
		h.publish(r);
 
3
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:setLevel
    public ByteArrayOutputStream baos;
    public StreamHandler streamHandler;
 
    public StubLogger() { 
        baos = new ByteArrayOutputStream(10000);
        streamHandler = new StreamHandler(baos, new SimpleFormatter());
        streamHandler.setLevel(Level.ALL);
        Logger logger = Logger.getLogger("");
        logger.addHandler(streamHandler);
    } 
 
    /** 
     * Returns any new java.util.logging messages since this method was last called,  
     * or since this object was constructed if this method has not yet been called. 
     */ 
    public String getNewLogMessages() {
        streamHandler.flush();
        String text = baos.toString();
        baos.reset();
        return text;
5
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:setFormatter, setEncoding
        } 
    } 
 
    private static FileHandler createFileHandler(String logDirectory, String fileName) {
        try { 
            FileHandler f = new FileHandler(logDirectory + File.separator + fileName, LOG_SIZE, LOG_FILE_COUNT);
            f.setEncoding(LOG_ENCODING);
            f.setFormatter(new SimpleFormatter());
            return f;
        } catch (IOException e) {
            throw new RuntimeException("Error initializing " + fileName + " file handler", e); //NON-NLS
        } 
    } 
 
    /** 
     * Factory method to retrieve a org.sleuthkit.autopsy.coreutils.Logger 
     * instance derived from java.util.logging.Logger. Hides the base class 
     * factory method. 
     * 
     * @param name A name for the logger. This should be a dot-separated name 
4
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:
import org.junit.BeforeClass; 
import org.junit.Test; 
 
public class LogTest { 
	protected static ByteArrayOutputStream _baos = new ByteArrayOutputStream();
	protected static StreamHandler _sh = new StreamHandler(_baos, new SimpleFormatter());
	protected static Level[] prevLevels;
	protected int _lastStart;
	protected int _lastFinish;
	protected static Object _testLock = new Object(); 
 
	@BeforeClass 
	public static void setUpBeforeClass() throws Exception {
		prevLevels = Log.getLevels();
		String app = Log.getApplicationClass();
		Logger logger = Logger.getLogger(app);
		logger.addHandler(_sh);
	} 
 
	@AfterClass 
5
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:flush
	 * Write to the log and return number of bytes that show up 
	 * in the log 
	 */ 
	protected int writeLog(int facility, Level level, String msg) {
		int start, finish;
		_sh.flush();
		start = _baos.toByteArray().length;
		Log.log(facility, level, msg);
		_sh.flush();
		finish = _baos.toByteArray().length;
		return finish - start;
	} 
	 
	protected void doTest(int facility, Level level) {
		 
		// set level off and make sure no logging 
		Log.setLevel(facility, Level.OFF);
		Assert.assertEquals(0, writeLog(facility, level, "test me off"));
 
		// set to ALL and make sure it does log 
4
CodeRank
Experience pair programming with AI  Get Codota for Java
This code example shows how to use the following methods:setFormatter
 
	public static void main(String[] args) {
		 
		Logger logger = Logger.getLogger(SingleLineFormatter.class.getPackage().getName());
		 
  	Handler handler = new StreamHandler();
  	Formatter formatter = new SingleLineFormatter();
  	handler.setFormatter(formatter);
  	logger.addHandler(handler);
 
		// Log a few message at different severity levels 
		logger.severe("severe message");
		logger.warning("warning message");
		logger.info("info message");
		logger.config("config message");
		logger.fine("fine message");
		logger.finer("finer message");
		logger.finest("finest message");
	} 
} 
5
CodeRank
Experience pair programming with AI  Get Codota for Java
See Code Examples for Java 8 StreamHandler Methods: