From 892c84a974a9c08f2b147c5373cad3c5803b81f8 Mon Sep 17 00:00:00 2001 From: Frank Becker Date: Sat, 15 Feb 2020 18:51:02 +0100 Subject: [PATCH] fixed loglevel and evaluation to enable debugging --- README.md | 27 +++++++++++++++++++++++++++ bot.py | 16 +++++++++------- config/logging.yml | 9 +++------ modules/echo.py | 3 ++- 4 files changed, 41 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 73b2cc0..3b709e0 100644 --- a/README.md +++ b/README.md @@ -242,11 +242,34 @@ __*ATTENTION:*__ Don't include bot itself in `BOT_OWNERS` if cron or any other m To enable debugging for the root logger set `DEBUG=True`. +## Logging + +Uses [python logging facility](https://docs.python.org/3/library/logging.html) to print information to the console. Customize it to your needs editing `config/logging.yml`. +See [logging.config documentation](https://docs.python.org/3/library/logging.config.html) for further information. + ## Module API Just write a python file with desired command name and place it in modules. See current modules for examples. No need to register it anywhere else. +*Simple skeleton for a bot module:* +```python + +class MatrixModule(BotModule): + + async def matrix_message(self, bot, room, event): + args = event.body.split() + args.pop(0) + + # Echo what they said back + self.logger.debug(f"room: {room.name} sender: {event.sender} wants an echo") + await bot.send_text(room, ' '.join(args)) + + def help(self): + return 'Echoes back what user has said' + +``` + Functions: * matrix_start - Called once on startup @@ -259,6 +282,10 @@ Functions: You only need to implement the ones you need. See existing bots for examples. +Logging: + +Use `self.logger` in your module to print information to the console. + Module settings are stored in Matrix account data. If you write a new module, please make a PR if it's something useful for others. diff --git a/bot.py b/bot.py index daeabba..6d1d029 100755 --- a/bot.py +++ b/bot.py @@ -42,13 +42,12 @@ class Bot: self.pollcount = 0 self.poll_task = None self.owners = [] - self.debug = os.getenv("DEBUG") + self.debug = os.getenv("DEBUG", "false").lower() == "true" + self.logger = None - self.initializeLogger() - self.logger = logging.getLogger("hemppa") - self.logger.debug("Initialized") + self.initialize_logger() - def initializeLogger(self): + def initialize_logger(self): if os.path.exists('config/logging.yml'): with open('config/logging.yml') as f: @@ -58,10 +57,13 @@ class Bot: log_format = '%(levelname)s - %(name)s - %(message)s' logging.basicConfig(format=log_format) + self.logger = logging.getLogger("hemppa") + if self.debug: logging.root.setLevel(logging.DEBUG) - else: - logging.root.setLevel(logging.INFO) + self.logger.info("enabled debugging") + + self.logger.debug("Logger initialized") async def send_text(self, room, body): msg = { diff --git a/config/logging.yml b/config/logging.yml index 31c7143..a5fd89b 100644 --- a/config/logging.yml +++ b/config/logging.yml @@ -1,18 +1,15 @@ version: 1 formatters: hemppa: - format: '%(levelname)s - %(name)s - %(message)s' + format: '%(asctime)s - %(levelname)s - %(name)s - %(message)s' handlers: console: class: logging.StreamHandler - level: INFO formatter: hemppa stream: ext://sys.stdout loggers: hemppa: - level: INFO - handlers: [console] - propagate: no + level: NOTSET root: - level: DEBUG + level: INFO handlers: [console] diff --git a/modules/echo.py b/modules/echo.py index 0d73581..b60e54e 100644 --- a/modules/echo.py +++ b/modules/echo.py @@ -7,7 +7,8 @@ class MatrixModule(BotModule): args.pop(0) # Echo what they said back + self.logger.debug(f"room: {room.name} sender: {event.sender} wants an echo") await bot.send_text(room, ' '.join(args)) def help(self): - return ('Echoes back what user has said') + return 'Echoes back what user has said'