Rework logging code

This commit is contained in:
Patrick Jentsch 2020-05-27 11:03:50 +02:00
parent 9d893db403
commit afd7469178

View File

@ -24,6 +24,13 @@ class Config:
NOPAQUE_MAIL_SUBJECT_PREFIX = '[nopaque]' NOPAQUE_MAIL_SUBJECT_PREFIX = '[nopaque]'
NOPAQUE_STORAGE = os.environ.get('NOPAQUE_STORAGE') NOPAQUE_STORAGE = os.environ.get('NOPAQUE_STORAGE')
os.makedirs('logs', exist_ok=True)
logging.basicConfig(filename='logs/nopaque.log',
format='[%(asctime)s] %(levelname)s in '
'%(name)s/%(filename)s, line %(lineno)d:'
'%(message)s',
datefmt='%Y-%m-%d %H:%M:%S', filemode='w')
@staticmethod @staticmethod
def init_app(app): def init_app(app):
pass pass
@ -38,19 +45,10 @@ class DevelopmentConfig(Config):
os.environ.get('POSTGRES_USER'), os.environ.get('POSTGRES_USER'),
os.environ.get('POSTGRES_PASSWORD'), os.environ.get('POSTGRES_PASSWORD'),
os.environ.get('POSTGRES_DB_NAME')) os.environ.get('POSTGRES_DB_NAME'))
if not os.path.isfile('logs/nopaque.log'):
file_path = os.path.join(os.getcwd(), 'logs/nopaque.log') ''' ### nopaque ### '''
log = open(file_path, 'w+') NOPAQUE_LOG_LEVEL = os.environ.get('NOPAQUE_LOG_LEVEL') or 'DEBUG'
log.close() logging.basicConfig(level=NOPAQUE_LOG_LEVEL)
if os.environ.get('FLASK_CONFIG') == 'development':
log_format = ("%(asctime)s - %(levelname)s - %(name)s - "
"%(filename)s - %(lineno)d - %(message)s")
logging.basicConfig(filename='logs/nopaque.log', level='WARNING',
format=log_format, datefmt='%Y-%m-%d %H:%M:%S',
filemode='w')
logger = logging.getLogger(__name__)
logger.warning('Logging has started with level WARNING.'
' From development config.')
class TestingConfig(Config): class TestingConfig(Config):
@ -71,24 +69,15 @@ class ProductionConfig(Config):
os.environ.get('POSTGRES_PASSWORD'), os.environ.get('POSTGRES_PASSWORD'),
os.environ.get('POSTGRES_DB_NAME') os.environ.get('POSTGRES_DB_NAME')
) )
if not os.path.isfile('logs/nopaque.log'):
file_path = os.path.join(os.getcwd(), 'logs/nopaque.log') ''' ### nopaque ### '''
log = open(file_path, 'w+') NOPAQUE_LOG_LEVEL = os.environ.get('NOPAQUE_LOG_LEVEL') or 'ERROR'
log.close() logging.basicConfig(level=NOPAQUE_LOG_LEVEL)
if os.environ.get('FLASK_CONFIG') == 'production':
log_format = ("%(asctime)s - %(levelname)s - %(name)s - "
"%(filename)s - %(lineno)d - %(message)s")
logging.basicConfig(filename='logs/nopaque.log', level='ERROR',
format=log_format, datefmt='%Y-%m-%d %H:%M:%S',
filemode='w')
logger = logging.getLogger(__name__)
logger.error('Logging has started with level ERROR.'
' From production config.')
config = { config = {
'development': DevelopmentConfig, 'development': DevelopmentConfig,
'testing': TestingConfig, 'testing': TestingConfig,
'production': ProductionConfig, 'production': ProductionConfig,
'default': DevelopmentConfig 'default': DevelopmentConfig,
} }