本文整理匯總了Java中org.apache.log4j.NDC類的典型用法代碼示例。如果您正苦於以下問題:Java NDC類的具體用法?Java NDC怎麽用?Java NDC使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
NDC類屬於org.apache.log4j包,在下文中一共展示了NDC類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testNDC
import org.apache.log4j.NDC; //導入依賴的package包/類
@Test
public void testNDC() throws Exception
{
initialize("TestJsonLayout/default.properties");
NDC.push("frist"); // misspelling intentional
NDC.push("second");
logger.debug(TEST_MESSAGE);
NDC.clear();
captureLoggingOutput();
assertCommonElements(TEST_MESSAGE);
DomAsserts.assertEquals("ndc", "frist second", dom, "/data/ndc");
}
示例2: run
import org.apache.log4j.NDC; //導入依賴的package包/類
@Override
public final void run()
{
try
{
NDC.inherit(loggingContext);
CurrentUser.setUserState(callingThreadsAuthentication);
CurrentInstitution.set(callingThreadsInstitution);
doRun();
}
finally
{
CurrentInstitution.remove();
CurrentUser.setUserState(null);
NDC.remove();
}
}
示例3: ndcPush
import org.apache.log4j.NDC; //導入依賴的package包/類
private int ndcPush() {
int count = 0;
try {
UserContext user = getUser();
if (user != null) {
NDC.push("uid:" + user.getTrueExternalUserId()); count++;
if (user.getCurrentAuthority() != null) {
NDC.push("role:" + user.getCurrentAuthority().getRole()); count++;
Long sessionId = user.getCurrentAcademicSessionId();
if (sessionId != null) {
NDC.push("sid:" + sessionId); count++;
}
}
}
} catch (Exception e) {}
return count;
}
示例4: bubbleSort
import org.apache.log4j.NDC; //導入依賴的package包/類
void bubbleSort() {
LOG.info( "Entered the sort method.");
for(int i = intArray.length -1; i >= 0 ; i--) {
NDC.push("i=" + i);
OUTER.debug("in outer loop.");
for(int j = 0; j < i; j++) {
NDC.push("j=" + j);
// It is poor practice to ship code with log staments in tight loops.
// We do it anyway in this example.
INNER.debug( "in inner loop.");
if(intArray[j] > intArray[j+1])
swap(j, j+1);
NDC.pop();
}
NDC.pop();
}
}
示例5: testNDCWithCDATA
import org.apache.log4j.NDC; //導入依賴的package包/類
/**
* Tests CDATA element within NDC content. See bug 37560.
*/
public void testNDCWithCDATA() throws Exception {
Logger logger = Logger.getLogger("com.example.bar");
Level level = Level.INFO;
String ndcMessage ="<envelope><faultstring><![CDATA[The EffectiveDate]]></faultstring><envelope>";
NDC.push(ndcMessage);
LoggingEvent event =
new LoggingEvent(
"com.example.bar", logger, level, "Hello, World", null);
Layout layout = createLayout();
String result = layout.format(event);
NDC.clear();
Element parsedResult = parse(result);
NodeList ndcs = parsedResult.getElementsByTagName("log4j:NDC");
assertEquals(1, ndcs.getLength());
StringBuffer buf = new StringBuffer();
for(Node child = ndcs.item(0).getFirstChild();
child != null;
child = child.getNextSibling()) {
buf.append(child.getNodeValue());
}
assertEquals(ndcMessage, buf.toString());
}
示例6: test4
import org.apache.log4j.NDC; //導入依賴的package包/類
/**
* The pattern on the server side: %5p %x %X{key1}%X{key4} [%t] %c{1} - %m%n
* meaning that we are testing NDC, MDC and localization functionality across
* the wire.
*/
public void test4() throws Exception {
socketAppender = new SocketAppender("localhost", PORT);
socketAppender.setLocationInfo(true);
rootLogger.addAppender(socketAppender);
NDC.push("some");
common("T4", "key4", "MDC-TEST4");
NDC.pop();
delay(1);
//
// These tests check MDC operation which
// requires JDK 1.2 or later
if(!System.getProperty("java.version").startsWith("1.1.")) {
ControlFilter cf = new ControlFilter(new String[]{PAT4, EXCEPTION1,
EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
Transformer.transform(
TEMP, FILTERED,
new Filter[] { cf, new LineNumberFilter(),
new JunitTestRunnerFilter(),
new SunReflectFilter() });
assertTrue(Compare.compare(FILTERED, "witness/socketServer.4"));
}
}
示例7: test8
import org.apache.log4j.NDC; //導入依賴的package包/類
/**
* The pattern on the server side: %5p %x %X{hostID}${key7} [%t] %c{1} - %m%n
*
* This test checks whether server side MDC works.
*/
public void test8() throws Exception {
socketAppender = new SocketAppender("localhost", PORT);
socketAppender.setLocationInfo(true);
rootLogger.addAppender(socketAppender);
NDC.push("some8");
common("T8", "key8", "MDC-TEST8");
NDC.pop();
delay(2);
//
// These tests check MDC operation which
// requires JDK 1.2 or later
if(!System.getProperty("java.version").startsWith("1.1.")) {
ControlFilter cf = new ControlFilter(new String[]{PAT8, EXCEPTION1,
EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
Transformer.transform(
TEMP, FILTERED,
new Filter[] { cf, new LineNumberFilter(),
new JunitTestRunnerFilter(),
new SunReflectFilter() });
assertTrue(Compare.compare(FILTERED, "witness/socketServer.8"));
}
}
示例8: service
import org.apache.log4j.NDC; //導入依賴的package包/類
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
count_++;
String ndcName = getClass().getName();
ndcName = ndcName.substring(ndcName.lastIndexOf('.')+1);
NDC.push(ndcName);
NDC.push("call-" + count_);
logger_.info("begin onService");
try
{
onService(req, resp);
}
catch (Exception exc)
{
lastException = exc;
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
finally
{
logger_.info("end onService");
NDC.pop();
NDC.pop();
}
}
示例9: run
import org.apache.log4j.NDC; //導入依賴的package包/類
public void run() {
NDC.push("main");
try {
log.info("starting up daemon");
isStarted = true;
//noinspection InfiniteLoopStatement
while (!ss.isClosed()) {
try {
final Socket socket = ss.accept();
socket.setSoTimeout(60000);
socket.setTcpNoDelay(true);
log.info("received connection, running");
executor.execute(new DaemonWorker(socket));
} catch (IOException e) {
log.warn("server socket error", e);
}
}
} finally {
NDC.pop();
}
}
示例10: verifyUser
import org.apache.log4j.NDC; //導入依賴的package包/類
public UserDomainObject verifyUser(String login, String password) {
NDC.push("verifyUser");
try {
UserDomainObject result = null;
boolean userAuthenticates = externalizedImcmsAuthAndMapper.authenticate(login, password);
UserDomainObject user = externalizedImcmsAuthAndMapper.getUser(login);
if (null == user) {
mainLog.info("->User '" + login + "' failed to log in: User not found.");
} else if (!user.isActive()) {
logUserDeactivated(user);
} else if (!userAuthenticates) {
mainLog.info("->User '" + login + "' failed to log in: Wrong password.");
} else {
result = user;
logUserLoggedIn(user);
}
return result;
} finally {
NDC.pop();
}
}
示例11: isDateOnly
import org.apache.log4j.NDC; //導入依賴的package包/類
/**
* @return {@code true} if the object contains just the date, false otherwise.
*/
private boolean isDateOnly(EventDateTime source) {
NDC.push("isDateOnly");
try {
if (source.getDate() != null && source.getDateTime() == null) {
return true;
}
if (source.getDate() == null && source.getDateTime() != null) {
return false;
}
logger.error("source: " + source);
logger.error("date: " + source.getDate());
logger.error("time: " + source.getDateTime());
throw new IllegalArgumentException("API must have changed, both Date and DateTime are returned, need to revise the code");
} finally {
NDC.pop();
}
}
示例12: networkFault
import org.apache.log4j.NDC; //導入依賴的package包/類
@Override
public void networkFault(OneWireNetworkEvent e, String message) {
NDC.push("networkFault");
try {
// This is an event pertinent to everyone
consume(new DataSample<Double>(
System.currentTimeMillis(), type + getAddress(), type + getAddress(),
null, new OneWireIOException(message)));
} finally {
NDC.pop();
}
}
示例13: check
import org.apache.log4j.NDC; //導入依賴的package包/類
/**
* Make sure the signal given to {@link #consume(DataSample)} is sane.
*
* @param signal Signal to check.
*/
private void check(DataSample<Double> signal) {
NDC.push("check");
try {
if (signal == null) {
throw new IllegalArgumentException("signal can't be null");
}
if (signal.isError()) {
logger.error("Should not have propagated all the way here", signal.error);
throw new IllegalArgumentException("Error signal should have been handled by zone controller");
}
if (signal.sample < 0.0) {
throw new IllegalArgumentException("Signal must be non-negative, received " + signal.sample);
}
} finally {
NDC.pop();
}
}
示例14: consume
import org.apache.log4j.NDC; //導入依賴的package包/類
/**
* {@inheritDoc}
*/
public void consume(DataSample<ProcessControllerStatus> signal) {
// Let them consume the common process controller signal components
super.consume(signal);
// And now let's take care of PID specific components
NDC.push("consume.pid");
try {
// This is a bit dangerous, but let's see how exactly dangerous
PidControllerStatus pidStatus = (PidControllerStatus) signal.sample;
long timestamp = signal.timestamp;
String sourceName = signal.sourceName;
consume(timestamp, sourceName + ".p", pidStatus.p);
consume(timestamp, sourceName + ".i", pidStatus.i);
consume(timestamp, sourceName + ".d", pidStatus.d);
} finally {
NDC.pop();
}
}
示例15: loadSampleConfiguration
import org.apache.log4j.NDC; //導入依賴的package包/類
/**
* Load the configuration already present in the jar file.
*
* With some luck, all objects instantiated here will be garbage collected
* pretty soon and won't matter.
*
* If this bothers you, remove the method invocation and recompile.
*
* @see #CF_EMBEDDED
*/
private void loadSampleConfiguration() {
NDC.push("loadSampleConfiguration");
try {
// We don't need it for anything other than loading a sample,
// hence local scope.
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] { CF_EMBEDDED });
// This class is the root of the instantiation hierarchy -
// if it can be instantiated, then everything else is fine
@SuppressWarnings("unused")
DamperController dc = (DamperController) applicationContext.getBean("damper_controller-sample1");
} catch (NoSuchBeanDefinitionException ex) {
logger.debug("Oh, they found and deleted the sample configuration! Good.");
} finally {
NDC.pop();
}
}