Logging

  • how-to
    +
    Logging with the Python SDK.

    The Python SDK allows logging via the standard logging module.

    Enabling Logging

    Python Logging Module
    import logging
    import traceback
    from datetime import timedelta
    
    import couchbase
    from couchbase.auth import PasswordAuthenticator
    from couchbase.cluster import Cluster
    from couchbase.diagnostics import ServiceType
    from couchbase.exceptions import CouchbaseException
    from couchbase.options import ClusterOptions, WaitUntilReadyOptions
    
    # output log messages to example.log
    logging.basicConfig(filename='example.log',
                        filemode='w', 
                        level=logging.DEBUG,
                        format='%(levelname)s::%(asctime)s::%(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    
    logger = logging.getLogger()
    couchbase.configure_logging(logger.name, level=logger.level) (1)
    
    cluster = Cluster('couchbase://your-ip',
                      ClusterOptions(PasswordAuthenticator("Administrator", "password")))
    
    cluster.wait_until_ready(timedelta(seconds=3),
                             WaitUntilReadyOptions(service_types=[ServiceType.KeyValue, ServiceType.Query]))
    
    logger.info('Cluster ready.')
    
    bucket = cluster.bucket("travel-sample")
    coll = bucket.scope('inventory').collection('airline')
    try:
        coll.get('not-a-key')
    except CouchbaseException:
        logger.error(traceback.format_exc())

    Environmental Settings

    Only one logger can be created. Either use PYCBC_LOG_LEVEL to create a console logger or configure_logging as mentioned above.

    In the command line environment, the PYCBC_LOG_LEVEL variable is set as follows:

    GNU/Linux and Mac
    export PYCBC_LOG_LEVEL=<log-level>
    Windows
    set PYCBC_LOG_LEVEL=<log-level>

    Version 4.3.3 of the SDK introduces the PYCBC_LOG_FILE variable that can be used in conjunction with PYCBC_LOG_LEVEL. Set PYCBC_LOG_FILE to a filename in order to have the log output to a file (instead of stdout).

    In the command line environment, the PYCBC_LOG_FILE variable is set as follows:

    GNU/Linux and Mac
    export PYCBC_LOG_FILE=<filename>
    Windows
    set PYCBC_LOG_FILE=<filename>

    Log Levels

    You can increase the log level for greater verbosity (more information) in the logs:

    • off — disables all logging, which is normally set by default.

    • critical — important functionality not working.

    • error — error messages.

    • warn — error notifications.

    • info — useful notices, not often.

    • debug — diagnostic information, minimum level required to investigate problems.

    • trace — detailed diagnostic information, often required to investigate problems.

    Log Redaction

    Redacting logs is a two-stage process. If you want to redact client logs (for example before handing them off to the Couchbase Support team) you first need to enable log redaction in your application. This is done through the ClusterOptions, setting log_redaction to True.

    Once the SDK writes the logs with the tags to a file, you can then use the cblogredaction tool to obfuscate the log.