Logger
Logger helper class. Makes the logger operations easy. Application needs to have no.nordicsemi.android.LOG permission to use this logger.
To use a logger you must first create a logger session. Log entries can be then appended to the session. Session has 2 parameters: a non-changeable key, which can be e.g. a device address and a name, which can be a human-readable device name. nRF Logger application use the key to group devices together. The logger session contains also the application name + optionally a profile name. To create the session invoke newSession method.
Log entries may not be deleted from the database except by removing the whole session.
Usage:
Adding log entry:
LogSession session = Logger.newSession(this, "DFU", deviceAddress, deviceName);
Logger.i(session, "Log session has been created");
...
String error = e.getMessage();
Logger.e(session, R.string.error_message, error);
where in strings.xml there is:
<string name="error_message">An error occurred: %s</string>
Getting session log entries:
final Cursor c = getContentResolver().query(session.getSessionEntriesUri(), new String[] {
TIME,
LEVEL,
DATA }, null, null, TIME + " ASC");
try {
while (c.moveToNext()) {
final String data = c.getString(2 /* DATA */);
...
}
} finally {
c.close();
}
You may use the following Uris to perform operations on the data set:
- no.nordicsemi.android.log/application - returns all applications that has created at least one log session
- no.nordicsemi.android.log/application/[APP_ID] - returns all sessions from given application
- no.nordicsemi.android.log/session - returns all sessions
- no.nordicsemi.android.log/session/[SESSION_ID] - returns the session with given id
- no.nordicsemi.android.log/session/[SESSION_ID]/log - returns the log entries from the session with given id
- no.nordicsemi.android.log/session/key/[KEY] - returns all sessions with given key
- no.nordicsemi.android.log/session/key/[KEY]/[NUMBER] - returns the session with given key and number. Numbers starts from 1 for every key.
- no.nordicsemi.android.log/session/key/[KEY]/[NUMBER]/log - returns the log entries from the session with given key and number
For every log session created on a new day for a single application (and optionally profile) a special "date" session is created. Its number is equal to 0 and key to "!date". To obtain a list of non-date sessions for a given application id sorted by date, key and time use:
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
long appId = args.getLong(APPLICATION_ID);
Uri sessionsUri = LogContract.Session.createSessionsUri(appId);
String selection = LogContract.Session.NUMBER + ">0";
return new CursorLoader(this, sessionsUri,
new String[] { NAME, CREATED_AT },
selection, null, "date((" + CREATED_AT + " / 1000), 'unixepoch') DESC,
" + KEY + " ASC,
" + CREATED_AT + " DESC");
}