类org.slf4j.MDC源码实例Demo

下面列出了怎么用org.slf4j.MDC的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: light-4j   文件: Server.java
public static void init() {
    logger.info("server starts");
    // setup system property to redirect undertow logs to slf4j/logback.
    System.setProperty("org.jboss.logging.provider", "slf4j");

    try {

        loadConfigs();

        // this will make sure that all log statement will have serviceId
        MDC.put(SID, config.getServiceId());

        // merge status.yml and app-status.yml if app-status.yml is provided
        mergeStatusConfig();

        start();
    } catch (RuntimeException e) {
        // Handle any exception encountered during server start-up
        logger.error("Server is not operational! Failed with exception", e);
        System.out.println("Failed to start server:" + e.getMessage());
        // send a graceful system shutdown
        System.exit(1);
    }
}
 
public void queueReconciliation(final Reconciliation reconciliation, final Consumer<Reconciliation> runner) {
    log.info("Queueing reconciliation {}", reconciliation);
    executor.submit(reconciliation.getClusterName(), reconciliation.getType(), () -> {
        MDC.put("clusterName", reconciliation.getClusterName());
        MDC.put("namespace", reconciliation.getNamespace());
        MDC.put("resourceName", reconciliation.getResourceName());
        MDC.put("type", reconciliation.getType());

        try {
            runner.accept(reconciliation);
        } catch (final Throwable t) {
            log.error("There was an error during reconciliation that the reconciler didn't handle", t);
        } finally {
            MDC.remove("type");
            MDC.remove("resourceName");
            MDC.remove("namespace");
            MDC.remove("clusterName");
        }
    });

    log.info("Reconciliation {} successfully queued", reconciliation);
}
 
@Test
public void linkTracingAndMdcToCurrentThread_should_set_tracing_and_mdc_to_state_values_if_available() {
    // given
    Map<String, String> stateMdcInfo = new HashMap<>();
    stateMdcInfo.put("foo", "bar");
    Deque<Span> stateTraceStack = new LinkedList<>();
    Span span = Span.generateRootSpanForNewTrace("fooSpanName", LOCAL_ONLY).withTraceId("fooTraceId").build();
    stateTraceStack.add(span);
    state.setLoggerMdcContextMap(stateMdcInfo);
    state.setDistributedTraceStack(stateTraceStack);

    assertThat(MDC.getCopyOfContextMap().isEmpty(), is(true));
    assertThat(Tracer.getInstance().getCurrentSpan(), nullValue());

    // when
    handler.linkTracingAndMdcToCurrentThread(ctxMock);

    // then
    // Tracer adds some stuff to the MDC
    stateMdcInfo.put(SpanFieldForLoggerMdc.TRACE_ID.mdcKey, span.getTraceId());
    assertThat(MDC.getCopyOfContextMap(), is(stateMdcInfo));
    assertThat(Tracer.getInstance().getCurrentSpanStackCopy(), is(stateTraceStack));
}
 
源代码4 项目: arcusplatform   文件: BridgeMdcUtil.java
public static MdcContextReference captureAndInitializeContext(Session session) {
   MdcContextReference ref = MdcContext.captureMdcContext();
   if(session == null) {
      return ref;
   }
   
   String place = session.getActivePlace();
   String principal = session.getClient() != null ? session.getClient().getPrincipalName() : null;
   String type = session.getClientType();
   String version = session.getClientVersion();
   
   if(!StringUtils.isEmpty(place)) {
      MDC.put(MdcContext.MDC_PLACE, place);
   }
   if(!StringUtils.isEmpty(principal)) {
      MDC.put(MdcContext.MDC_TARGET, principal);
   }
   if(StringUtils.isEmpty(version)) {
      version = "unknown";
   }
   MDC.put(MdcContext.MDC_CLIENT_VERSION, type + " " + version);
   return ref;
}
 
