/** * Create a new LogBlock with the same body but updated uuid. * * @param uuid New uuid. * @return New LogBlock with updated uuid. */ public LogBlock withUuid(String uuid) { LogBlock newLogBlock = new LogBlock(uuid); body.entrySet().forEach(entry -> newLogBlock.body.put(entry.getKey(), entry.getValue())); return newLogBlock; }
/** * Record logging information in the logging context. * * @param logPhase The name of the class destined to hold this logging information * * @see LogBlock */ public static void record(LogInfo logPhase) { try { RLOG.get().info.add(logPhase); } catch (NullPointerException ignored) { LOG.warn( "Attempted to append log info while request log object was uninitialized: {}", logPhase.getClass().getSimpleName() ); } }
/** * Returns a map of all the LogInfo blocks currently registered with this thread's RequestLog. * * @return A map of all the LogInfo objects registered to this thread's RequestLog */ public static Map<String, LogInfo> retrieveAll() { RequestLog requestLog = RLOG.get(); if (requestLog == null) { String message = String.format("Attempted to retrieve log info while request log object was uninitialized"); LOG.error(message); throw new IllegalStateException(message); } return requestLog.info.any(); }
/** * Creates a new and empty request log at the calling thread. */ private void init() { logId = UUID.randomUUID().toString(); info = new LogBlock(logId); // Trick to place Durations and Threads in front of the Json while keep using a LinkedHashMap. // The actual entries will be replaced later when export is called but the initial order will be respected info.add(Durations.class); info.add(Threads.class); info.add(Preface.class); getLoginfoOrder().stream().filter(entry -> !Objects.equals(entry, "Epilogue")).forEachOrdered( entry -> { try { @SuppressWarnings("unchecked") Class<LogInfo> cls = (Class<LogInfo>) Class.forName(entry); info.add(cls); } catch (ClassNotFoundException | ClassCastException e) { String msg = ErrorMessageFormat.LOGINFO_CLASS_INVALID.logFormat(entry); LOG.warn(msg, e); } } ); times.clear(); threadIds.clear(); threadIds.add(Thread.currentThread().getName()); MDC.put(ID_KEY, logId); }
/** * Prepend an id prefix to generated druid query id. * * @param idPrefix Prefix for queryId sent to druid */ public static void addIdPrefix(String idPrefix) { RequestLog current = RLOG.get(); String newId = idPrefix + getId(); current.info = current.info.withUuid(newId); current.logId = newId; MDC.put(ID_KEY, newId); }
/** * Retrieve logging information in the logging context. * * @param cls The class destined to hold this logging information * * @return the logging information in the logging context * * @see LogBlock */ public static LogInfo retrieve(Class cls) { RequestLog requestLog = RLOG.get(); if (requestLog == null) { String message = String.format( "Attempted to retrieve log info while request log object was uninitialized: %s", cls.getSimpleName() ); LOG.error(message); throw new IllegalStateException(message); } LogInfo logInfo = requestLog.info.get(cls.getSimpleName()); if (logInfo == null) { String message = ErrorMessageFormat.RESOURCE_RETRIEVAL_FAILURE.format(cls.getSimpleName()); LOG.error(message); throw new IllegalStateException(message); } return logInfo; }