本文整理匯總了Java中org.apache.log4j.NDC.push方法的典型用法代碼示例。如果您正苦於以下問題:Java NDC.push方法的具體用法?Java NDC.push怎麽用?Java NDC.push使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.log4j.NDC
的用法示例。
在下文中一共展示了NDC.push方法的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: 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;
}
示例3: 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();
}
}
示例4: 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());
}
示例5: 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"));
}
}
示例6: beforeRequest
import org.apache.log4j.NDC; //導入方法依賴的package包/類
/**
* Logs the before-request message through Log4J and
* adds a message the Log4J NDC before the request is processed.
*/
@Override
protected void beforeRequest(HttpServletRequest request, String message) {
if (log4jLogger.isDebugEnabled()) {
log4jLogger.debug(message);
}
NDC.push(getNestedDiagnosticContextMessage(request));
}
示例7: uncaughtException
import org.apache.log4j.NDC; //導入方法依賴的package包/類
@Override
public void uncaughtException(Thread t, Throwable e) {
String className = e.getClass().getCanonicalName() + "\n";
StringBuilder stackTrace = new StringBuilder(className);
for (StackTraceElement el : e.getStackTrace()) {
stackTrace.append("at ").append(el).append('\n');
}
String msg = "";
StringBuilder sb = new StringBuilder();
File logFile;
String logName = Log.getCurrentLogFile();
if (logName != null && (logFile = new File(logName)).canRead()) {
Log.error("Runtime exception", e);
try (BufferedReader input = Files.newBufferedReader(logFile.toPath(), StandardCharsets.UTF_8)) {
String line;
while ((line = input.readLine()) != null) {
sb.append(line).append('\n');
}
NDC.push(sb.toString());
} catch (IOException ex) {
ex.printStackTrace();
}
} else {
System.err.println("Runtime exception");
System.err.println(stackTrace);
msg += "Uncaught Exception in " + JHVGlobals.userAgent;
msg += "\nDate: " + new Date();
msg += "\nThread: " + t;
msg += "\nMessage: " + e.getMessage();
msg += "\nStacktrace:\n";
msg += stackTrace + "\n";
}
Log.fatal(null, e);
showErrorDialog(msg + "Log:\n" + sb);
}
示例8: main
import org.apache.log4j.NDC; //導入方法依賴的package包/類
public static void main(String[] args) {
BasicConfigurator.configure();
NDC.push("Client #45890");
logger.info("Awake awake. Put on thy strength.");
Trivial.foo();
InnerTrivial.foo();
logger.info("Exiting Trivial.");
}
示例9: main
import org.apache.log4j.NDC; //導入方法依賴的package包/類
public static void main(String[] args) throws IOException {
// Configure the LF5Appender using the DefaultLF5Configurator. This
// will add the LF5Appender to the root of the Category tree.
DefaultLF5Configurator.configure();
// Add an NDC to demonstrate how NDC information is output.
NDC.push("#23856");
// Log some information.
for (int i = 0; i < 10; i++) {
logger.debug("Hello, my name is Homer Simpson.");
logger.info("Mmmmmm .... Chocolate.");
logger.warn("Mmm...forbidden donut.");
}
// Clean up NDC
NDC.pop();
NDC.remove();
NDC.push("Another NDC");
// Log some information.
logger.fatal("Hello, my name is Bart Simpson.");
logger.error("Hi diddly ho good neighbour.");
// Clean up NDC
NDC.pop();
NDC.remove();
// Call methods on both classes.
InitUsingDefaultConfigurator.foo();
InnerInitUsingDefaultConfigurator.foo();
logger.info("Exiting InitUsingDefaultConfigurator.");
}
示例10: foo
import org.apache.log4j.NDC; //導入方法依賴的package包/類
public static void foo() {
logger.debug("Entered foo in InitUsingDefaultConfigurator class");
NDC.push("#123456");
logger.debug("Hello, my name is Marge Simpson.");
logger.info("D'oh!! A deer! A female deer.");
// Clean up NDC
NDC.pop();
NDC.remove();
}
示例11: testSerializationNDC
import org.apache.log4j.NDC; //導入方法依賴的package包/類
/**
* Serialize a logging event with ndc.
* @throws Exception if exception during test.
*
*/
public void testSerializationNDC() throws Exception {
Logger root = Logger.getRootLogger();
NDC.push("ndc test");
LoggingEvent event =
new LoggingEvent(
root.getClass().getName(), root, Level.INFO, "Hello, world.", null);
// event.prepareForDeferredProcessing();
int[] skip = new int[] { 352, 353, 354, 355, 356 };
SerializationTestHelper.assertSerializationEquals(
"witness/serialization/ndc.bin", event, skip, 237);
}
示例12: test6
import org.apache.log4j.NDC; //導入方法依賴的package包/類
/**
* The pattern on the server side: %5p %x %X{hostID}${key6} [%t] %c{1} - %m%n
*
* This test checks whether client-side MDC overrides the server side.
* It uses an AsyncAppender encapsulating a SocketAppender
*/
public void test6() throws Exception {
socketAppender = new SocketAppender("localhost", PORT);
socketAppender.setLocationInfo(true);
AsyncAppender asyncAppender = new AsyncAppender();
asyncAppender.setLocationInfo(true);
asyncAppender.addAppender(socketAppender);
rootLogger.addAppender(asyncAppender);
NDC.push("some6");
MDC.put("hostID", "client-test6");
common("T6", "key6", "MDC-TEST6");
NDC.pop();
MDC.remove("hostID");
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[]{PAT6, EXCEPTION1,
EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
Transformer.transform(
TEMP, FILTERED,
new Filter[] { cf, new LineNumberFilter(),
new JunitTestRunnerFilter(),
new SunReflectFilter() });
assertTrue(Compare.compare(FILTERED, "witness/socketServer.6"));
}
}
示例13: test7
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 client-side MDC overrides the server side.
*/
public void test7() throws Exception {
socketAppender = new SocketAppender("localhost", PORT);
socketAppender.setLocationInfo(true);
rootLogger.addAppender(socketAppender);
NDC.push("some7");
MDC.put("hostID", "client-test7");
common("T7", "key7", "MDC-TEST7");
NDC.pop();
MDC.remove("hostID");
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[]{PAT7, EXCEPTION1,
EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
Transformer.transform(
TEMP, FILTERED,
new Filter[] { cf, new LineNumberFilter(),
new JunitTestRunnerFilter(),
new SunReflectFilter() });
assertTrue(Compare.compare(FILTERED, "witness/socketServer.7"));
}
}
示例14: common
import org.apache.log4j.NDC; //導入方法依賴的package包/類
static
void common(String dc, String key, Object o) {
String oldThreadName = Thread.currentThread().getName();
Thread.currentThread().setName("main");
int i = -1;
NDC.push(dc);
MDC.put(key, o);
Logger root = Logger.getRootLogger();
logger.setLevel(Level.DEBUG);
rootLogger.setLevel(Level.DEBUG);
logger.log(XLevel.TRACE, "Message " + ++i);
logger.setLevel(Level.TRACE);
rootLogger.setLevel(Level.TRACE);
logger.trace("Message " + ++i);
root.trace("Message " + ++i);
logger.debug("Message " + ++i);
root.debug("Message " + ++i);
logger.info("Message " + ++i);
logger.warn("Message " + ++i);
logger.log(XLevel.LETHAL, "Message " + ++i); //5
Exception e = new Exception("Just testing");
logger.debug("Message " + ++i, e);
root.error("Message " + ++i, e);
NDC.pop();
MDC.remove(key);
Thread.currentThread().setName(oldThreadName);
}
示例15: setUp
import org.apache.log4j.NDC; //導入方法依賴的package包/類
/**
* Ensures that the in-vm broker is created and initialized.
*
* @throws Exception Any exceptions allowed to fall through and fail the test.
*/
protected void setUp() throws Exception
{
super.setUp();
NDC.push(getName());
testProps = TestContextProperties.getInstance(MessagingTestConfigProperties.defaults);
}