elvishew/xLog

Check if XLog already initialized

stanmots opened this issue · 2 comments

Hi,

Can you add the possibility to check whether the XLog has already been initialized?

When I run a bunch of Robolectric tests the following exception is being thrown:

java.lang.RuntimeException: java.lang.IllegalStateException: XLog is already initialized, do not initialize again

I am initializing the XLog in my application subclass:

 override fun onCreate() {
        super.onCreate()

        XLog.init(if (BuildConfig.DEBUG) LogLevel.ALL else LogLevel.NONE)
 }

It seems that Robolectric creates the application multiple times but XLog for some reason stays initialized.

What do you think? Maybe the public getter of sIsInitialized would be enough to solve this issue?

@storix Maybe this exception is not necessary to be thrown out, anyway, can you use

static {
  XLog.init(...);
}

instead, to see if it works?
I will fix this issue in next release.

Here is the relevant Robolectric issue - link

There was a similar problem with another library called ACRA. They decided to change the exception to a simple warning.

Currently I'm using a static field isLoggerInitialized to fix this issue like this:

    if (!isLoggerInitialized) {
          XLog.init(if (BuildConfig.DEBUG) LogLevel.ALL else LogLevel.NONE)
          isLoggerInitialized = true
    }