Using Logs for Troubleshooting

    +

    Description — Couchbase Lite on Objective-C — Using Logs for Troubleshooting
    Related Content — Troubleshooting Queries | Decoding Crash Logs

    Constraints
    • The retrieval of logs from the device is out of scope of this feature.

    • This content applies to the post 2.5 versions. If you are using a Couchbase Lite release prior to 2.5 see Deprecated functionality

    Introduction

    Couchbase Lite provides a robust Logging API [1] — see: API References for CBLLog, CBLFileLogger and CBLLogFileConfiguration — which make debugging and troubleshooting easier during development and in production. It delivers flexibility in terms of how logs are generated and retained, whilst also maintaining the level of logging required by Couchbase Support for investigation of issues.

    Log output is split into the following streams:

    • File based logging

      Here logs are written to separate log files filtered by log level, with each log level supporting individual retention policies.

    • Console based logging

      You can independently configure and control console logs, which provides a convenient method of accessing diagnostic information during debugging scenarios. With console logging, you can fine-tune diagnostic output to suit specific debug scenarios, without interfering with any logging required by Couchbase Support for the investigation of issues.

    • Custom logging

      For greater flexibility you can implement a custom logging class using the CBLLogger interface.

    In all instances, you control what is logged and at what level using the CBLLog class.

    Console based logging

    Console based logging is often used to facilitate troubleshooting during development.

    Console logs are your go-to resource for diagnostic information. You can easily fine-tune their diagnostic content to meet the needs of a particular debugging scenario, perhaps by increasing the verbosity and-or choosing to focus on messages from a specific domain; to better focus on the problem area.

    Changes to console logging are independent of file logging, so you can make change without compromising any files logging streams. It is enabled by default. To change default settings use database’s CBLLog method to set the required values — see Example 1

    You will primarily use CBLLog.console and CBLConsoleLogger to control console logging.

    Example 1. Change Console Logging Settings

    This example enables and defines console-based logging settings.

    CBLDatabase.log.console.domains = kCBLLogDomainAll; (1)
    CBLDatabase.log.console.level = kCBLLogLevelVerbose; (2)
    1 Define the required domain ; here we turn on logging for all available domains — see: CBLLog.console.domains and enum CBLLogDomain
    2 Here we turn on the most verbose log level — see: CBLLog.console.level and enum CBLLogLevel.
    To disable logging for the specified CBLLogDomain set the CBLLogLevel to None.

    File based logging

    File based logging is disabled by default —  see: Example 2 for how to enable it.

    You will primarily use CBLLog.file and CBLFileLogger to control file-based logging.

    Formats

    Available file based logging formats:

    • Binary — most efficient for storage and performance. It is the default for file based logging.

      Use this format and a decoder, such as cbl-log, to view them — see: Decoding binary logs.

    • Plaintext

    Configuration

    As with console logging you can set the log level — see: the CBLFileLogger class.

    With file based logging you can also use the CBLLogFileConfiguration class’s properties to specify the:

    • log file path to the directory to store the log file Once this limit is exceeded a new log file is started.

    • log file format
      The default is binary. You can over ride that where necessary and output a plain text log.

    • maximum number of rotated log files to keep

    • maximum size of the log file (bytes).

    Example 2. Enabling file logging

    This example enables file based logging.

    NSString* tempFolder = [NSTemporaryDirectory() stringByAppendingPathComponent:@"cbllog"];
    CBLLogFileConfiguration* config = [[CBLLogFileConfiguration alloc] initWithDirectory:tempFolder]; (1)
    config.maxRotateCount = 2; (2)
    config.maxSize = 1024; (3)
    config.usePlainText = YES; (4)
    [CBLDatabase.log.file setConfig:config];
    [CBLDatabase.log.file setLevel: kCBLLogLevelInfo]; (5)
    1 Set the log file directory
    2 Here we change the max rotation count from the default (1) to 5.
    Note this means six files may exist at any one time; the five rotated log files, plus the active log file
    3 Here we set the maximum size (bytes) for our log file
    4 Here we select the binary log format (included for reference only as this is the default)
    5 Here we increase the log output level from the default (warnings) to info — see: CBLLog.file.level: LogLevel

    Note that the use of CBLDatabase.setLogLevel() is now deprecated. Further, you can no longer set a log level for a specific domain.

    Custom logging

    Couchbase Lite allows for the registration of a callback function to receive Couchbase Lite log messages, which may be logged using any external logging framework.

    To do this, apps must implement the CBLLogger interface — see Example 3 — and enable custom logging using CBLLog.custom — see Example 4.

    Example 3. Implementing logger interface

    Here we introduce the code that implements the CBLLogger interface.

    @interface LogTestLogger : NSObject<CBLLogger>
    
    // set the log level
    @property (nonatomic) CBLLogLevel level;
    
    @end
    
    @implementation LogTestLogger
    
    @synthesize level=_level;
    
    - (void) logWithLevel: (CBLLogLevel)level domain: (CBLLogDomain)domain message: (NSString*)message {
        // handle the message, for example piping it to
        // a third party framework
    }
    
    @end
    Example 4. Enabling custom logging

    This example show how to enable the custom logger from Example 3.

    [CBLDatabase.log setCustom:[[LogTestLogger alloc] initWithLogLevel: kCBLLogLevelWarning]];
    1 Here we set the custom logger with a level of 'warning'.

    Decoding binary logs

    You can use the cbl-log tool to decode binary log files — see Example 5.

    Example 5. Using the cbl-log tool
    • macOS

    • CentOS

    • Windows

    Download the cbl-log tool using wget.

    wget https://packages.couchbase.com/releases/couchbase-lite-log/2.8.0/couchbase-lite-log-2.8.0-macos.zip

    Navigate to the bin directory and run the cbl-log executable.

    $ ./cbl-log logcat LOGFILE <OUTPUT_PATH>

    Download the cbl-log tool using wget.

    wget https://packages.couchbase.com/releases/couchbase-lite-log/2.8.0/couchbase-lite-log-2.8.0-centos.zip

    Navigate to the bin directory and run the cbl-log executable.

    cbl-log logcat LOGFILE <OUTPUT_PATH>

    Download the cbl-log tool using PowerShell.

    Invoke-WebRequest https://packages.couchbase.com/releases/couchbase-lite-log/2.8.0/couchbase-lite-log-2.8.0-windows.zip -OutFile couchbase-lite-log-2.8.0-windows.zip

    Run the cbl-log executable.

    $ .\cbl-log.exe logcat LOGFILE <OUTPUT_PATH>

    Logging functionality prior to Release 2.5

    Deprecation
    The pre-Couchbase Lite 2.5 logging functionality is deprecated. It was superseded by an enhanced Logging API in that release. This information is included for completeness only.

    The log messages are split into different domains (LogDomains) which can be tuned to different log levels. The following example enables verbose logging for the replicator and query domains.

    Example 6. Enable verbose logging on a domain
    [CBLDatabase setLogLevel: kCBLLogLevelVerbose domain: kCBLLogDomainReplicator];
    [CBLDatabase setLogLevel: kCBLLogLevelVerbose domain: kCBLLogDomainQuery];
    Learn more . . .
    Dive Deeper . . .

    1. From version 2.5