-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathlogging_example.py
More file actions
39 lines (31 loc) · 1.25 KB
/
logging_example.py
File metadata and controls
39 lines (31 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# logging implemented in 4.0.2
# tag::logging[]
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)
cluster = Cluster.connect('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())
# end::logging[]