@Override
public K call() throws Exception {
    LOG.debug("Call using MDCHystrixContextCallable...");
    Map childMDC = MDC.getCopyOfContextMap();
    LOG.debug("childMDC --> " + childMDC);
    try {
        if (parentMDC != null) {
            MDC.setContextMap(parentMDC);
        }
        LOG.debug("parentMDC --> " + parentMDC);
        return actual.call();
    } finally {
        if (childMDC != null) {
            MDC.setContextMap(childMDC);
        }
    }
}
 
源代码6 项目: emissary   文件: DirectoryAdapter.java
public boolean inboundAddPlaces(final HttpServletRequest req, final String dir) {
    if (disableAddPlaces) {
        return true;
    } else {
        final AddPlacesRequestBean bean = new AddPlacesRequestBean(req);
        final IRemoteDirectory directory = getLocalDirectory(dir);

        if (directory == null) {
            throw new IllegalArgumentException("No directory found using name " + dir);
        }

        MDC.put(MDCConstants.SERVICE_LOCATION, KeyManipulator.getServiceLocation(directory.getKey()));
        try {
            directory.irdAddPlaces(bean.getEntries(), bean.isPropagating());
        } finally {
            MDC.remove(MDCConstants.SERVICE_LOCATION);
        }
        return true;
    }
}
 
源代码7 项目: wingtips   文件: CallableWithTracingTest.java
@Before
public void beforeMethod() throws Exception {
    callableMock = mock(Callable.class);

    throwExceptionDuringCall = false;
    currentSpanStackWhenCallableWasCalled = new ArrayList<>();
    currentMdcInfoWhenCallableWasCalled = new ArrayList<>();
    doAnswer(new Answer() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            currentSpanStackWhenCallableWasCalled.add(Tracer.getInstance().getCurrentSpanStackCopy());
            currentMdcInfoWhenCallableWasCalled.add(MDC.getCopyOfContextMap());
            if (throwExceptionDuringCall)
                throw new RuntimeException("kaboom");
            return null;
        }
    }).when(callableMock).call();

    resetTracing();
}
 
源代码8 项目: cosmic   文件: CallContext.java
protected static CallContext register(final User callingUser, final Account callingAccount, final Long userId, final Long accountId, final String contextId) {
    /*
            Unit tests will have multiple times of setup/tear-down call to this, remove assertions to all unit test to run
            assert s_currentContext.get() == null : "There's a context already so what does this new register context mean? " + s_currentContext.get().toString();
            if (s_currentContext.get() != null) { // FIXME: This should be removed soon.  I added this check only to surface all the places that have this problem.
                throw new CloudRuntimeException("There's a context already so what does this new register context mean? " + s_currentContext.get().toString());
            }
    */
    CallContext callingContext = null;
    if (userId == null || accountId == null) {
        callingContext = new CallContext(callingUser, callingAccount, contextId);
    } else {
        callingContext = new CallContext(userId, accountId, contextId);
    }
    s_currentContext.set(callingContext);
    MDC.put("ctx", " (ctx: " + UuidUtils.first(contextId) + ")");
    if (s_logger.isTraceEnabled()) {
        s_logger.trace("Registered: " + callingContext);
    }

    s_currentContextStack.get().push(callingContext);

    return callingContext;
}
 
源代码9 项目: govpay   文件: AbstractTask.java
protected IContext initBatchContext(boolean setContext) throws UtilsException {
	GpContextFactory factory = new GpContextFactory();
	IContext ctx = factory.newBatchContext();
	MDC.put(MD5Constants.OPERATION_ID, this.name);
	MDC.put(MD5Constants.TRANSACTION_ID, ctx.getTransactionId());
	Service service = new Service();
	service.setName(CostantiTask.SERVICE_NAME_TASK);
	service.setType(GpContext.TIPO_SERVIZIO_GOVPAY_OPT);
	ctx.getApplicationContext().getTransaction().setService(service);
	Operation opt = new Operation();
	opt.setName(this.name);
	ctx.getApplicationContext().getTransaction().setOperation(opt);
	if(setContext)
		ContextThreadLocal.set(ctx);
	return ctx;
}
 
