本文整理匯總了Java中java.util.logging.StreamHandler類的典型用法代碼示例。如果您正苦於以下問題:Java StreamHandler類的具體用法?Java StreamHandler怎麽用?Java StreamHandler使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
StreamHandler類屬於java.util.logging包,在下文中一共展示了StreamHandler類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createLogger
import java.util.logging.StreamHandler; //導入依賴的package包/類
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;
}
示例2: testOwnFormatter
import java.util.logging.StreamHandler; //導入依賴的package包/類
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);
}
示例3: testLogger
import java.util.logging.StreamHandler; //導入依賴的package包/類
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();
}
示例4: testConstructor_NoParameter_NoProperties
import java.util.logging.StreamHandler; //導入依賴的package包/類
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());
}
示例5: testConstructor_NoParameter_ValidProperties
import java.util.logging.StreamHandler; //導入依賴的package包/類
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());
}
示例6: testConstructor_NoParameter_InvalidProperties
import java.util.logging.StreamHandler; //導入依賴的package包/類
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());
}
示例7: testConstructor_HasParameters_NoProperties
import java.util.logging.StreamHandler; //導入依賴的package包/類
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());
}
示例8: testConstructor_HasParameters_ValidProperties
import java.util.logging.StreamHandler; //導入依賴的package包/類
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());
}
示例9: testConstructor_HasParameters_InvalidProperties
import java.util.logging.StreamHandler; //導入依賴的package包/類
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());
}
示例10: testConstructor_HasParameters_ValidPropertiesNullStream
import java.util.logging.StreamHandler; //導入依賴的package包/類
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
}
}
示例11: testConstructor_HasParameters_ValidPropertiesNullFormatter
import java.util.logging.StreamHandler; //導入依賴的package包/類
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
}
}
示例12: testIsLoggable_NoFilter
import java.util.logging.StreamHandler; //導入依賴的package包/類
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));
}
示例13: testIsLoggable_WithFilter
import java.util.logging.StreamHandler; //導入依賴的package包/類
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());
}
示例14: testPublish_AfterClose
import java.util.logging.StreamHandler; //導入依賴的package包/類
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());
}
示例15: initializeLogger
import java.util.logging.StreamHandler; //導入依賴的package包/類
/**
* 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;
}