add config and filters to logging
Basic logging configuration can now be done in the config.yaml file and filter has been added to redact the access tokens from the log output. Closes issue #38
This commit is contained in:
parent
2d4476b9a3
commit
ad49c3a354
2 changed files with 45 additions and 6 deletions
|
@ -13,3 +13,25 @@ MATRIX_PNUT_TOKEN: '<AUTH_TOKEN>' # pnut.io auth token for the matrix bot
|
||||||
PNUTCLIENT_ID: '<CLIENT_ID>' # pnut.io app client ID
|
PNUTCLIENT_ID: '<CLIENT_ID>' # pnut.io app client ID
|
||||||
PNUT_APPTOKEN: '<APP TOKEN>' # pnut.io app token
|
PNUT_APPTOKEN: '<APP TOKEN>' # pnut.io app token
|
||||||
PNUT_APPKEY: '<APPSTREAM KEY>' # pnut.io app stream key
|
PNUT_APPKEY: '<APPSTREAM KEY>' # pnut.io app stream key
|
||||||
|
|
||||||
|
logging:
|
||||||
|
version: 1
|
||||||
|
formatters:
|
||||||
|
precise:
|
||||||
|
format: '%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - %(message)s'
|
||||||
|
normal:
|
||||||
|
format: '%(name)s - %(levelname)s - %(message)s'
|
||||||
|
handlers:
|
||||||
|
console:
|
||||||
|
class: logging.StreamHandler
|
||||||
|
formatter: normal
|
||||||
|
loggers:
|
||||||
|
werkzeug:
|
||||||
|
level: DEBUG
|
||||||
|
appservice:
|
||||||
|
level: DEBUG
|
||||||
|
urllib3.connectionpool:
|
||||||
|
level: DEBUG
|
||||||
|
root:
|
||||||
|
level: DEBUG
|
||||||
|
handlers: [console]
|
||||||
|
|
|
@ -2,6 +2,7 @@ import websocket
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
|
import logging.config
|
||||||
import yaml
|
import yaml
|
||||||
import json
|
import json
|
||||||
import pnutpy
|
import pnutpy
|
||||||
|
@ -9,6 +10,7 @@ import requests
|
||||||
import magic
|
import magic
|
||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
from matrix_client.api import MatrixHttpApi
|
from matrix_client.api import MatrixHttpApi
|
||||||
from matrix_client.api import MatrixError, MatrixRequestError
|
from matrix_client.api import MatrixError, MatrixRequestError
|
||||||
|
@ -22,6 +24,21 @@ logger = logging.getLogger()
|
||||||
_shutdown = threading.Event()
|
_shutdown = threading.Event()
|
||||||
_reconnect = threading.Event()
|
_reconnect = threading.Event()
|
||||||
|
|
||||||
|
class MLogFilter(logging.Filter):
|
||||||
|
|
||||||
|
ACCESS_TOKEN_RE = re.compile(r"(\?.*access(_|%5[Ff])token=)[^&]*(\s.*)$")
|
||||||
|
ACCESS_TOKEN_RE2 = re.compile(r"(\?.*access(_|%5[Ff])token=)[^&]*(.*)$")
|
||||||
|
|
||||||
|
def filter(self, record):
|
||||||
|
if record.name == "werkzeug" and len(record.args) > 0:
|
||||||
|
redacted_uri = MLogFilter.ACCESS_TOKEN_RE.sub(r"\1<redacted>\3", record.args[0])
|
||||||
|
record.args = (redacted_uri, ) + record.args[1:]
|
||||||
|
elif record.name == "urllib3.connectionpool" and len(record.args) > 3:
|
||||||
|
redacted_uri = MLogFilter.ACCESS_TOKEN_RE2.sub(r"\1<redacted>\3", record.args[4])
|
||||||
|
record.args = record.args[:4] + (redacted_uri,) + record.args[5:]
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
def new_message(msg, meta):
|
def new_message(msg, meta):
|
||||||
logger.debug("channel: " + msg.channel_id)
|
logger.debug("channel: " + msg.channel_id)
|
||||||
logger.debug("username: " + msg.user.username)
|
logger.debug("username: " + msg.user.username)
|
||||||
|
@ -368,17 +385,17 @@ if __name__ == '__main__':
|
||||||
# )
|
# )
|
||||||
args = a_parser.parse_args()
|
args = a_parser.parse_args()
|
||||||
|
|
||||||
if args.debug:
|
|
||||||
# websocket.enableTrace(True)
|
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
|
||||||
else:
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
|
||||||
|
|
||||||
configyaml = os.environ.get("CONFIG_FILE")
|
configyaml = os.environ.get("CONFIG_FILE")
|
||||||
|
|
||||||
with open(configyaml, "rb") as config_file:
|
with open(configyaml, "rb") as config_file:
|
||||||
config = yaml.load(config_file, Loader=yaml.SafeLoader)
|
config = yaml.load(config_file, Loader=yaml.SafeLoader)
|
||||||
|
|
||||||
|
# websocket.enableTrace(True)
|
||||||
|
logging.config.dictConfig(config['logging'])
|
||||||
|
redact_filter = MLogFilter()
|
||||||
|
logging.getLogger("werkzeug").addFilter(redact_filter)
|
||||||
|
logging.getLogger("urllib3.connectionpool").addFilter(redact_filter)
|
||||||
|
|
||||||
ws_url = 'wss://stream.pnut.io/v1/app?access_token='
|
ws_url = 'wss://stream.pnut.io/v1/app?access_token='
|
||||||
ws_url += config['PNUT_APPTOKEN'] + '&key=' + config['PNUT_APPKEY']
|
ws_url += config['PNUT_APPTOKEN'] + '&key=' + config['PNUT_APPKEY']
|
||||||
ws_url += '&include_raw=1'
|
ws_url += '&include_raw=1'
|
||||||
|
|
Loading…
Reference in a new issue