elvishew/xLog

How to print the thread info and the code line number

Closed this issue · 5 comments

你好,请问怎样打印出线程与输出日志所在的代码行数,就像orhanobut的logger中那样。
如下面的:
Thread: main与MainActivity.initData (MainActivity.java:66)
╔═══════════════════════════════════════════
║ Thread: main
╟──────────────────────────────
║ MainActivity.initData (MainActivity.java:66)
╟──────────────────────────────
║ {
║ "name": "Elvis",
║ "age": 18
║ }
╚═══════════════════════════════════════════

@susong0618 Does the thread id and method line important enough? Normally, we only want to print the method line when we need, instead of printing it ahead of every log message. If you really need the method line info, you can call XLog.method(Object...args).

And about the thread id, I will add it in development plan.

@elvishew 感谢您的回复,我觉得method line 对我是重要的,我可以在logcat直接定位日志输出的位置,如果没有这个method line,我可能要找半天,我不是一定要在每句输出中打印方法行数,能做个可选配置就行,希望您能加上这个功能,感谢。
再次感谢您能加入打印线程信息的功能。

@susong0618
Currently you can override
AndroidPrinter#onPrintBorderedMessage(int logLevel, String tag, String msg)
or
FilePrinter#onPrintFormattedMessage(int logLevel, String tag, String msg)
and wrap the msg with thread id.
e.g:
super.onPrintBorderedMessage(logLevel, tag, Thread.currentThread().getName() + "\n" + msg);
or
super.onPrintFormattedMessage(logLevel, tag, Thread.currentThread().getName() + "\n" + msg);

And remember to pass the printers when calling XLog#init(...).

Hope this help you.

@elvishew Thank you very much!

The APIs are changed a lot when v1.0.0 released.

Since v1.0.0

  • Config about printing the thread info, see LogConfiguration.Builder.t() and LogConfiguration.Builder.nt()
  • Config about printing the stack trace(including line number) info, see LogConfiguration.Builder.st(int) and LogConfiguration.Builder.nst()

For example, if you want to print a log with thread info and stack trace info(with depth 2), just call XLog.t().st(2).d("Simple message")