fixed loglevel and evaluation to enable debugging

This commit is contained in:
Frank Becker 2020-02-15 18:51:02 +01:00
parent 561338fc10
commit 892c84a974
4 changed files with 41 additions and 14 deletions

View File

@ -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`. 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 ## Module API
Just write a python file with desired command name and place it in modules. See current modules for 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. 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: Functions:
* matrix_start - Called once on startup * 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. 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. 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. If you write a new module, please make a PR if it's something useful for others.

16
bot.py
View File

@ -42,13 +42,12 @@ class Bot:
self.pollcount = 0 self.pollcount = 0
self.poll_task = None self.poll_task = None
self.owners = [] self.owners = []
self.debug = os.getenv("DEBUG") self.debug = os.getenv("DEBUG", "false").lower() == "true"
self.logger = None
self.initializeLogger() self.initialize_logger()
self.logger = logging.getLogger("hemppa")
self.logger.debug("Initialized")
def initializeLogger(self): def initialize_logger(self):
if os.path.exists('config/logging.yml'): if os.path.exists('config/logging.yml'):
with open('config/logging.yml') as f: with open('config/logging.yml') as f:
@ -58,10 +57,13 @@ class Bot:
log_format = '%(levelname)s - %(name)s - %(message)s' log_format = '%(levelname)s - %(name)s - %(message)s'
logging.basicConfig(format=log_format) logging.basicConfig(format=log_format)
self.logger = logging.getLogger("hemppa")
if self.debug: if self.debug:
logging.root.setLevel(logging.DEBUG) logging.root.setLevel(logging.DEBUG)
else: self.logger.info("enabled debugging")
logging.root.setLevel(logging.INFO)
self.logger.debug("Logger initialized")
async def send_text(self, room, body): async def send_text(self, room, body):
msg = { msg = {

View File

@ -1,18 +1,15 @@
version: 1 version: 1
formatters: formatters:
hemppa: hemppa:
format: '%(levelname)s - %(name)s - %(message)s' format: '%(asctime)s - %(levelname)s - %(name)s - %(message)s'
handlers: handlers:
console: console:
class: logging.StreamHandler class: logging.StreamHandler
level: INFO
formatter: hemppa formatter: hemppa
stream: ext://sys.stdout stream: ext://sys.stdout
loggers: loggers:
hemppa: hemppa:
level: INFO level: NOTSET
handlers: [console]
propagate: no
root: root:
level: DEBUG level: INFO
handlers: [console] handlers: [console]

View File

@ -7,7 +7,8 @@ class MatrixModule(BotModule):
args.pop(0) args.pop(0)
# Echo what they said back # 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)) await bot.send_text(room, ' '.join(args))
def help(self): def help(self):
return ('Echoes back what user has said') return 'Echoes back what user has said'