下面列出了java.util.logging.Handler# publish ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testLogNoParamsWithThrowable() {
Logger log = LogUtils.getL7dLogger(LogUtilsTest.class, null, "testLogNoParamsWithThrowable");
Handler handler = EasyMock.createNiceMock(Handler.class);
Exception ex = new Exception("x");
LogRecord record = new LogRecord(Level.SEVERE, "subbed in {0} only");
record.setThrown(ex);
EasyMock.reportMatcher(new LogRecordMatcher(record));
handler.publish(record);
EasyMock.replay(handler);
synchronized (log) {
log.addHandler(handler);
// handler called *after* localization of message
LogUtils.log(log, Level.SEVERE, "SUB1_MSG", ex);
EasyMock.verify(handler);
log.removeHandler(handler);
}
}
@Test
public void testLogParamSubstitutionWithThrowable() throws Exception {
Logger log = LogUtils.getL7dLogger(LogUtilsTest.class, null, "testLogParamSubstitutionWithThrowable");
Handler handler = EasyMock.createNiceMock(Handler.class);
Exception ex = new Exception();
LogRecord record = new LogRecord(Level.SEVERE, "subbed in 1 only");
record.setThrown(ex);
EasyMock.reportMatcher(new LogRecordMatcher(record));
handler.publish(record);
EasyMock.replay(handler);
synchronized (log) {
log.addHandler(handler);
LogUtils.log(log, Level.SEVERE, "SUB1_MSG", ex, 1);
EasyMock.verify(handler);
log.removeHandler(handler);
}
}
@Test
public void testLogParamsSubstitutionWithThrowable() throws Exception {
Logger log = LogUtils.getL7dLogger(LogUtilsTest.class, null,
"testLogParamsSubstitutionWithThrowable");
Handler handler = EasyMock.createNiceMock(Handler.class);
Exception ex = new Exception();
LogRecord record = new LogRecord(Level.SEVERE, "subbed in 4 & 3");
record.setThrown(ex);
EasyMock.reportMatcher(new LogRecordMatcher(record));
handler.publish(record);
EasyMock.replay(handler);
synchronized (log) {
log.addHandler(handler);
LogUtils.log(log, Level.SEVERE, "SUB2_MSG", ex, new Object[] {3, 4});
EasyMock.verify(handler);
log.removeHandler(handler);
}
}
private static void publish(Logger logger, LogRecord record) {
// Annoyingly this method appears to copy the array every time it is called, but there's
// nothing much we can do about this (and there could be synchronization issues even if we
// could access things directly because handlers can be changed at any time). Most of the
// time this returns the singleton empty array however, so it's not as bad as all that.
for (Handler handler : logger.getHandlers()) {
handler.publish(record);
}
if (logger.getUseParentHandlers()) {
logger = logger.getParent();
if (logger != null) {
publish(logger, record);
}
}
}
/**
* Publishes the record.
*/
public void publish(LogRecord record)
{
if (! isLoggable(record))
return;
int level = getLevel().intValue();
for (int i = 0; i < _handlers.length; i++) {
Handler handler = _handlers[i];
if (level <= handler.getLevel().intValue())
handler.publish(record);
}
}
@Test
public void testHandleL7dMessage() throws Exception {
Logger log = LogUtils.getL7dLogger(LogUtilsTest.class, null, "testHandleL7dMessage");
Handler handler = EasyMock.createNiceMock(Handler.class);
log.addHandler(handler);
// handler called *before* localization of message
LogRecord record = new LogRecord(Level.WARNING, "FOOBAR_MSG");
record.setResourceBundle(log.getResourceBundle());
EasyMock.reportMatcher(new LogRecordMatcher(record));
handler.publish(record);
EasyMock.replay(handler);
log.log(Level.WARNING, "FOOBAR_MSG");
EasyMock.verify(handler);
log.removeHandler(handler);
}
@Test
public void testLogNoParamsOrThrowable() {
Logger log = LogUtils.getL7dLogger(LogUtilsTest.class, null, "testLogNoParamsOrThrowable");
Handler handler = EasyMock.createNiceMock(Handler.class);
log.addHandler(handler);
// handler called *after* localization of message
LogRecord record = new LogRecord(Level.SEVERE, "subbed in {0} only");
EasyMock.reportMatcher(new LogRecordMatcher(record));
handler.publish(record);
EasyMock.replay(handler);
LogUtils.log(log, Level.SEVERE, "SUB1_MSG");
EasyMock.verify(handler);
log.removeHandler(handler);
}
@Override
public void publish(LogRecord record) {
for (Handler h : instances) {
h.publish(record);
}
}
@Override
public void publish(LogRecord record) {
for (Handler h : Lookup.getDefault().lookupAll(Handler.class)) {
h.publish(record);
}
}
@Override
protected void internalLogFormatted(String msg, LogRecord record) {
Level level = record.getLevel();
Throwable t = record.getThrown();
Handler[] targets = getHandlers();
if (targets != null) {
for (Handler h : targets) {
h.publish(record);
}
}
if (!getUseParentHandlers()) {
return;
}
/*
* As we can not use a "switch ... case" block but only a "if ... else if ..." block, the order of the
* comparisons is important. We first try log level FINE then INFO, WARN, FINER, etc
*/
if (Level.FINE.equals(level)) {
if (locationAwareLogger == null) {
logger.debug(msg, t);
} else {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, msg, null, t);
}
} else if (Level.INFO.equals(level)) {
if (locationAwareLogger == null) {
logger.info(msg, t);
} else {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.INFO_INT, msg, null, t);
}
} else if (Level.WARNING.equals(level)) {
if (locationAwareLogger == null) {
logger.warn(msg, t);
} else {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.WARN_INT, msg, null, t);
}
} else if (Level.FINER.equals(level)) {
if (locationAwareLogger == null) {
logger.trace(msg, t);
} else {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, msg, null, t);
}
} else if (Level.FINEST.equals(level)) {
if (locationAwareLogger == null) {
logger.trace(msg, t);
} else {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.TRACE_INT, msg, null, t);
}
} else if (Level.ALL.equals(level)) {
// should never occur, all is used to configure java.util.logging
// but not accessible by the API Logger.xxx() API
if (locationAwareLogger == null) {
logger.error(msg, t);
} else {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.ERROR_INT, msg, null, t);
}
} else if (Level.SEVERE.equals(level)) {
if (locationAwareLogger == null) {
logger.error(msg, t);
} else {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.ERROR_INT, msg, null, t);
}
} else if (Level.CONFIG.equals(level)) {
if (locationAwareLogger == null) {
logger.debug(msg, t);
} else {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, msg, null, t);
}
} else if (Level.OFF.equals(level)) {
// don't log
}
}
@Override
protected void internalLogFormatted(final String msg, final LogRecord record) {
final Level level = record.getLevel();
final Throwable t = record.getThrown();
final Handler[] targets = getHandlers();
if (targets != null) {
for (final Handler h : targets) {
h.publish(record);
}
}
if (!getUseParentHandlers()) {
return;
}
/*
* As we can not use a "switch ... case" block but only a "if ... else if ..." block, the order of the
* comparisons is important. We first try log level FINE then INFO, WARN, FINER, etc
*/
if (Level.FINE.equals(level)) {
if (locationAwareLogger == null) {
logger.debug(msg, t);
} else {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, msg, null, t);
}
} else if (Level.INFO.equals(level)) {
if (locationAwareLogger == null) {
logger.info(msg, t);
} else {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.INFO_INT, msg, null, t);
}
} else if (Level.WARNING.equals(level)) {
if (locationAwareLogger == null) {
logger.warn(msg, t);
} else {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.WARN_INT, msg, null, t);
}
} else if (Level.FINER.equals(level)) {
if (locationAwareLogger == null) {
logger.trace(msg, t);
} else {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, msg, null, t);
}
} else if (Level.FINEST.equals(level)) {
if (locationAwareLogger == null) {
logger.trace(msg, t);
} else {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.TRACE_INT, msg, null, t);
}
} else if (Level.ALL.equals(level)) {
// should never occur, all is used to configure java.util.logging
// but not accessible by the API Logger.xxx() API
if (locationAwareLogger == null) {
logger.error(msg, t);
} else {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.ERROR_INT, msg, null, t);
}
} else if (Level.SEVERE.equals(level)) {
if (locationAwareLogger == null) {
logger.error(msg, t);
} else {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.ERROR_INT, msg, null, t);
}
} else if (Level.CONFIG.equals(level)) {
if (locationAwareLogger == null) {
logger.debug(msg, t);
} else {
locationAwareLogger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, msg, null, t);
}
}
// don't log if Level.OFF
}
private static void publishAndFlush(Handler handler, LogRecord logRecord) {
handler.publish(logRecord);
handler.flush();
}