Log Upload
SAPcpmsLogUploader
Use the SAPcpmsLogUploader
component to upload persisted logs to SAP Mobile Services.
SAP Mobile Services configuration
To use this component you must enable Log Upload functionality on the SAPcpms, see Defining Client Log Policies
If you define your application using the SAP BTP SDK for iOS Assistant, and select Upload Logs from the Optional Configurations screen, the log upload function is enabled on SAP Mobile Services and implemented in the generated application. For additional information, see Uploading and Viewing Client Logs.
Usage
To use the SAPcpmsLogUploader
component:
- Attach the handler to the
Logger
- Create logs
- Upload your logs and check for errors
Attaching the handler to the Logger
Applications that want to use the log upload feature must register a specific handler with the root logger as soon as possible in the app’s lifecycle:
// AppDelegate's didFinishLaunchingWithOptions:
SAPcpmsLogUploader.attachToRootLogger()
Creating logs
You can use the Logger
component located in the SAPCommon
framework to set the loglevel and create logs:
Logger
Uploading logs
There are two methods by whcih you can upload logs to SAP Mobile Services:
1) (recommended) If you are connecting to SAP Mobile Services directly, you can use SAPcpmsSettingsParameters
to upload logs. This component uses the backendURL
and the applicationID
parameter from SAPcpmsSettingsParameters
to be able to concatenate the SAPcpms logupload URL. The SAPURLSession
must be configured according to your authentication settings.
let settingsParameters = SAPcpmsSettingsParameters(backendURL: <#yourBaseUrl#>, applicationID: <#yourApplicationID#>)
SAPcpmsLogUploader.uploadLogs(sapURLSession: <#your SAPURLSession#>, settingsParameters: <#your SAPcpmsSettingsParameters instance#>) { error in
// error handling ...
}
After successfully uploading the logs, the error in the completion block should be nil
and the uploaded logs are removed from persistence.
2) If you are not connecting directly to SAP Mobile Services, pass your custom URL to the uploadLogs()
method. The SAPURLSession
must be configured according to your authentication settings.
SAPcpmsLogUploader.uploadLogs(sapURLSession: <#your SAPURLSession#>, endpoint: <#your URL#>){ error in
// error handling ...
}
After successfully uploading the logs, the error in the completion block should be nil
and the uploaded logs are removed from persistence.
Note: When uploading large log files there is a chance that the URL request times out. In such cases consider increasing the timeout on the SAPURLSession
:
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = <#Increased timeout#>
configuration.timeoutIntervalForResource = <#Increased timeout#>
let sapURLSession = SAPURLSession(configuration: configuration)
Limit amount of logs
You can set the maximum number and size for logfiles on the attachToRootLogger()
method:
SAPcpmsLogUploader.attachToRootLogger(maxFileSize: <#max bytes per file#>, maxBackups: <#max number of files#>)
You can also set expiration for logfiles:
SAPcpmsLogUploader.logExpiration = <#number of days#>
SAPcpmsLogUploader ignores the files which contain only older logs then the value of logExpiration.
Log format
The SAPcpmsLogUploader uses the SAP log format, which makes the logs available for server upload. This format can not be changed. Format: #Date time#Severity#CorrelationId#Source#Location#Message#
The message can not contain # characters, these are replaced with _ characters. The new line character \n is escaped to \n in the message.
Best practices
When logs are collected it is highly recommended that you:
- Allow users to opt-in to log upload
- Include an information screen in the app that explains what the logs contain, or allow users to see the logs before uploading
LogUploader component Logger ID
This component uses the following name prefix for logging: ‘SAP.Foundation.LogUploader’
-
Declaration
Swift
extension Logger
-
LogUploader can be used to upload persisted logs to SAPcpms. Applications which want to use the log upload feature must attach a handler to the Logger using ‘attachToRootLogger()’ method as soon as possible in the app’s lifecycle.
AppDelegate’s didFinishLaunchingWithOptions:
SAPcpmsLogUploader.attachToRootLogger()
Then, when you want to upload the logs:
See moreSAPcpmsLogUploader.uploadLogs(sapURLSession: <#urlSession#>, settingsParameters: <#settingsParameters#>) { error in // error handling }
Declaration
Swift
public class SAPcpmsLogUploader
-
Enum covering all errors occuring in the LogUploader.
See moreDeclaration
Swift
public enum SAPcpmsLogUploaderError : Error
extension SAPcpmsLogUploaderError: SAPError