Write Cordova console.log to file

2019-05-07 13:32发布

问题:

Does anyone knows if it is possible the console.log to write to a file or something similar?

I have already log for my application but it is only written on the console. For purposes of remote debugging I need the existing log to be written to a file too.

I thought of creating a File and write my log inside that file but that will make me duplicate the existing log code. So it could be better if it is possible to redirect where the console.log writes.

My application uses Ionic, Cordova and is hybrid for iOS and android using crosswalk browser plugin.

回答1:

This is the plugin cordova-plugin-logtofile that suits your requirement. But unfortunately it works only in Android.

Since you are using ionic framework, have a look at this too - FileLogger



回答2:

You need to add plugin console plugin.

To add:

cordova plugin add https://github.com/apache/cordova-plugin-console



回答3:

Yes, you can create a logging service to wrap console.log and the cordova file plugin: http://ngcordova.com/docs/plugins/file/

When you start the app, open a file for writing, then simply using the service to log to console and the file.

so instead of console.log you use LoggingService.log()