Skip to content

Log Configuration

Log Initialization Configuration

    // Enable logger
    FTLoggerConfig *loggerConfig = [[FTLoggerConfig alloc]init];
    loggerConfig.enableCustomLog = YES;
    loggerConfig.enableLinkRumData = YES;
    loggerConfig.logLevelFilter = @[@(FTStatusError),@(FTStatusCritical)];
    loggerConfig.discardType = FTDiscardOldest;
    [[FTMobileAgent sharedInstance] startLoggerWithConfigOptions:loggerConfig];
    let loggerConfig = FTLoggerConfig()
    loggerConfig.enableCustomLog = true
    loggerConfig.enableLinkRumData = true
    loggerConfig.logLevelFilter = [NSNumber(value: FTLogStatus.statusError.rawValue),NSNumber(value: FTLogStatus.statusCritical.rawValue)] // loggerConfig.logLevelFilter = [2,3]
    loggerConfig.discardType = .discardOldest
    FTMobileAgent.sharedInstance().startLogger(withConfigOptions: loggerConfig)
Property Type Required Description
logSamplingRate float No Log sampling rate, range [0,100], decimals supported, default 100. 0.1 means 0.1%, 1 means 1%. Supported since SDK 1.6.8.
sampleRate int No Deprecated since SDK 1.6.8, retained for compatibility. Integer percentage sampling rate, range [0,100], default 100. Use logSamplingRate instead.
enableWebViewLog BOOL No SDK 1.6.8+: receives logs generated by the Browser Logs SDK in WebView, default NO, independent of enableCustomLog. The web side still needs to initialize the log SDK, see WebView Logs
enableCustomLog BOOL No Whether to upload custom logs. Default NO
printCustomLogToConsole BOOL No Whether to output custom logs to the console. Default NO, custom log output format
logLevelFilter NSArray No Set log level filtering; not set by default. Example: 1. To collect logs at Info and Error levels, set to @[@(FTStatusInfo),@(FTStatusError)] or @[@0,@1]; 2. To collect logs including custom levels, for example, to collect "customLevel" and Error, set to @[@"customLevel",@(FTStatusError)]. SDK 1.5.16 and later supports filtering custom levels
enableLinkRumData BOOL No Whether to associate with RUM data. Default NO
globalContext NSDictionary No Add custom log tags. For adding rules, see here
logCacheLimitCount int No Maximum number of log entries in local cache [1000,). The more logs, the greater the disk cache pressure. Default: 5000
discardType FTLogCacheDiscard No Set the log discard rule after the log limit is reached. Default FTDiscard. FTDiscard: when the number of log entries exceeds the maximum value (5000), discard appended data; FTDiscardOldest: when log data exceeds the maximum value, discard old data
Sampling Rate Parameter Naming

SDK 1.6.8 and later recommend using logSamplingRate, with a percentage value from 0 to 100, supporting decimals; invalid values (out of range, NaN, infinity) do not overwrite the original configuration. The old sampleRate / samplerate only supports integer percentages, has been marked as deprecated, and may be removed in future versions; when reading a decimal sampling rate through the old getter, the integer part is returned.

For the units and priority of remote sampling fields, see Remote Configuration Fields.

Logger Log Printing

When initializing the SDK with Log Configuration, configure enableCustomLog to allow adding custom logs.

Currently, log content is limited to 30 KB, and characters exceeding this limit are truncated.

Usage

//
//  FTLogger.h
//  TrueWatchSDK
/// Add a custom log at the info level
/// - Parameters:
///   - content: Log content
///   - property: Custom properties (optional)
-(void)info:(NSString *)content property:(nullable NSDictionary *)property;

/// Add a custom log at the warning level
/// - Parameters:
///   - content: Log content
///   - property: Custom properties (optional)
-(void)warning:(NSString *)content property:(nullable NSDictionary *)property;