源代码10 项目: wingtips   文件: FunctionWithTracingTest.java
@DataProvider(value = {
    "true",
    "false"
})
@Test
public void current_thread_info_constructor_sets_fields_as_expected(boolean useStaticFactory) {
    // given
    Tracer.getInstance().startRequestWithRootSpan("request-" + UUID.randomUUID().toString());
    Deque<Span> spanStackMock = Tracer.getInstance().getCurrentSpanStackCopy();
    Map<String, String> mdcInfoMock = MDC.getCopyOfContextMap();

    // when
    FunctionWithTracing instance = (useStaticFactory)
                                     ? withTracing(functionMock)
                                     : new FunctionWithTracing(functionMock);

    // then
    assertThat(instance.origFunction).isSameAs(functionMock);
    assertThat(instance.spanStackForExecution).isEqualTo(spanStackMock);
    assertThat(instance.mdcContextMapForExecution).isEqualTo(mdcInfoMock);
}
 
源代码11 项目: wingtips   文件: BiConsumerWithTracingTest.java
@Before
public void beforeMethod() {
    biConsumerMock = mock(BiConsumer.class);

    inObj1 = new Object();
    inObj2 = new Object();
    throwExceptionDuringCall = false;
    currentSpanStackWhenBiConsumerWasCalled = new ArrayList<>();
    currentMdcInfoWhenBiConsumerWasCalled = new ArrayList<>();
    doAnswer(invocation -> {
        currentSpanStackWhenBiConsumerWasCalled.add(Tracer.getInstance().getCurrentSpanStackCopy());
        currentMdcInfoWhenBiConsumerWasCalled.add(MDC.getCopyOfContextMap());
        if (throwExceptionDuringCall)
            throw new RuntimeException("kaboom");
        return null;
    }).when(biConsumerMock).accept(inObj1, inObj2);

    resetTracing();
}
 
源代码12 项目: framework   文件: TransLoggerService4File.java
/**
 * Description: <br>
 * 
 * @author 王伟<br>
 * @taskId <br>
 * @param stackId
 * @param parentStackId
 * @param beginTime
 * @param method
 * @param params <br>
 */
@Override
public void before(final String stackId, final String parentStackId, final long beginTime, final String method,
    final Object[] params) {
    if (this.isAlwaysLog()) {
        MDC.put("stackId", stackId);
        MDC.put("parentStackId", parentStackId);
        MDC.put("method", method);
        MDC.put("params", Arrays.toString(params));
        logger.info("BEFORE");
        MDC.clear();
    }
    else {
        super.before(stackId, parentStackId, beginTime, method, params);
    }
}
 
源代码13 项目: riposte   文件: AsyncNettyHelperTest.java
@Test
public void linkTracingAndMdcToCurrentThread_pair_works_as_expected_with_non_null_pair_and_null_innards() {
    // given
    Pair<Deque<Span>, Map<String, String>> infoForLinking = Pair.of(null, null);
    resetTracingAndMdc();
    Tracer.getInstance().startRequestWithRootSpan("foo-" + UUID.randomUUID().toString());
    Pair<Deque<Span>, Map<String, String>> expectedPreCallInfo = Pair.of(
        Tracer.getInstance().getCurrentSpanStackCopy(),
        MDC.getCopyOfContextMap()
    );

    // when
    Pair<Deque<Span>, Map<String, String>> preCallInfo =
        AsyncNettyHelper.linkTracingAndMdcToCurrentThread(infoForLinking);
    Pair<Deque<Span>, Map<String, String>> postCallInfo = Pair.of(
        Tracer.getInstance().getCurrentSpanStackCopy(),
        MDC.getCopyOfContextMap()
    );

    // then
    assertThat(preCallInfo).isEqualTo(expectedPreCallInfo);
    assertThat(postCallInfo).isEqualTo(Pair.of(null, Collections.emptyMap()));
}
 
