A simple sqlite-logger for React Native based on CocoaLumberjack on iOS and Logback on Android.
- 💆♂️ Easy to setup: Just call
SQLiteLogger.configure()
and you're done. All your existingconsole.log/debug/...
calls are automatically logged into a DB
React-native-sqlite-logger uses the undocumented global.__inspectorLog
from React Native. It allows to intercept any calls to console
and to retrieve the already-formatted log message. React-native-sqlite-logger uses DB-loggers from CocoaLumberjack on iOS and Logback Android on Android to append messages into DB.
npm i react-native-sqlite-logger
npx pod-install
import { SQLiteLogger } from "react-native-sqlite-logger";
SQLiteLogger.configure();
This is all you need to add sqlite-logging to your app. All your existing console
calls will be appended into a DB. SQLiteLogger.configure()
also takes several options to customize logging. If you don't want to use console
calls for logging, you can also use the direct access API.
Initialize the sqlite-logger with the specified options. As soon as the returned promise is resolved, all console
calls are inserted into a DB. To ensure that no logs are missing, it is good practice to await
this call at the launch of your app.
Option | Description | Default |
---|---|---|
logLevel |
Minimum log level for DB output (it won't affect console output) | LogLevel.Debug |
formatter |
A function that takes the log level and message and returns the formatted string to write into a DB. | Default format: ${msg} |
captureConsole |
If true , all console calls are automatically captured and written into a DB. It can also be changed by calling the enableConsoleCapture() and disableConsoleCapture() methods |
true |
logFileDir |
Absolute path of directory where the logs are stored in a DB file. If not defined, appropriate directory is automatically chosen. | undefined |
logFileName |
Name of the DB file where the logs are stored. | logs.sqlite |
maxAge |
Maximal age (in seconds) of the log messages. Messages older than maxAge could be automatically removed. |
60 * 60 * 24 * 5 (5 days) |
deleteInterval |
How often (in seconds) to delete old log messages. Value lower or equal to zero means that logs won't be deleted. | 60 * 5 (5 minutes) |
Delete logs according to the filter criteria.
Option | Description |
---|---|
start |
Delete logs where log.timestamp >= start |
end |
Delete logs where log.timestamp <= end |
maxId |
Delete logs where log.id <= maxId |
Returns the absolute path of a DB log file.
Returns the list of log messages according to the filter criteria.
Option | Description |
---|---|
start |
Fetch logs where log.timestamp >= start |
end |
Fetch logs where log.timestamp <= end |
level |
Fetch logs where log.level === level |
limit |
Fetch at most limit logs in the result list |
order |
Order result list by timestamp. Possible values are asc (default) and desc |
Enable appending messages from console
calls into the DB. It is already enabled by default when calling SQLiteLogger.configure()
.
After calling this method, console
calls will no longer be written into the DB.
Change the minimum log level for DB output. The initial log level can be passed as an option to SQLiteLogger.configure()
.
Return the current log level.
If you don't want to use console
calls for DB logging, you can directly use the following methods to directly insert messages into the DB. It is encouraged to wrap these calls with your own logger API.
Shortcut for SQLiteLogger.write(LogLevel.Trace, msg)
.
Shortcut for SQLiteLogger.write(LogLevel.Debug, msg)
.
Shortcut for SQLiteLogger.write(LogLevel.Info, msg)
.
Shortcut for SQLiteLogger.write(LogLevel.Warning, msg)
.
Shortcut for SQLiteLogger.write(LogLevel.Error, msg)
.
Append the given message into the DB with the specified log level. The message will be formatted with the formatter
function specified during the SQLiteLogger.configure()
call.
If you are using the console
logger api, please check that you do NOT strip logging from your release build with custom transformers in your babel.config.js
like babel-plugin-transform-remove-console