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`.
## 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.

16
bot.py
View File

@ -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 = {

View File

@ -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]

View File

@ -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'