elvishew/xLog

如何获取 sIsInitialized 变量?

Nstd opened this issue · 5 comments

Nstd commented

up主,你好,我在初始化XLog的时候,设置了一个filePrinter,文件路径是通过一个封装过的工具类:StorageUtil获取的,获取这个路径的时候,做了一个“是否要创建目录”的判断,同时输出相关日志,此时由于XLog并未初始化,导致崩溃,所以,想手工判断一下是否初始化,否则不用XLog输出。

PS: XLog暂时用做日志文件备份,项目中还是有一个日志工具的,只不过在那个日志工具调用Log.x()后,再调用XLog

@Nstd
You shouldn't use xLog when you are still initializing xLog, so I think you don't need to check if xLog is initialized.

If you really want to check, you can define a boolean field and set it to true when you initialize xLog, then you can check this boolean field later. Or you can read the sIsInitialized value via reflecting.

Nstd commented

@elvishew
Thanks for your reply, and as you said, I use a boolean field to record the xLog initialize state, but the code is a bit ugly.
When replying, I have an idea. How about creating an ExceptionHandler or ExceptionHandleStrategy to determine how to handle the exception ?
When xLog haven't initialized or just initializing , I just want to ignore the log.

@Nstd If you don't need xLog to print the log, just set the log level to LogLevel.NONE, you can always initialize xLog in the very beginning of your app, like Application.onCreate

Version 1.4.0 are going to be released, with this release, when you use xLog but forget to initialize it, a warning will be log, instead of throwing a exception.

Nstd commented

thanks~

@Nstd Since the access level of sIsInitialized is package-private, you can create package 'com.elvishew.xlog' in your project, and create a class with a public static method to return the value of XLog.sIsInitialized