源代码14 项目: webanno   文件: DocumentServiceImpl.java
@Override
@Transactional
public void createAnnotationDocument(AnnotationDocument aAnnotationDocument)
{
    Validate.notNull(aAnnotationDocument, "Annotation document must be specified");
    
    if (isNull(aAnnotationDocument.getId())) {
        entityManager.persist(aAnnotationDocument);
        
        try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
                String.valueOf(aAnnotationDocument.getProject().getId()))) {
            log.info(
                    "Created annotation document [{}] for user [{}] for source document "
                    + "[{}]({}) in project [{}]({})",
                    aAnnotationDocument.getId(), aAnnotationDocument.getUser(), 
                    aAnnotationDocument.getDocument().getName(),
                    aAnnotationDocument.getDocument().getId(),
                    aAnnotationDocument.getProject().getName(),
                    aAnnotationDocument.getProject().getId());
        }
    }
    else {
        entityManager.merge(aAnnotationDocument);
    }
}
 
源代码15 项目: webanno   文件: ProjectServiceImpl.java
@Override
@Transactional
public void createProject(Project aProject)
    throws IOException
{
    if (aProject.getId() != null) {
        throw new IllegalArgumentException("Project has already been created before.");
    }
    
    aProject.setCreated(new Date());
    entityManager.persist(aProject);
    
    try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
            String.valueOf(aProject.getId()))) {
        log.info("Created project [{}]({})", aProject.getName(), aProject.getId());
    }
    
    String path = repositoryProperties.getPath().getAbsolutePath() + "/" + PROJECT_FOLDER + "/"
            + aProject.getId();
    FileUtils.forceMkdir(new File(path));
    
    applicationEventPublisher.publishEvent(new AfterProjectCreatedEvent(this, aProject));
}
 
源代码16 项目: yugong   文件: YuGongController.java
@Override
public void stop() {
    super.stop();
    for (YuGongInstance instance : instances) {
        if (instance.isStart()) {
            instance.stop();
        }
    }
    schedule.shutdownNow();
    MDC.remove(YuGongConstants.MDC_TABLE_SHIT_KEY);
    progressTracer.print(true);
    if (dataSourceFactory.isStart()) {
        dataSourceFactory.stop();
    }
    MDC.remove(YuGongConstants.MDC_TABLE_SHIT_KEY);
}
 
源代码17 项目: lucene-solr   文件: SolrLogLayout.java
private void appendMDC(StringBuilder sb) {
  if (!StringUtils.isEmpty(MDC.get(NODE_NAME_PROP)))  {
    sb.append(" n:").append(MDC.get(NODE_NAME_PROP));
  }
  if (!StringUtils.isEmpty(MDC.get(COLLECTION_PROP)))  {
    sb.append(" c:").append(MDC.get(COLLECTION_PROP));
  }
  if (!StringUtils.isEmpty(MDC.get(SHARD_ID_PROP))) {
    sb.append(" s:").append(MDC.get(SHARD_ID_PROP));
  }
  if (!StringUtils.isEmpty(MDC.get(REPLICA_PROP))) {
    sb.append(" r:").append(MDC.get(REPLICA_PROP));
  }
  if (!StringUtils.isEmpty(MDC.get(CORE_NAME_PROP))) {
    sb.append(" x:").append(MDC.get(CORE_NAME_PROP));
  }
}
 
源代码18 项目: gcp-token-broker   文件: ProxyUserValidation.java
public static void validateImpersonator(String impersonator, String impersonated) {
    String mappedImpersonated = AbstractUserMapper.getInstance().map(impersonated);
    EmailValidation.validateEmail(mappedImpersonated);
    MDC.put(LoggingUtils.MDC_AUTH_MODE_PROXY_IMPERSONATED_USER_KEY, impersonated);
    List<? extends Config> proxyConfigs = AppSettings.getInstance().getConfigList(AppSettings.PROXY_USERS);
    for (Config proxyConfig : proxyConfigs) {
        String proxy = proxyConfig.getString(CONFIG_PROXY);
        if (impersonator.equals(proxy)) {
            if (isWhitelistedByUsername(proxyConfig, mappedImpersonated)) {
                // The user is directly whitelisted by its username
                return;
            }
            else if (isWhitelistedByGroupMembership(proxyConfig, mappedImpersonated)) {
                // The user is whitelisted by group membership
                return;
            }
        }
    }
    throw Status.PERMISSION_DENIED
        .withDescription("Impersonation disallowed for `" + impersonator + "`")
        .asRuntimeException();
}
 
