@Override public Optional<Profile> parseFile(Path path) { log.entry(path); try { List<String> lines = Files .lines(path) .map(String::trim) .filter(this::isNotCommentLine) .collect(Collectors.toList()); return log.exit(Optional.of( Profile.builder() .arguments(lines.stream() .filter(this::isNotFieldLine) .flatMap(this::separateWhitespace) .collect(Collectors.toList())) .fields(lines.stream() .filter(this::isFieldLine) .collect(Collectors.toMap(this::extractKey, this::extractValue))) .location(path) .build())); } catch (IOException e) { log.catching(XLogger.Level.WARN, e); return log.exit(Optional.empty()); } }
@Override public Optional<List<Path>> scan() { try { return Optional.of(createStreamWithLogLevel(XLogger.Level.WARN).collect(Collectors.toList())); } catch (RuntimeException ex) { return Optional.empty(); } }
@Override public Stream<Path> streamAndIgnoreErrors() { try { return createStreamWithLogLevel(XLogger.Level.WARN); } catch (RuntimeException e) { return Stream.empty(); } }
private Stream<Path> createStreamWithLogLevel(XLogger.Level logLevel) { try { return Files .walk(searchDir.get(), this.depth, FileVisitOption.FOLLOW_LINKS) .filter(path -> !path.equals(searchDir.get())) .filter(this::includeFileOrDirectory) .filter(this::hasAllowedExtension) .filter(this::hasNotCompanionFile); } catch (IOException e) { log.catching(logLevel, e); throw new RuntimeException(e); } }
@Override public RunningExternalProcess sleep(long timeout, TimeUnit unit) { try { Thread.sleep(unit.toMillis(timeout)); } catch (InterruptedException e) { log.catching(XLogger.Level.WARN, e); } return this; }
@Synchronized @Override public void joinCluster() { if (isConnected()) { log.info("Already joined the cluster {}.", settings.getClusterName()); return; } try { log.debug("Joining cluster {}...", settings.getClusterName()); this.channel = new JChannel(settings.getJgroupsConfigFile()); channel.setName(settings.getHostname()); channel.connect(settings.getClusterName()); ForkChannel taskChannel = new ForkChannel(channel, "tasks", "tasks_ch"); taskChannel.connect(settings.getClusterName()); taskState.initialize(taskChannel, channel.getAddressAsString()); ForkChannel rpcChannel = new ForkChannel(channel, "rpc", "rpc_ch"); rpcChannel.connect(settings.getClusterName()); messageDispatcher.initialize(rpcChannel, channel.getAddressAsString()); if (executor != null) executor.shutdown(); executor = Executors.newSingleThreadScheduledExecutor(); executor.scheduleAtFixedRate(taskState::removeOrphanTasks, 1, 1, TimeUnit.MINUTES); log.info("Joined cluster {} with {} member(s).", channel.getClusterName(), channel.getView().getMembers().size()); log.info("Cluster address: {}", channel.getAddress()); } catch (Exception e) { channel = null; log.catching(XLogger.Level.WARN, e); log.info("Could not create or join cluster. Will work as single node."); } }
@Synchronized @Override public void leaveCluster() { if (channel == null) return; try { log.info("Leaving cluster..."); channel.disconnect(); if (executor != null) executor.shutdown(); log.info("Left the cluster."); } catch (Exception ex) { log.catching(XLogger.Level.WARN, ex); } channel = null; }
public void testEntering() { XLogger logger = XLoggerFactory.getXLogger("UnitTest"); logger.entry(); logger.entry(1); logger.entry("test"); logger.entry("a", "b", "c", "d"); logger.entry("a", "b", "c", "d", "e"); logger.entry("a", "b", "c", "d", "e", "f"); assertEquals(6, listAppender.list.size()); verify((LoggingEvent) listAppender.list.get(0), "entry"); verify((LoggingEvent) listAppender.list.get(1), "entry with (1)"); verify((LoggingEvent) listAppender.list.get(2), "entry with (test)"); }
public void testExiting() { XLogger logger = XLoggerFactory.getXLogger("UnitTest"); logger.exit(); assertEquals(Integer.valueOf(0), logger.exit(0)); assertEquals(Boolean.FALSE, logger.exit(false)); assertEquals(3, listAppender.list.size()); verify((LoggingEvent) listAppender.list.get(0), "exit"); verify((LoggingEvent) listAppender.list.get(1), "exit with (0)"); verify((LoggingEvent) listAppender.list.get(2), "exit with (false)"); }
public void testThrowing() { XLogger logger = XLoggerFactory.getXLogger("UnitTest"); Throwable t = new UnsupportedOperationException("Test"); assertEquals(t, logger.throwing(t)); assertEquals(t, logger.throwing(XLogger.Level.DEBUG, t)); assertEquals(2, listAppender.list.size()); verifyWithException((LoggingEvent) listAppender.list.get(0), "throwing", t); LoggingEvent event = (LoggingEvent) listAppender.list.get(1); verifyWithLevelAndException(event, XLogger.Level.DEBUG, "throwing", t); }
public void testCaught() { XLogger logger = XLoggerFactory.getXLogger("UnitTest"); long x = 5; Throwable t = null; try { @SuppressWarnings("unused") long y = x / 0; } catch (Exception ex) { t = ex; logger.catching(ex); logger.catching(XLogger.Level.DEBUG, ex); } verifyWithException((LoggingEvent) listAppender.list.get(0), "catching", t); verifyWithLevelAndException((LoggingEvent) listAppender.list.get(1), XLogger.Level.DEBUG, "catching", t); }
@Override public Stream<Path> stream() { return createStreamWithLogLevel(XLogger.Level.ERROR); }
public LoggedAction withLevel(XLogger.Level level) { Objects.requireNonNull(level); this.level = Loggers.valueOf(level.name()); return this; }
Loggers(BiConsumer<XLogger, String> consumer) { this.consumer = consumer; }
void logWith(XLogger logger, String statement) { consumer.accept(logger, statement); }
void verifyWithLevelAndException(LoggingEvent le, XLogger.Level level, String expectedMsg, Throwable t) { verify(le, expectedMsg); assertEquals(t.toString(), le.getThrowableStrRep()[0]); assertEquals(le.getLevel().toString(), level.toString()); }