@Test public void testObject() { Date date = new Date(); XLog.addObjectFormatter(Date.class, new ObjectFormatter<Date>() { @Override public String format(Date date) { return Long.toString(date.getTime()); } }).i(date); boolean result = (logsContainer.size() == 1 && logsContainer.get(0).msg.equals(Long.toString(date.getTime()))); assertTrue("Formatted object log not found", result); }
@Test public void testBorder() { XLog.b().i("Message with a border"); boolean result = (logsContainer.size() == 1 && logsContainer.get(0).msg.startsWith("╔═══") && logsContainer.get(0).msg.endsWith("════")); assertTrue("No bordered log found", result); logsContainer.clear(); XLog.nb().i("Message without a border"); result = (logsContainer.size() == 1 && !logsContainer.get(0).msg.startsWith("╔═══") && !logsContainer.get(0).msg.endsWith("════")); assertTrue("Bordered log found", result); }
@Override protected void onResume() { super.onResume(); String message; hasPermission = hasPermission(); if (hasPermission) { message = "Permission granted.\nLog to file."; } else { message = "Permission not granted.\nCan not log to file."; } XLog.printers(viewPrinter).i(message); }
XLog.printers(viewPrinter).i("XLog is ready.\nPrint your log now!");
@Test public void testModifyingInterceptor() { XLog.addInterceptor(new Interceptor() { @Override public LogItem intercept(LogItem log) { log.tag = CUSTOM_TAG; return log; } }).addInterceptor(new Interceptor() { @Override public LogItem intercept(LogItem log) { log.msg = log.msg + "[i1]"; return log; } }).addInterceptor(new Interceptor() { @Override public LogItem intercept(LogItem log) { log.msg = log.msg + "[i2]"; return log; } }).addInterceptor(new Interceptor() { @Override public LogItem intercept(LogItem log) { log.level = DEBUG; return log; } }).i(MESSAGE); assertLog(DEBUG, CUSTOM_TAG, MESSAGE + "[i1][i2]"); }
@Test public void testBlockingInterceptor() { XLog.addInterceptor(new Interceptor() { @Override public LogItem intercept(LogItem log) { log.msg = "i1"; return log; } }).addInterceptor(new Interceptor() { @Override public LogItem intercept(LogItem log) { // Block the log. return null; } }).addInterceptor(new Interceptor() { @Override public LogItem intercept(LogItem log) { log.msg = "i2"; return log; } }).i(MESSAGE); AssertUtil.assertNoLog(logsContainer); }
@Test public void testStackTrace() { XLog.st(1).i("Message with stack trace, depth 1"); boolean result = (logsContainer.size() == 1 && logsContainer.get(0).msg.contains("\t─ ")); assertTrue("No stack trace found", result); logsContainer.clear(); XLog.st(2).i("Message with stack trace, depth 2"); result = (logsContainer.size() == 1 && logsContainer.get(0).msg.contains("\t├ ")); assertTrue("No stack trace found", result); logsContainer.clear(); XLog.nst().i("Message without stack trace"); result = (logsContainer.size() == 1 && !logsContainer.get(0).msg.contains("\t├ ")); assertTrue("Stack trace found", result); }
@Test public void testCustomBorderFormatter() { XLog.t().threadFormatter(new ThreadFormatter() { @Override public String format(Thread data) { return "T1"; } }).b().borderFormatter(new BorderFormatter() { @Override public String format(String[] segments) { return addCustomBorder(segments); } }).i(MESSAGE); assertLog(INFO, DEFAULT_TAG, addCustomBorder(new String[]{"T1", MESSAGE})); }
@Test public void testReplacingInterceptor() { XLog.addInterceptor(new Interceptor() { @Override public LogItem intercept(LogItem log) { return new LogItem(LogLevel.VERBOSE, "tag1", "msg1"); } }).addInterceptor(new Interceptor() { @Override public LogItem intercept(LogItem log) { return new LogItem(LogLevel.DEBUG, "tag2", "msg2"); } }).i(MESSAGE); assertLog(DEBUG, "tag2", "msg2"); }
@Test public void testCustomThreadFormatter() { final String formattedThread = "This is the thread info"; XLog.threadFormatter( new ThreadFormatter() { @Override public String format(Thread data) { return formattedThread; } }) .t() .i(MESSAGE); assertLog(INFO, DEFAULT_TAG, formattedThread + "\n" + MESSAGE); }
@Test public void testCustomStackTraceFormatter() { final String formattedStackTrace = "This is the stack trace"; XLog.stackTraceFormatter( new StackTraceFormatter() { @Override public String format(StackTraceElement[] data) { return formattedStackTrace; } }) .st(1) .i(MESSAGE); assertLog(INFO, DEFAULT_TAG, formattedStackTrace + "\n" + MESSAGE); }
@Test public void testCustomPrinter() { XLog.printers( new ContainerPrinter(logsContainer) { @Override protected LogItem onPrint(LogItem logItem) { logItem.msg = CUSTOM_PRINTER_MSG_PREFIX + logItem.msg; return logItem; } }) .i(MESSAGE); assertLog(INFO, DEFAULT_TAG, CUSTOM_PRINTER_MSG_PREFIX + MESSAGE); }
@Test public void testThread() { XLog.t().t().i("Message with thread info"); boolean result = (logsContainer.size() == 1 && logsContainer.get(0).msg.contains("Thread: ")); assertTrue("No thread info found", result); logsContainer.clear(); XLog.t().nt().i("Message without thread info"); result = (logsContainer.size() == 1 && !logsContainer.get(0).msg.contains("Thread: ")); assertTrue("Thread info found", result); }
@Test public void testCustomThrowableFormatter() { final String formattedThrowable = "This is a throwable"; XLog.throwableFormatter( new ThrowableFormatter() { @Override public String format(Throwable data) { return formattedThrowable; } }) .i(MESSAGE, new Throwable()); assertLog(INFO, DEFAULT_TAG, MESSAGE + "\n" + formattedThrowable); }
@Test public void testTag() { XLog.i(MESSAGE); assertLog(INFO, DEFAULT_TAG, MESSAGE); logsContainer.clear(); XLog.tag(CUSTOM_TAG).i(MESSAGE); assertLog(INFO, CUSTOM_TAG, MESSAGE); }