Skip to content

Logging

Using the Mobile development kit, you can set up logging in one of two ways:

Using Logging Actions

Logs help you trace events that occur while your application is running. The Mobile development kit offers a range of actions that can be used to log messages, set current logger levels, to enable or disable logging, and upload stored log entries to mobile services.

The following log actions are supported:

Set State

You can enable, disable, or set the logger to automatically toggle itself on or off during execution.

The following table describes metadata properties for the set state action:

Editor Property Metadata Property Type Required/Optional Description and Permitted Values
LogFileName LogFileName String Optional Name of the log file. Default name is ClientLog.txt.
MaxFileSize MaxFileSize Number Optional Maximum size of the log file in megabytes (MB). Default size is 5 MB. If value is 0, the file size is not limited.
Log State LoggerState String Required State of the logger. Value can be:
  • On
  • Off
  • Toggle

Sample code schema:

{
  "LogFileName": "newClientLog.txt",
  "MaxFileSize": 100,
  "LoggerState": "On",
  "_Type": "Action.Type.Logger.SetState"
}

Note

You can add Set State log action at application onLaunch event so that when your app launches it starts gathering logs.

Set Level

You can set the category of the log based on severity.

The following table describes metadata properties that are applicable to the set level action:

Editor Property Metadata Property Type Required/Optional Description and Permitted Values
Level Level String Required Severity level of the log. Value can be one of the following:
  • Trace (lowest severity, such as verbose tracing information)
  • Debug (very low severity, such as tracing information)
  • Info (low severity, such as information of lapsed event)
  • Warn (medium severity, such as potential issues or non-fatal errors)
  • Error (high severity, such as information on fatal errors)
  • Off (turns off the logging feature, no information will be logged)

Sample code schema:

{
 "Level": "Debug",
 "_Type": "Action.Type.Logger.SetLevel"
}

Note

You can bind Set Level log action to the success of Set State action.

Upload

You can upload stored logs from the user's mobile device to SAP Mobile Services.

Sample code schema:

{
    "_Type": " Action.Type.Logger.Upload"
}

Note

Developers can call this Upload log action from anywhere in the application. For example, from the onPress event of any button.

Log Message

You can set log messages and categorize them based on the severity level.

The following table describes the metadata properties that are applicable to the log message action:

Editor Property Metadata Property Type Required/Optional Description and Permitted Values
Message Message String Required Message that is logged
Log Level Level String Optional Severity level of the log message. Value can be one of the following:
  • Trace (lowest severity, such as verbose tracing information)
  • Debug (very low severity, such as tracing information)
  • Info (low severity, such as information of lapsed event)
  • Warn (medium severity, such as potential issues or non-fatal errors)
  • Error (high severity, such as information on fatal errors)
  • Off (turns off the logging feature, no information will be logged)

Sample code schema:

{
 "Message": "setting up log message to Error",
   "Level": "Error",
 "_Type": "Action.Type.Logger.LogMessage"
}

Using the Client API in Rules

The interface of the object returned to a rule function when the rule executes the IClientAPI::getLogger() method.

// Initialize logger by rule
// Ideally used when the application starts
export default function InitializeLoggerRule(clientAPI) {

  const logFileName = 'LogFile.txt';
  const maxFileSizeInMegaBytes = 8;
  // If the logger has already been initialized, it has no effect.
  // FileName and fileSize are optional values, if not specified, default values will be used.
  clientAPI.initializeLogger(logFileName, maxFileSizeInMegaBytes);

  // You can even initialize logger state and level
  let logger = clientAPI.getLogger;
  logger.on;
  logger.setLevel('Info');
}

// Or you can even use this
// Initialize logger by rule which calls an action
export default function InitializeLoggerWithCallingAnAction(clientAPI) {
  return clientAPI.executeAction('/AssetWorkManager/Actions/Logger/SetState.action');
}

// Use the logger by rule (it should have been already initialized)
// Available functionality:
// Log a message, Edit log level, Edit state (turn on/off), Get current logger state,
// Get current root log level, Upload log file
export default function UseLoggerRule(clientAPI) {

  // Get logger instance
  let logger = clientAPI.getLogger;

  // Returns a boolean according to current logger state (on=true, off=false)
  logger.isTurnedOn;

  // Turns off the logger
  logger.off;

  // Turns on the logger
  logger.on;

  // Toggles the logger state (on/off)
  logger.toggle;

  // Returns the current logger root level. Possible values: Trace, Debug, Info, Warn, Error, Off
  logger.getLevel;

  // Sets the logger root log level. Possible values: Trace, Debug, Info, Warn, Error, Off
  logger.setLevel('Info');

  // Logs a message with the specified log level.
  // If log level is not specified, the root log level will be used.
  const optionalLogMessageLogLevel = 'Debug';
  logger.log('This is a test message', optionalLogMessageLogLevel);

  //Log uploading works only after successful authentication
  // Upload the log file according to client settings
  clientAPI.executeAction('/AssetWorkManager/Actions/Logger/Upload.action');

  // Upload log file according to own settings
  logger.uploadLogFile(backendURL, applicationID);
}

Configuring Logging in SAP Mobile Services

Your application must be configured in the SAP mobile service cockpit to accept the uploaded logs. To do that:

  1. Login to mobile services cockpit, navigate to > Mobile Client Log Upload.
  2. Enable Log Upload and click Save. For more information, see Defining Client Log Policy.

Viewing the Logs

  1. To view the logs, open mobile services cockpit, navigate to Mobile Applications > Native/MDK > > Mobile Client Log Upload > Log Files.
  2. A log file is created for all the uploaded files, including all levels, errors and fatalities. You can also select and download log files for further examination.

Tutorial

Upload Logs from a Mobile Development Kit App


Last update: January 3, 2024