Skip to content

Commit 77f907e

Browse files
authored
Merge pull request #22 from pythoncanarias/log-configuration
Log all configuration at bot start
2 parents acc0d7c + fffb148 commit 77f907e

2 files changed

Lines changed: 34 additions & 5 deletions

File tree

bot.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ def main():
8383
format='%(asctime)s [%(name)s] %(levelname)s: %(message)s',
8484
)
8585
logger.info('Starting bot...')
86-
logger.info(f'- Log level is {config.LOG_LEVEL}')
87-
logger.info(f'- Poll interval is {config.POLL_INTERVAL}')
86+
config.log(logger.info)
8887
updater = Updater(config.TELEGRAM_BOT_TOKEN)
8988
dp = updater.dispatcher
9089

config.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,38 @@
1-
from prettyconf import config
1+
from typing import NamedTuple, Any
2+
3+
from prettyconf import config as _config
4+
5+
6+
_config_registry = []
7+
8+
class _ConfigItem(NamedTuple):
9+
name: str
10+
value: Any
11+
suppress_log: bool = False
12+
13+
def log(self, logger_method, indent=False):
14+
value = "***PRIVATE***" if self.suppress_log else self.value
15+
indentation = ' ' if indent else ''
16+
logger_method(f"{indentation}{self.name} = {value}")
17+
18+
19+
def config(item, cast=lambda v: v, suppress_log=False, **kwargs):
20+
value = _config(item, cast, **kwargs)
21+
global _config_registry
22+
_config_registry.append(_ConfigItem(item, value, suppress_log))
23+
return value
24+
25+
26+
def log(logger_method):
27+
logger_method("Bot configuration:")
28+
for config_item in _config_registry:
29+
config_item.log(logger_method, indent=True)
30+
231

332
TELEGRAM_BOT_TOKEN = config(
433
"TELEGRAM_BOT_TOKEN",
5-
default="put here the token of your bot"
34+
default="put here the token of your bot",
35+
suppress_log=True
636
)
737

838
# How likely is the bot to be triggered by one of the patterns it recognises.
@@ -17,7 +47,7 @@
1747
POLL_INTERVAL = config('POLL_INTERVAL', int, default=3)
1848

1949
# Bot message for start command
20-
BOT_GREETING = "Hi! I'm a friendly, sligthly psychopath robot"
50+
BOT_GREETING = config('BOT_GREETING', default="Hi! I'm a friendly, slightly psychopath robot")
2151

2252
# A username longer than this will be considered non-human
2353
# - Allowed values: An integer larger than 1

0 commit comments

Comments
 (0)