this is the code i've made so far:
import logging, os , json, inspect, sys
from logging import handlers
class StructuredMessage(object):
def __init__(self, **kwargs):
self.kwargs = kwargs
def __str__(self):
return '%s' % (json.dumps(self.kwargs))
def setup(self, file):
if(os.getenv('LOG_TO_FILE') == 'True'):
logging.basicConfig(filename='logs/'+file, format='%(levelname)s %(asctime)s %(message)s', filemode='a')
else:
logging.basicConfig(filename='logs/'+file, format='%(levelname)s %(asctime)s %(message)s', filemode='a')
logger = logging.getLogger()
def info(msg):
fun = StructuredMessage()
fun.setup('info.log')
logger.setLevel('INFO')
frame = ''.join(str(inspect.currentframe().f_back.f_code.co_filename))
lNo = ''.join(str(inspect.currentframe().f_back.f_code.co_firstlineno))
logging.info(StructuredMessage(msg=msg, file=frame+':'+lNo))
def error(msg):
fun = StructuredMessage()
fun.setup('exception.log')
logger.setLevel('ERROR')
frame = ''.join(str(inspect.currentframe().f_back.f_code.co_filename))
lNo = ''.join(str(inspect.currentframe().f_back.f_code.co_firstlineno))
logging.error(StructuredMessage(msg=msg, file=frame+':'+lNo))
What i'm trying to achieve is to obtain a module which when i call info function (for example) writes the message in the specified file in basicConfig.
The wrong is that when i call the error function it writes the error in the info.log file instead of the exception.log file, and info.log file contains both the messages.
The goal is to get to separate functions, info and error , that each one of them writes exclusively on the specified file.
What i'm doing wrong?