Skip to content

Commit f0c7611

Browse files
authored
Enable logging (#118)
* logging setup * add logging for http calls * fix linter * read logging configuration from config file * fix linter * using package name
1 parent 19cef7b commit f0c7611

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

railib/api.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import time
2020
import re
2121
import io
22+
import logging
2223
from enum import Enum, unique
2324
from typing import List, Union
2425
from requests_toolbelt import multipart
@@ -34,6 +35,9 @@
3435
PATH_USER = "/users"
3536
PATH_OAUTH_CLIENT = "/oauth-clients"
3637

38+
# logger
39+
logger = logging.getLogger(__package__)
40+
3741

3842
# Engine sizes
3943
@unique
@@ -887,11 +891,13 @@ def exec(
887891
readonly: bool = True,
888892
**kwargs
889893
) -> TransactionAsyncResponse:
894+
logger.info('exec: database %s engine %s readonly %s' % (database, engine, readonly))
890895
start_time = time.time()
891896
txn = exec_async(ctx, database, engine, command, inputs=inputs, readonly=readonly)
892897
# in case of if short-path, return results directly, no need to poll for
893898
# state
894899
if not (txn.results is None):
900+
logger.info('transaction id: %s' % txn.transaction["id"])
895901
return txn
896902

897903
rsp = TransactionAsyncResponse()
@@ -903,6 +909,8 @@ def exec(
903909
start_time=start_time,
904910
)
905911

912+
logger.info('transaction id: %s' % txn["id"])
913+
906914
rsp.transaction = get_transaction(ctx, txn["id"], **kwargs)
907915
rsp.metadata = get_transaction_metadata(ctx, txn["id"], **kwargs)
908916
rsp.problems = get_transaction_problems(ctx, txn["id"], **kwargs)

railib/rest.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""Low level HTTP interface to the RelationalAI REST API."""
1616

1717
import json
18+
import logging
1819
from os import path
1920
from urllib.parse import urlencode, urlsplit, quote
2021
from urllib.request import Request, urlopen
@@ -38,6 +39,9 @@
3839
EXPIRES_IN_KEY = "expires_in"
3940
SCOPE = "scope"
4041

42+
# logger
43+
logger = logging.getLogger(__package__)
44+
4145

4246
# Context contains the state required to make rAI REST API calls.
4347
class Context(object):
@@ -215,7 +219,14 @@ def request(ctx: Context, method: str, url: str, headers={}, data=None, **kwargs
215219
req = Request(method=method, url=url, headers=headers, data=data)
216220
req = _authenticate(ctx, req)
217221
_print_request(req)
218-
return urlopen(req)
222+
rsp = urlopen(req)
223+
224+
# logging
225+
content_type = headers["Content-Type"] if "Content-Type" in headers else ""
226+
agent = headers["User-Agent"] if "User-Agent" in headers else ""
227+
request_id = rsp.headers["X-Request-ID"] if "X-Request-ID" in rsp.headers else ""
228+
logger.debug(f"{rsp._method} HTTP/{rsp.version} {content_type} {rsp.url} {rsp.status} {agent} {request_id}")
229+
return rsp
219230

220231

221232
def delete(ctx: Context, url: str, data, headers={}, **kwargs):

test/logger.config

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[loggers]
2+
keys=root,railib
3+
4+
[handlers]
5+
keys=stream_handler
6+
7+
[formatters]
8+
keys=fmt
9+
10+
[logger_root]
11+
level=INFO
12+
handlers=stream_handler
13+
14+
[logger_railib]
15+
level=DEBUG
16+
handlers=stream_handler
17+
qualname=railib
18+
19+
[handler_stream_handler]
20+
class=StreamHandler
21+
level=DEBUG
22+
formatter=fmt
23+
args=(sys.stdout,)
24+
25+
[formatter_fmt]
26+
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

test/test_integration.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import uuid
55
import tempfile
66

7+
from logging.config import fileConfig
78
from pathlib import Path
89
from railib import api, config
910

@@ -39,6 +40,9 @@
3940
engine = f"python-sdk-{suffix}"
4041
dbname = f"python-sdk-{suffix}"
4142

43+
# init "rai" logger
44+
fileConfig("./test/logger.config")
45+
4246

4347
class TestTransactionAsync(unittest.TestCase):
4448
def setUp(self):

0 commit comments

Comments
 (0)