Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can you make FilePrinter.java appendLog() to catch Exception? #70

Open
jclova opened this issue Apr 3, 2019 · 4 comments
Open

Can you make FilePrinter.java appendLog() to catch Exception? #70

jclova opened this issue Apr 3, 2019 · 4 comments

Comments

@jclova
Copy link

jclova commented Apr 3, 2019

void appendLog(String flattenedLog) {
try {
bufferedWriter.write(flattenedLog);
bufferedWriter.newLine();
bufferedWriter.flush();
} catch (IOException e) {
}
}

Can you change catch IOException to Exception?

I am getting many following NullPointerException
Fatal Exception: java.lang.NullPointerException
Attempt to invoke virtual method 'void java.io.Writer.write(java.lang.String)' on a null object reference
com.elvishew.xlog.printer.file.FilePrinter$Writer.appendLog (FilePrinter.java:481)

As the application trying to initialize XLog, there are some logs being logged concurrently.
Some logs are being written even before bufferedWriter is initialized.

@elvishew
Copy link
Owner

elvishew commented Apr 4, 2019

@jclova Have you modified the xlog library's source code, like set the FilePrinter#USE_WORKER to false?

By default, USE_WORKER is set to true, and we use a worker, which is thread-safe, to print the log, so no worry about being logged concurrently. But if you set USE_WORKER to false, then something wrong may happen.

Besides, as what you said "Some logs are being written even before bufferedWriter is initialized.", no worry, if you use XLog to log, an exception will be thrown if you haven't init xlog.

@jclova
Copy link
Author

jclova commented Apr 11, 2019

No, I did not FilePrinter#USE_WORKER to false.
I have looked into your source code too and I don't see any cases that it will throw an error.
However, in reality I have a bunch of user crashes reported to my Crashlytics.
image

@jclova
Copy link
Author

jclova commented Apr 11, 2019

btw, this is what my init method looks like:

`public static void init(Context context, boolean isDebug) {
Printer androidPrinter = new AndroidPrinter();
filePrinter = new FilePrinter
.Builder(context.getFilesDir().getAbsolutePath() + "/xlog/")
.backupStrategy(new NeverBackupStrategy())
.build();

    LogConfiguration config = new LogConfiguration.Builder()
            .logLevel(isDebug ?
                      LogLevel.ALL
                              :
                      LogLevel.INFO)
            .tag(DEFAULT_TAG)
            .build();
    XLog.init(config,
            androidPrinter,
            filePrinter);

    XLog.d("*** Application start ***");
}`

@elvishew
Copy link
Owner

Finally figured out the root reason:
Before the log-to-file worker started, if there are several logging requests from different threads come in together, the worker may be started several times, that results multiple writing thread, and singleton isn't guaranteed any more.

Make sure only one writing thread, the crash should be resolved

Please update to v1.8.0 ASAP, and sorry for letting this issue existing so long

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants