log notifications

This commit is contained in:
2018-09-11 21:02:07 +03:00
parent 0fbb8d0780
commit b29f3790c1
3 changed files with 74 additions and 17 deletions

View File

@@ -3,10 +3,16 @@ from datetime import datetime
from flask import current_app as app
import requests
import re
import json
try:
import magic
except ImportError:
pass
try:
from werkzeug.utils import secure_filename
except ImportError:
pass
try:
from urllib.request import pathname2url
from urllib.request import urlparse
@@ -14,6 +20,33 @@ except ImportError:
from urllib import pathname2url
from urlparse import urlparse
class Logger:
def __init__(self, filename):
self.filename = filename
self.init()
def info(self, message):
self.write("INFO", str(message))
def init(self):
self.info("Service started")
def warning(self, message):
self.write("WARNING", str(message))
def write(self, level, message):
with open(self.filename, 'at') as fp:
fp.write("%s\t%s\t%s\n"%(
datetime.now().isoformat(),
level,
message
))
fp.flush()
def download_url(url, filename):
try:
@@ -186,6 +219,28 @@ def path2url(path):
return pathname2url(path)
def read_config(app):
# Read config from json
config_values = json.load(open(os.getenv('FLEES_CONFIG'),'rt'))
app.config['PUBLIC_URL'] = config_values['public_url']
app.config['SITE_NAME'] = config_values['site_name']
app.config['UPLOAD_FOLDER'] = config_values['data_folder']
app.config['SHARES_FILE'] = config_values['shares_file']
if 'log_file' in config_values:
app.config['LOG_FILE'] = config_values['log_file']
else:
app.config['LOG_FILE'] = os.path.join(app.config['UPLOAD_FOLDER'], 'flees.log')
app.config['LOGGER'] = Logger(app.config['LOG_FILE'])
app.config['ZIP_FOLDER'] = config_values['zip_folder']
app.config['MAX_ZIP_SIZE'] = config_values['max_zip_size'] # megabytes
app.config['DATE_FORMAT'] = config_values['date_format']
app.config['UID'] = config_values['uid']
app.config['GID'] = config_values['gid']
app.config['DEBUG'] = config_values['debug']
app.config['VERSION_FOLDER'] = config_values['version_folder']
app.config['SESSION_COOKIE_NAME'] = secure_filename(config_values['site_name'])
return config_values
def safe_name(s):
return safe_string(s, "-_")