Java Code Examples for org.slf4j.MDC

The following examples show how to use org.slf4j.MDC. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: light-4j   Source File: Server.java    License: Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example 2
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);
}
 
Example 3
@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));
}
 
Example 4
Source Project: arcusplatform   Source File: BridgeMdcUtil.java    License: Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 5
@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);
        }
    }
}
 
Example 6
Source Project: emissary   Source File: DirectoryAdapter.java    License: Apache License 2.0 6 votes vote down vote up
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;
    }
}
 
Example 7
Source Project: wingtips   Source File: CallableWithTracingTest.java    License: Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 8
Source Project: cosmic   Source File: CallContext.java    License: Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 9
Source Project: govpay   Source File: AbstractTask.java    License: GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 10
Source Project: wingtips   Source File: FunctionWithTracingTest.java    License: Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 11
Source Project: wingtips   Source File: BiConsumerWithTracingTest.java    License: Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 12
Source Project: framework   Source File: TransLoggerService4File.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 13
Source Project: riposte   Source File: AsyncNettyHelperTest.java    License: Apache License 2.0 6 votes vote down vote up
@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()));
}
 
Example 14
Source Project: webanno   Source File: DocumentServiceImpl.java    License: Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example 15
Source Project: webanno   Source File: ProjectServiceImpl.java    License: Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example 16
Source Project: yugong   Source File: YuGongController.java    License: GNU General Public License v2.0 6 votes vote down vote up
@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);
}
 
Example 17
Source Project: lucene-solr   Source File: SolrLogLayout.java    License: Apache License 2.0 6 votes vote down vote up
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));
  }
}
 
Example 18
Source Project: gcp-token-broker   Source File: ProxyUserValidation.java    License: Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 19
Source Project: styx   Source File: MiddlewaresTest.java    License: Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example 20
@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"))));
}
 
Example 21
@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)));
}
 
Example 22
Source Project: wingtips   Source File: ConsumerWithTracingTest.java    License: Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 23
Source Project: HttpSessionReplacer   Source File: TestSessionManager.java    License: MIT License 6 votes vote down vote up
@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()));    
}
 
Example 24
Source Project: SkyBot   Source File: CommandManager.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
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)
    );
}
 
Example 25
Source Project: dapeng-soa   Source File: MdcCtxInfoUtil.java    License: Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 26
Source Project: styx   Source File: MDCDecoratingHandler.java    License: Apache License 2.0 5 votes vote down vote up
@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);
  }
}
 
Example 27
Source Project: audiveris   Source File: LogUtil.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 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());
    }
}
 
Example 28
Source Project: wingtips   Source File: TracerTest.java    License: Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 29
Source Project: webanno   Source File: DocumentServiceImpl.java    License: Apache License 2.0 5 votes vote down vote up
@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());
    }
}
 
Example 30
Source Project: emissary   Source File: FileDataServer.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}