The key need here is being able to log to local storage or web sql. There are quite a few existing frameworks out there, many support more intelligent logging to the console with different log levels and the ability to configure for different deployments, some support remote logging via AJAX calls, but I haven't run across any that do precisely what you are looking for.
One of the most prominent and active of these seems to be log4javascript. I have to confess that I haven't used it, but in looking at the code, it could be extended to add an appender for local storage or web sql.