Skip to content

Commit

Permalink
Nicer logging - using the logger object
Browse files Browse the repository at this point in the history
One more step on the way to #15
  • Loading branch information
kolayne committed Jan 17, 2023
1 parent f84572e commit 4380987
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
5 changes: 5 additions & 0 deletions Rimokon/config.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ emergency_shutdown_command = 'YOUR_COMMAND_HERE'
emergency_shutdown_public = True


# Optional: set logging level
#import logging
#logging.getLogger('Rimokon').setLevel(logging.DEBUG)


# The following parameters `actions` and `aliases` define the actions your Rimokon instance will be able
# to perform. To enable them, import the action functions from plugins and add them to the following
# dictionaries.
Expand Down
21 changes: 14 additions & 7 deletions Rimokon/import_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@
actions, aliases


logger = logging.getLogger('Rimokon')
_handler = logging.StreamHandler()
_handler.setFormatter(logging.Formatter(
fmt='%(asctime)s %(levelname)s\t%(message)s', datefmt="%d.%m.%Y %H:%M:%S"))
logger.addHandler(_handler)
del _handler # Make unimportable


def die(*args):
# TODO: use logger object
logging.critical(*args)
logger.critical(*args)
exit(1)

def canonicalize_key_or_die(k_: str) -> str:
Expand All @@ -32,7 +40,7 @@ def canonicalize_key_or_die(k_: str) -> str:
updated_actions = {}

for k_, v in actions.items():
logging.debug('Processing action %s', repr(k_))
logger.debug('Processing action %s', repr(k_))
k = canonicalize_key_or_die(k_)
if k in updated_actions.keys():
die('Attempt to redefine action %s (in `config.actions`)', repr(k))
Expand All @@ -51,7 +59,7 @@ def new_action_func(bot, msg, _rest):
# symbol from the message text.
new_rest = prepended_string + cmd_get_rest(msg.text, False)

logging.debug('String alias triggered. Message text: %s; prepended string: %s; '
logger.debug('String alias triggered. Message text: %s; prepended string: %s; '
'resulting (new) rest: %s',
repr(msg.text), repr(prepended_string), repr(new_rest))

Expand All @@ -63,7 +71,7 @@ def new_action_func(bot, msg, _rest):
updated_aliases = {}

for k_, v_ in aliases.items():
logging.debug('Processing alias %s', repr(k_))
logger.debug('Processing alias %s', repr(k_))
k = canonicalize_key_or_die(k_)
if k in updated_aliases.keys():
die('Attempt to redefine alias %s (in `config.aliases`)', repr(k))
Expand All @@ -90,6 +98,5 @@ def new_action_func(bot, msg, _rest):
del actions, aliases, updated_actions, updated_aliases


# When executed as a script, behave as a config checker
if __name__ == "__main__":
print("Config file imported successfully")
# When executed as a script, behave as a config checker. Otherwise just notify of success
(print if __name__ == "__main__" else logger.info)("Successfully imported the config file")

0 comments on commit 4380987

Please sign in to comment.