Logger

open class 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
Please use LogContract member classes to build Uris.

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");
}

Constructors

Link copied to clipboard
constructor()

Properties

Link copied to clipboard
val MARK_CLEAR: Int = 0
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
open fun a(@Nullable session: ILogSession, @NonNull message: String)
open fun a(@Nullable session: ILogSession, @StringRes messageResId: Int, params: Array<Any>)
Logs the message in APPLICATION level.
Link copied to clipboard
open fun d(@Nullable session: ILogSession, @NonNull message: String)
open fun d(@Nullable session: ILogSession, @StringRes messageResId: Int, params: Array<Any>)
Logs the message in DEBUG (lowest) level.
Link copied to clipboard
open fun e(@Nullable session: ILogSession, @NonNull message: String)
open fun e(@Nullable session: ILogSession, @StringRes messageResId: Int, params: Array<Any>)
Logs the message in ERROR (highest) level.
Link copied to clipboard
open fun i(@Nullable session: ILogSession, @NonNull message: String)
open fun i(@Nullable session: ILogSession, @StringRes messageResId: Int, params: Array<Any>)
Logs the message in INFO level.
Link copied to clipboard
open fun log(@Nullable session: ILogSession, @Nullable values: Array<ContentValues>)
Inserts an array of log entries in a bulk insert operation.
open fun log(@Nullable session: ILogSession, @Nullable values: List<ContentValues>)
Inserts a list of log entries in a bulk insert operation.
open fun log(@Nullable session: ILogSession, level: Int, @NonNull message: String)
open fun log(@Nullable session: ILogSession, level: Int, @StringRes messageResId: Int, params: Array<Any>)
Adds the log entry to nRF Logger log.
Link copied to clipboard
open fun logEntry(@Nullable session: ILogSession, level: Int, @NonNull message: String): ContentValues
open fun logEntry(@Nullable session: ILogSession, level: Int, @StringRes messageResId: Int, params: Array<Any>): ContentValues
Returns the log entry.
Link copied to clipboard
open fun newSession(@NonNull context: Context, @NonNull key: String, @Nullable name: String): LogSession
open fun newSession(@NonNull context: Context, @Nullable profile: String, @NonNull key: String, @Nullable name: String): LogSession
Creates new logger session.
Link copied to clipboard
Returns the log session object.
Link copied to clipboard
open fun setSessionDescription(@Nullable session: ILogSession, @Nullable description: String)
Sets the session description.
Link copied to clipboard
open fun setSessionMark(@Nullable session: ILogSession, mark: Int)
Sets the session mark (star or flag).
Link copied to clipboard
open fun v(@Nullable session: ILogSession, @NonNull message: String)
open fun v(@Nullable session: ILogSession, @StringRes messageResId: Int, params: Array<Any>)
Logs the message in VERBOSE level.
Link copied to clipboard
open fun w(@Nullable session: ILogSession, @NonNull message: String)
open fun w(@Nullable session: ILogSession, @StringRes messageResId: Int, params: Array<Any>)
Logs the message in WARNING level.