add basic filebased logging configuration

This commit is contained in:
Frank Becker 2020-02-09 11:38:40 +01:00
parent b40be62cb1
commit de96c3de06
3 changed files with 31 additions and 5 deletions

View File

@ -15,6 +15,7 @@ requests = "*"
igramscraper = "*"
twitterscraper = "*"
httpx = "*"
pyyaml = "==5.3"
[dev-packages]
pylint = "*"

15
bot.py
View File

@ -3,13 +3,14 @@
import asyncio
import glob
import importlib
import json
import yaml
import os
import re
import sys
import traceback
import urllib.parse
import logging
import logging.config
from importlib import reload
import requests
@ -46,14 +47,20 @@ class Bot:
self.logger.debug("Initialized")
def initializeLogger(self):
if os.path.exists('config/logging.yml'):
with open('config/logging.yml') as f:
config = yaml.load(f, Loader=yaml.Loader)
logging.config.dictConfig(config)
else:
log_format = '%(levelname)s - %(name)s - %(message)s'
logging.basicConfig(format=log_format)
if self.debug:
logging.root.setLevel(logging.DEBUG)
else:
logging.root.setLevel(logging.INFO)
logging.basicConfig(format=log_format)
async def send_text(self, room, body):
msg = {
"body": body,
@ -73,7 +80,7 @@ class Bot:
def remove_callback(self, callback):
for cb_object in bot.client.event_callbacks:
if cb_object.func == callback:
print("remove callback")
self.logger.info("remove callback")
bot.client.event_callbacks.remove(cb_object)
def get_room_by_id(self, room_id):

18
config/logging.yml Normal file
View File

@ -0,0 +1,18 @@
version: 1
formatters:
hemppa:
format: '%(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
root:
level: DEBUG
handlers: [console]