源代码19 项目: styx   文件: MiddlewaresTest.java
@Test
public void testExceptionAndRequestIdHandlerAcceptsRequestIdHeader()
    throws InterruptedException, ExecutionException, TimeoutException {
  final RequestContext requestContext = mock(RequestContext.class);
  final String requestId = UUID.randomUUID().toString();
  final Request request = Request.forUri("/", "GET")
      .withHeader("X-Request-Id", requestId);
  final AtomicReference<String> propagatedRequestId = new AtomicReference<>();
  when(requestContext.request()).thenReturn(request);

  Response<Object> response = Middlewares.exceptionAndRequestIdHandler()
      .apply(rc -> {
        propagatedRequestId.set(MDC.get("request-id"));
        LoggerFactory.getLogger(MiddlewaresTest.class).info("I'm OK!");
        return completedFuture(Response.forStatus(Status.OK));
      })
      .invoke(requestContext)
      .toCompletableFuture().get(5, SECONDS);

  assertThat(response, hasStatus(withCode(Status.OK)));
  assertThat(response, hasHeader("X-Request-Id", is(requestId)));
  assertThat(propagatedRequestId.get(), is(requestId));
}
 
@Test
public void properlyEscapesExclusions() throws Exception {
	StringBuilder sb = new StringBuilder();
	@SuppressWarnings("serial")
	Map<String, String> explicitFields = new HashMap<String, String>() {
		{
			put("explicit" + HACK_ATTEMPT, "explicit value");
		}
	};
	MDC.put("mdc" + HACK_ATTEMPT, "mdc value");

	converter.setExclusions(Arrays.asList("explicit" + HACK_ATTEMPT, "mdc" + HACK_ATTEMPT));
	converter.convert(sb, explicitFields);

	assertThat(unmarshal(sb), allOf(not(hasEntry("mdc" + HACK_ATTEMPT, "mdc value")),
			not(hasEntry("explicit" + HACK_ATTEMPT, "explicit value"))));
}
 
@Test
public void properlyEscapesValues() throws Exception {
	StringBuilder sb = new StringBuilder();
	@SuppressWarnings("serial")
	Map<String, String> explicitFields = new HashMap<String, String>() {
		{
			put("explicit key", "explicit" + HACK_ATTEMPT);
		}
	};
	MDC.put("mdc key", "mdc" + HACK_ATTEMPT);

	converter.convert(sb, explicitFields);

	assertThat(unmarshal(sb),
			allOf(hasEntry("mdc key", "mdc" + HACK_ATTEMPT), hasEntry("explicit key", "explicit" + HACK_ATTEMPT)));
}
 
源代码22 项目: wingtips   文件: ConsumerWithTracingTest.java
@Before
public void beforeMethod() {
    consumerMock = mock(Consumer.class);

    inObj = new Object();
    throwExceptionDuringCall = false;
    currentSpanStackWhenConsumerWasCalled = new ArrayList<>();
    currentMdcInfoWhenConsumerWasCalled = new ArrayList<>();
    doAnswer(invocation -> {
        currentSpanStackWhenConsumerWasCalled.add(Tracer.getInstance().getCurrentSpanStackCopy());
        currentMdcInfoWhenConsumerWasCalled.add(MDC.getCopyOfContextMap());
        if (throwExceptionDuringCall)
            throw new RuntimeException("kaboom");
        return null;
    }).when(consumerMock).accept(inObj);

    resetTracing();
}
 
