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:
|
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:
|
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:
|
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: Debug, Info, Warn, Error, Off
logger.getLevel;
// Sets the logger root log level. Possible values: 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 uploaded logs. To do that:
- From the cockpit, configure your application and select Client Policies.
- Scroll down to Log Policy and select Enable Client Log Upload.
See Defining Client Log Policy for more information.
- To view the log in the cockpit, select Analytics > Logs.
- Click Technical Logs and then select your log from the list by filtering with Client Log type. You may have to adjust the time range filter to see your log. You may select required log entries and then either view them directly in SAP mobile service cockpit or download them locally.
Tutorial¶
Upload Logs from a Mobile Development Kit App