Skip to content

Commit 2f589e1

Browse files
committed
Improved config logging
This commit adds twarc.config.ConfigProvider which is based on click_config_file.configobj_provider and stores the file path for the config file that was used. This is useful for logging. Also when --verbose is used the log will now contain the keys that are being used to talk to the API. This isn't something you would normally want in your logs, but it can be useful for debugging situations like #441 and #469.
1 parent 2fe76ed commit 2f589e1

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
lines changed

twarc/client2.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -720,13 +720,15 @@ def connect(self):
720720
self.client.close()
721721

722722
if self.auth_type == "application" and self.bearer_token:
723-
log.info('Creating HTTP session headers for app auth.')
723+
log.info('creating HTTP session headers for app auth.')
724+
auth = f"Bearer {self.bearer_token}"
725+
log.debug('authorization: %s', auth)
724726
self.client = requests.Session()
725-
self.client.headers.update(
726-
{"Authorization": f"Bearer {self.bearer_token}"}
727-
)
727+
self.client.headers.update({"Authorization": auth})
728728
elif self.auth_type == "application":
729-
log.info('Creating app auth client via OAuth2')
729+
log.info('creating app auth client via OAuth2')
730+
log.debug('client_id: %s', self.consumer_key)
731+
log.debug('client_secret: %s', self.client_secret)
730732
client = BackendApplicationClient(client_id=self.consumer_key)
731733
self.client = OAuth2Session(client=client)
732734
self.client.fetch_token(
@@ -736,6 +738,10 @@ def connect(self):
736738
)
737739
else:
738740
log.info('creating user auth client')
741+
log.debug('client_id: %s', self.consumer_key)
742+
log.debug('client_secret: %s', self.client_secret)
743+
log.debug('resource_owner_key: %s', self.access_token)
744+
log.debug('resource_owner_secret: %s', self.access_token_secret)
739745
self.client = OAuth1Session(
740746
client_key=self.consumer_key,
741747
client_secret=self.consumer_secret,

twarc/command2.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919

2020
from twarc.version import version
2121
from twarc.handshake import handshake
22+
from twarc.config import ConfigProvider
2223
from twarc.decorators import cli_api_error
2324
from twarc.expansions import ensure_flattened
2425
from click_config_file import configuration_option
2526

27+
config_provider = ConfigProvider()
2628

2729
@with_plugins(iter_entry_points('twarc.plugins'))
2830
@click.group()
@@ -42,23 +44,26 @@
4244
show_default=True,
4345
)
4446
@click.option('--log', default='twarc.log')
47+
@click.option('--verbose', is_flag=True, default=False)
4548
@click.option('--metadata/--no-metadata', default=True, show_default=True,
4649
help="Include/don't include metadata about when and how data was collected.")
47-
@configuration_option(cmd_name='twarc')
50+
@configuration_option(cmd_name='twarc', config_file_name='config', provider=config_provider)
4851
@click.pass_context
4952
def twarc2(
5053
ctx, consumer_key, consumer_secret, access_token, access_token_secret, bearer_token,
51-
log, metadata, app_auth
54+
log, metadata, app_auth, verbose
5255
):
5356
"""
5457
Collect data from the Twitter V2 API.
5558
"""
5659
logging.basicConfig(
5760
filename=log,
58-
level=logging.INFO,
61+
level=logging.DEBUG if verbose else logging.INFO,
5962
format="%(asctime)s %(levelname)s %(message)s"
6063
)
6164

65+
logging.info("using config %s", config_provider.file_path)
66+
6267
if bearer_token or (consumer_key and consumer_secret):
6368
if app_auth and (bearer_token or (consumer_key and consumer_secret)):
6469
ctx.obj = twarc.Twarc2(
@@ -103,15 +108,14 @@ def configure(ctx):
103108
"""
104109
Set up your Twitter app keys.
105110
"""
111+
112+
config_file = config_provider.file_path
113+
logging.info('creating config file: %s', config_file)
114+
106115
keys = handshake()
107116
if keys is None:
108117
raise click.ClickException("Unable to authenticate")
109118

110-
config_dir = pathlib.Path(click.get_app_dir('twarc'))
111-
if not config_dir.is_dir():
112-
config_dir.mkdir(parents=True)
113-
config_file = config_dir / 'config'
114-
115119
config = configobj.ConfigObj(unrepr=True)
116120
config.filename = config_file
117121

@@ -682,3 +686,12 @@ def _error_str(errors):
682686
def _write(results, outfile, pretty=False):
683687
indent = 2 if pretty else None
684688
click.echo(json.dumps(results, indent=indent), file=outfile)
689+
690+
"""
691+
def _get_config_file():
692+
config_dir = pathlib.Path(click.get_app_dir('twarc'))
693+
if not config_dir.is_dir():
694+
config_dir.mkdir(parents=True)
695+
config_file = config_dir / 'config'
696+
return config_file
697+
"""

twarc/config.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import logging
2+
import configobj
3+
4+
# Adapted from click_config_file.configobj_provider so that we can store the
5+
# file path that the config was loaded from in order to log it later.
6+
7+
log = logging
8+
9+
class ConfigProvider():
10+
11+
def __init__(self):
12+
self.file_path = None
13+
14+
def __call__(self, file_path, cmd_name):
15+
self.file_path = file_path
16+
return configobj.ConfigObj(file_path, unrepr=True)

twarc/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = '2.1.3'
1+
version = '2.1.4'

0 commit comments

Comments
 (0)