/// Add a custom log at the error level
/// - Parameters:
///   - content: Log content
///   - property: Custom properties (optional)
-(void)error:(NSString *)content  property:(nullable NSDictionary *)property;

/// Add a custom log at the critical level
/// - Parameters:
///   - content: Log content
///   - property: Custom properties (optional)
-(void)critical:(NSString *)content property:(nullable NSDictionary *)property;

/// Add a custom log at the ok level
/// - Parameters:
///   - content: Log content
///   - property: Custom properties (optional)
-(void)ok:(NSString *)content property:(nullable NSDictionary *)property;

/// Add a custom log
/// - Parameters:
///   - content: Log content
///   - status: Log status level
///   - property: Custom properties (optional)
- (void)log:(NSString *)content status:(NSString *)status property:(nullable NSDictionary *)property;
open class FTLogger : NSObject, FTLoggerProtocol {}
public protocol FTLoggerProtocol : NSObjectProtocol {
/// Add a custom log at the info level
/// - Parameters:
///   - content: Log content
///   - property: Custom properties (optional)
optional func info(_ content: String, property: [AnyHashable : Any]?)

/// Add a custom log at the warning level
/// - Parameters:
///   - content: Log content
///   - property: Custom properties (optional)
optional func warning(_ content: String, property: [AnyHashable : Any]?)

/// Add a custom log at the error level
/// - Parameters:
///   - content: Log content
///   - property: Custom properties (optional)
optional func error(_ content: String, property: [AnyHashable : Any]?)

/// Add a custom log at the critical level
/// - Parameters:
///   - content: Log content
///   - property: Custom properties (optional)
optional func critical(_ content: String, property: [AnyHashable : Any]?)

/// Add a custom log at the ok level
/// - Parameters:
///   - content: Log content
///   - property: Custom properties (optional)
optional func ok(_ content: String, property: [AnyHashable : Any]?)

/// Add a custom log
/// - Parameters:
///   - content: Log content
///   - status: Log status level
///   - property: Custom properties (optional)
optional func log(_ content: String, status: String, property: [AnyHashable : Any]?)
}

Log Levels

/// Event level and status, default: FTStatusInfo
typedef NS_ENUM(NSInteger, FTLogStatus) {
    /// Info
    FTStatusInfo         = 0,
    /// Warning
    FTStatusWarning,
    /// Error
    FTStatusError,
    /// Critical
    FTStatusCritical,
    /// Recovery
    FTStatusOk,
};
/// Event level and status, default: FTStatusInfo
public enum FTLogStatus : Int, @unchecked Sendable {
    /// Info
    case statusInfo = 0
    /// Warning
    case statusWarning = 1
    /// Error
    case statusError = 2
    /// Critical
    case statusCritical = 3
    /// Recovery
    case statusOk = 4
}

Code Example

// If the SDK is not initialized successfully, adding custom logs will fail
[[FTLogger sharedInstance] info:@"test" property:@{@"custom_key":@"custom_value"}];
// If the SDK is not initialized successfully, adding custom logs will fail
FTLogger.shared().info("contentStr", property: ["custom_key":"custom_value"])

Output Custom Logs to Console

Set printCustomLogToConsole = YES to enable outputting custom logs to the console. You will see logs in the following format in the Xcode debug console:

iOS/tvOS:

2023-06-29 13:47:56.960021+0800 App[64731:44595791] [IOS APP] [INFO] content ,{K=V,...,Kn=Vn}

macOS:

2023-06-29 13:47:56.960021+0800 App[64731:44595791] [MACOS APP] [INFO] content ,{K=V,...,Kn=Vn}

2023-06-29 13:47:56.960021+0800 App[64731:44595791]: Standard prefix for os_log output;

[IOS APP] / [MACOS APP]: Prefix used to distinguish custom logs output by the SDK. The SDK selects the prefix based on the running platform:

[INFO]: Level of the custom log;

content: Custom log content;

{K=V,...,Kn=Vn}: Custom properties.