xLog icon indicating copy to clipboard operation
xLog copied to clipboard

保存日志到本地文件,可以记录进程id和进程名吗?

Open tianxiake opened this issue 3 years ago • 5 comments

apk做了多进程,看本地日志文件的时候,只能看到线程id,无法看到进程id和进程名,不太方便。谢谢

tianxiake avatar Jul 16 '21 08:07 tianxiake

For performance, load process info only once.

static String procName = loadProcessName();
static int pid = loadPid();

You can add an Interceptor when initializing xLog, and append procName and pid to log message

LogConfiguration config = new LogConfiguration.Builder()
    .addInterceptor(log -> {
      log.msg = log.msg + <process name and ID>;
      return log;
    })
    ...
XLog.init(config...)

Or, if you want the process info displayed together with thread info, you can customize a ThreadFormatter

LogConfiguration config = new LogConfiguration.Builder()
    .enableThreadInfo()
    .threadFormatter(thread -> {
      return "Process" + pid + ":" + procName + " Thread:" + thread.getName();
    })
    ...
XLog.init(config...)

Hope this help you!

elvishew avatar Jul 16 '21 11:07 elvishew

Thanks. I read the source of xlog find I can customize a ThreadFormatter to append msg to log. the expandability of xlog is so great! Thanks for you!

tianxiake avatar Jul 19 '21 08:07 tianxiake

But I don't understand why design tag for global in default.In my case I need tag for current class. I found that use Xlog.tag(xx) to custom tag,but invoke xlog.tag(xxx) will create a new Logger object in every times, For performance, I think this is not good ,this is my views, hoping your answer!

tianxiake avatar Jul 19 '21 08:07 tianxiake

But I don't understand why design tag for global in default.In my case I need tag for current class. I found that use Xlog.tag(xx) to custom tag,but invoke xlog.tag(xxx) will create a new Logger object in every times, For performance, I think this is not good ,this is my views, hoping your answer!

Currently, if you want to use different tags in different classes, you can keep a Logger with a custom tag in each class, and use this Logger.* instead of using XLog.* directly.

elvishew avatar Jul 19 '21 13:07 elvishew

怎么抓取别的进程的日志啊?另外,我想在不同的类中打印日志的时候,添加对应类名作为tag 可以实现么?

longzekai avatar Jun 16 '22 08:06 longzekai