static void init(String host, String portStr) { Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); BasicConfigurator.configure(root.getLoggerContext()); try { int port = Integer.parseInt(portStr); logger.info("Creating socket appender (" + host + "," + port + ")."); s = new SocketAppender(); s.setRemoteHost(host); s.setPort(port); s.setName("S"); root.addAppender(s); } catch (java.lang.NumberFormatException e) { e.printStackTrace(); usage("Could not interpret port number [" + portStr + "]."); } catch (Exception e) { System.err.println("Could not start!"); e.printStackTrace(); System.exit(1); } }
@Before public void resetLogging() { LoggerContext loggerContext = ((Logger) LoggerFactory.getLogger(getClass())).getLoggerContext(); loggerContext.reset(); BasicConfigurator.configure(loggerContext); }
@SuppressWarnings("unchecked") public static void main(String... args) throws ParseException, JoranException, IOException { Parser parser = new BasicParser(); CommandLine cl = null; try { cl = parser.parse(OPTS, args); } catch (ParseException e) { HelpFormatter help = new HelpFormatter(); help.printHelp("dlog", OPTS, true); System.exit(-1); } LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); loggerContext.reset(); if (cl.hasOption("config")) { // Read Logback configuration JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(loggerContext); configurator.doConfigure(cl.getOptionValue("file", "logback.xml")); StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext); } else { BasicConfigurator.configure(loggerContext); } Appender appender = null; if (cl.hasOption("output")) { String outputAppender = cl.getOptionValue("output", "console"); appender = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME).getAppender(outputAppender); } ChronicleTools.warmup(); Chronicle chronicle = new IndexedChronicle(cl.getOptionValue("path"), ChronicleConfig.DEFAULT); ExcerptTailer ex = chronicle.createTailer(); Level level = Level.valueOf(cl.getOptionValue("level", "TRACE")); if (cl.hasOption("head")) { int lines = Integer.parseInt(cl.getOptionValue("head", "10")); for (int i = 0; i < lines; i++) { LoggingEvent evt = readLoggingEvent(ex, loggerContext); if (evt.getLevel().isGreaterOrEqual(level)) { writeEvent(evt, appender); } } } else if (cl.hasOption("tail")) { int lines = Integer.parseInt(cl.getOptionValue("tail", "10")); Queue<LoggingEvent> tail = new LinkedBlockingQueue<LoggingEvent>(lines); while (ex.nextIndex()) { LoggingEvent evt = readLoggingEvent(ex, loggerContext); if (!tail.offer(evt)) { tail.poll(); tail.add(evt); } } LoggingEvent evt; while (null != (evt = tail.poll())) { if (evt.getLevel().isGreaterOrEqual(level)) { writeEvent(evt, appender); } } } else if (cl.hasOption("search")) { String regex = cl.getOptionValue("search"); Pattern regexPatt = Pattern.compile(regex); while (ex.nextIndex()) { LoggingEvent evt = readLoggingEvent(ex, loggerContext); if (null != evt && evt.getLevel().isGreaterOrEqual(level)) { if (regexPatt.matcher(evt.getFormattedMessage()).matches()) { writeEvent(evt, appender); } } } } loggerContext.stop(); chronicle.close(); }