@Override
 protected void before() throws Throwable {
   threadContextHolder = new ThreadContextHolder(restoreMap, restoreStack);
   if (restoreMap) {
     ThreadContext.clearMap();
   }
   if (restoreStack) {
     ThreadContext.clearStack();
   }
 }
Пример #2
0
  @Test
  public void testThreadContext() {
    final Logger logger = LogManager.getLogger("test");

    ThreadContext.push("Message only");
    ThreadContext.push("int", 1);
    ThreadContext.push("int-long-string", 1, 2l, "3");
    ThreadContext.put("key", "value");

    logger.info("Hello World");

    ThreadContext.clearAll();
  }
 @After
 public void zeroOutThreadContext() {
   waitAQuarterSecond();
   logger.info("Emptying ThreadContext");
   ThreadContext.pop();
   waitAQuarterSecond();
   logger.info("Done Emptying ThreadContext\n\n");
 }
 @Before
 public void recordTestName() {
   waitAQuarterSecond();
   logger.info("Recording TestName, @Before method");
   waitAQuarterSecond();
   ThreadContext.push(testName.getMethodName());
   logger.info("TestName:  " + testName);
   waitAQuarterSecond();
   logger.info("Done Recording TestName\n\n");
 }
Пример #5
0
  private void initLogMessageInfo(
      Info info,
      final String fqcn,
      final Level level,
      final Marker marker,
      final Message message,
      final Throwable thrown) {
    info.translator.setValues(
        this,
        getName(),
        marker,
        fqcn,
        level,
        message, //
        // don't construct ThrowableProxy until required
        thrown, //

        // config properties are taken care of in the EventHandler
        // thread in the #actualAsyncLog method

        // needs shallow copy to be fast (LOG4J2-154)
        ThreadContext.getImmutableContext(), //

        // needs shallow copy to be fast (LOG4J2-154)
        ThreadContext.getImmutableStack(), //

        // Thread.currentThread().getName(), //
        // info.cachedThreadName, //
        info.threadName(), //

        // location: very expensive operation. LOG4J2-153:
        // Only include if "includeLocation=true" is specified,
        // exclude if not specified or if "false" was specified.
        calcLocationIfRequested(fqcn),

        // System.currentTimeMillis());
        // CoarseCachedClock: 20% faster than system clock, 16ms gaps
        // CachedClock: 10% faster than system clock, smaller gaps
        // LOG4J2-744 avoid calling clock altogether if message has the timestamp
        eventTimeMillis(message), //
        nanoClock.nanoTime() //
        );
  }
  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws IOException, ServletException {
    boolean clear = false;
    if (!ThreadContext.containsKey("id")) {
      clear = true;
      ThreadContext.put("id", UUID.randomUUID().toString());
      HttpSession session = ((HttpServletRequest) request).getSession(false);
      if (session != null) {
        ThreadContext.put("username", (String) session.getAttribute("username"));
      }
    }

    try {
      chain.doFilter(request, response);
    } finally {
      if (clear) {
        ThreadContext.clearAll();
      }
    }
  }
  @Test
  public void testConvert02() {
    ThreadContext.clearStack();
    final ThreadContext.ContextStack stack =
        new MutableThreadContextStack(Arrays.asList("key1", "value2", "my3"));

    final String converted = this.converter.convertToDatabaseColumn(stack);

    assertNotNull("The converted value should not be null.", converted);

    final ThreadContext.ContextStack reversed = this.converter.convertToEntityAttribute(converted);

    assertNotNull("The reversed value should not be null.", reversed);
    assertEquals("The reversed value is not correct.", stack.asList(), reversed.asList());
  }
 @Override
 @SuppressWarnings("unchecked")
 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
   if (matcher.match(msg)) {
     final I typedMsg = (I) msg;
     try {
       if (quotaMeter.getOneMinuteRate() > USER_QUOTA_LIMIT) {
         sendErrorResponseIfTicked(typedMsg.id);
         return;
       }
       quotaMeter.mark();
       messageReceived(ctx, handlerState, typedMsg);
     } catch (Exception e) {
       handleGeneralException(ctx, e);
     } finally {
       ThreadContext.clearMap();
       ReferenceCountUtil.release(msg);
     }
   }
 }