源代码23 项目: HttpSessionReplacer   文件: TestSessionManager.java
@Test
public void testDisableSessionInMdc() {
  MDC.remove(configuration.getLoggingMdcKey());
  configuration.setLoggingMdcActive(false);
  SessionData sessionData = new SessionData("1", now(), 10);
  RepositoryBackedSession session = mock(RepositoryBackedSession.class);
  when(session.getId()).thenReturn("1");
  when(repository.getSessionData("1")).thenReturn(sessionData);
  when(factory.build(sessionData)).thenReturn(session);
  RequestWithSession request = mock(RequestWithSession.class);
  when(tracking.retrieveId(request)).thenReturn(new SessionTracking.IdAndSource("1", false));
  sessionManager.getSession(request, false, null);
  assertNull("Logging MDC should remain null", MDC.get(configuration.getLoggingMdcKey()));    
  MDC.put(configuration.getLoggingMdcKey(), "something");
  sessionManager.getSession(request, false, null);
  assertEquals("Logging MDC was changed", "something", MDC.get(configuration.getLoggingMdcKey()));    
  request = mock(RequestWithSession.class);
  sessionManager.getSession(request, false, null);
  assertEquals("Logging MDC was changed", "something", MDC.get(configuration.getLoggingMdcKey()));    
}
 
源代码24 项目: SkyBot   文件: CommandManager.java
private void runNormalCommand(ICommand cmd, String invoke, List<String> args, GuildMessageReceivedEvent event) {
    if (cmd.getCategory() == CommandCategory.NSFW && !event.getChannel().isNSFW()) {
        sendMsg(event, "Woops, this channel is not marked as NSFW.\n" +
            "Please mark this channel as NSFW to use this command");
        return;
    }

    MDC.put("command.class", cmd.getClass().getName());

    LOGGER.info("Dispatching command \"{}\" in guild \"{}\" with {}", cmd.getClass().getSimpleName(), event.getGuild(), args);

    cmd.executeCommand(
        new CommandContext(invoke, args, event, variables)
    );
}
 
源代码25 项目: dapeng-soa   文件: MdcCtxInfoUtil.java
private static void switchMdcToAppClassLoader(String methodName, ClassLoader appClassLoader, String mdcKey, String mdcValue) {
    try {
        MdcCtxInfo mdcCtxInfo = mdcCtxInfoCache.get(appClassLoader);
        if (mdcCtxInfo == null) {
            synchronized (appClassLoader) {
                mdcCtxInfo = mdcCtxInfoCache.get(appClassLoader);
                if (mdcCtxInfo == null) {
                    Class<?> mdcClass = appClassLoader.loadClass(MDC.class.getName());

                    mdcCtxInfo = new MdcCtxInfo(appClassLoader,
                            mdcClass,
                            mdcClass.getMethod("put", String.class, String.class),
                            mdcClass.getMethod("remove", String.class));
                    mdcCtxInfoCache.put(appClassLoader, mdcCtxInfo);
                }
            }
        }
        if (methodName.equals("put")) {
            mdcCtxInfo.put.invoke(mdcCtxInfo.mdcClass, mdcKey, mdcValue);
        } else {
            mdcCtxInfo.remove.invoke(mdcCtxInfo.mdcClass, mdcKey);
        }
    } catch (ClassNotFoundException | NoSuchMethodException
            | IllegalAccessException |
            InvocationTargetException e) {
        LOGGER.error(appClassLoader.getClass().getSimpleName() + "::switchMdcToAppClassLoader," + e.getMessage(), e);
    }
}
 
