Collecting Information & Logging
- how-to
Logging with the Python SDK.
The Python SDK allows logging via the standard logging
module.
Enabling Logging
From version 4.0.2 of the Python SDK onwards, logging is configured with configure_logging()
— as in this example, below — and not through the environmental variable.
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://localhost',
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())
1 | In Python SDK versions 4.0.0 and 4.0.1, configure_logging() is not implemented and logging must be set with an environmental variable, as follows:
GNU/Linux and Mac
Windows
Where |
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, 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.
-
You may wish to read more on Log Redaction in the Server docs.