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
sampleRate int No Sample rate. Value range [0,100], 0 means no collection, 100 means full collection, default value is 100
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 filter, not set by default. Example: 1. To collect logs with Info and Error levels, set to @[@(FTStatusInfo),@(FTStatusError)] or @[@0,@1]; 2. To collect logs including custom levels, such as "customLevel" and Error, set to @[@"customLevel",@(FTStatusError)]. SDK 1.5.16 and above support filtering custom levels
enableLinkRumData BOOL No Whether to associate with RUM data. Default NO
globalContext NSDictionary No Add custom tags for logs. For adding rules, please refer to here
logCacheLimitCount int No Maximum number of log entries cached locally. [1000,), the larger the log, the greater the disk cache pressure, default 5000
discardType FTLogCacheDiscard No Set the log discard rule after reaching the limit. Default FTDiscard. FTDiscard discards the appended data when the number of log entries exceeds the maximum (5000); FTDiscardOldest discards the oldest data when the number of log entries exceeds the maximum
Sample Rate Parameter Naming

In SDK 1.6.6 and above, samplerate is deprecated but can still be used and maps to the same value as sampleRate. It is recommended to use sampleRate for new code; for versions below 1.6.6, continue using samplerate.

Logger Log Printing

When configuring the Log Configuration during SDK initialization, set enableCustomLog to allow custom log addition.

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

Usage

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

/// Add custom log with warning level
/// - Parameters:
///   - content: Log content
///   - property: Custom attributes (optional)
-(void)warning:(NSString *)content property:(nullable NSDictionary *)property;

/// Add custom log with error level
/// - Parameters:
///   - content: Log content
///   - property: Custom attributes (optional)
-(void)error:(NSString *)content  property:(nullable NSDictionary *)property;

/// Add custom log with critical level
/// - Parameters:
///   - content: Log content
///   - property: Custom attributes (optional)
-(void)critical:(NSString *)content property:(nullable NSDictionary *)property;

/// Add custom log with ok level
/// - Parameters:
///   - content: Log content
///   - property: Custom attributes (optional)
-(void)ok:(NSString *)content property:(nullable NSDictionary *)property;

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

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

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

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

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

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

Log Levels

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

Code Example

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

Custom Log Output 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: Content of the custom log;

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