源代码26 项目: styx   文件: MDCDecoratingHandler.java
@Override
public void transitionInto(RunState runState, EventRouter eventRouter) {
  try (var closer = Closer.create()) {
    closer.register(MDC.putCloseable(WFI_ID, runState.workflowInstance().toString()));
    closer.register(MDC.putCloseable(WFI_STATE_COUNTER, Long.toString(runState.counter())));
    closer.register(MDC.putCloseable(WFI_STATE_NAME, runState.state().toString()));
    delegate.transitionInto(runState, eventRouter);
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
源代码27 项目: audiveris   文件: LogUtil.java
/**
 * In the calling thread, start log annotation with stub ID.
 *
 * @param stub the sheet/stub related to processing
 */
public static void start (SheetStub stub)
{
    start(stub.getBook());

    if (!SwingUtilities.isEventDispatchThread()) {
        MDC.put(SHEET, stub.getNum());
    }
}
 
源代码28 项目: wingtips   文件: TracerTest.java
@Test
public void registerWithThread_should_work_as_advertised_if_existing_stack_is_empty() {
    // given
    getSpanStackThreadLocal().set(new LinkedList<>());
    Tracer tracer = Tracer.getInstance();

    Deque<Span> newSpanStack = new LinkedList<>();
    Span parentSpan = Span.newBuilder("foo", SpanPurpose.LOCAL_ONLY).build();
    Span subspan = Span.newBuilder("bar", SpanPurpose.LOCAL_ONLY).build();
    newSpanStack.push(parentSpan);
    newSpanStack.push(subspan);

    assertThat(MDC.get(SpanFieldForLoggerMdc.TRACE_ID.mdcKey)).isNull();

    // when
    tracer.registerWithThread(newSpanStack);

    // then
    // our stack was registered, so subspan should be current
    assertThat(MDC.get(SpanFieldForLoggerMdc.TRACE_ID.mdcKey)).isEqualTo(subspan.getTraceId());
    assertThat(tracer.getCurrentSpan()).isEqualTo(subspan);

    // a *copy* of the stack we passed in should have been registered, and modifying the original stack should not affect Tracer's stack
    Deque<Span> spanStack = getSpanStackThreadLocal().get();
    assertThat(Tracer.getInstance().containsSameSpansInSameOrder(spanStack, newSpanStack)).isTrue();
    assertThat(spanStack).isNotSameAs(newSpanStack);

    newSpanStack.push(subspan.generateChildSpan("subsub", SpanPurpose.LOCAL_ONLY));
    assertThat(newSpanStack).hasSize(3);
    assertThat(spanStack).hasSize(2);
}
 
源代码29 项目: webanno   文件: DocumentServiceImpl.java
@Override
@Transactional
public void removeSourceDocument(SourceDocument aDocument)
    throws IOException
{
    Validate.notNull(aDocument, "Source document must be specified");
    
    // BeforeDocumentRemovedEvent is triggered first, since methods that rely 
    // on it might need to have access to the associated annotation documents 
    applicationEventPublisher.publishEvent(new BeforeDocumentRemovedEvent(this, aDocument));
    
    for (AnnotationDocument annotationDocument : listAllAnnotationDocuments(aDocument)) {
        removeAnnotationDocument(annotationDocument);
    }
    
    entityManager.remove(
            entityManager.contains(aDocument) ? aDocument : entityManager.merge(aDocument));

    String path = repositoryProperties.getPath().getAbsolutePath() + "/" + PROJECT_FOLDER + "/"
            + aDocument.getProject().getId() + "/" + DOCUMENT_FOLDER + "/" + aDocument.getId();
    
    // remove from file both source and related annotation file
    if (new File(path).exists()) {
        FileUtils.forceDelete(new File(path));
    }
    
    try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID,
            String.valueOf(aDocument.getProject().getId()))) {
        Project project = aDocument.getProject();
        log.info("Removed source document [{}]({}) from project [{}]({})", aDocument.getName(),
                aDocument.getId(), project.getName(), project.getId());
    }
}
 
源代码30 项目: emissary   文件: FileDataServer.java
/**
 * Report the problem file and move it to the error location
 * 
 * @param f the file having the problem
 */
protected void reportProblem(File f, String errDir) {
    MDC.put(MDCConstants.SHORT_NAME, f.getPath());
    try {
        String n = f.getName();
        boolean renamed = false;

        if (f.exists()) {
            if (!f.canRead()) {
                logger.warn("FileDataServer: cannot read file");
                renamed = f.renameTo(new File(errDir, n));
            }

            else if (!f.isFile()) {
                logger.warn("FileDataServer: file is not a normal file");
                renamed = f.renameTo(new File(errDir, n));
            }

            else if (f.length() <= 0) {
                logger.warn("FileDataServer: file has zero size");
                renamed = f.renameTo(new File(errDir, n));
            }

            if (!renamed) {
                logger.warn("File could not be moved");
            }
        } else {
            logger.warn("File does not exist");
        }
    } finally {
        MDC.remove(MDCConstants.SHORT_NAME);
    }
}