How to create a new log file every time the application runs using logger.config?
how i can create new info.log file each time app runs. Let me know the cahnges i need to make in this config. I know we have to use doRollover or rotate but how to implement in this config not getting any idea. Any help will be appreciated and thanks in advance.
file-1. <<log.py >>
import configparser
import logging.config
code_dir_path = os.path.abspath(os.path.dirname(__file__))
log = logging.getLogger(__name__)
logging_conf = \
{u'disable_existing_loggers': False,
u'formatters': {u'basic': {
u'format': u'%(asctime)s - %(name)s - %(threadName)s : %(levelname)s : %(module)s : %(lineno)d : %(message)s'}},
u'handlers': {u'console': {u'class': u'logging.StreamHandler',
u'formatter': u'basic',
u'level': u'DEBUG',
u'stream': u'ext://sys.stdout'},
u'info_file_handler': {u'backupCount': 20,
u'class': u'logging.handlers.RotatingFileHandler',
u'encoding': u'utf8',
u'filename': u'info.log',
u'formatter': u'basic',
u'level': u'INFO',
u'maxBytes': 10485760}},
u'loggers': {u'APP_LOG': {u'handlers': [u'console'],
u'level': u'INFO',
u'propagate': u'no'}},
u'root': {u'handlers': [u'console', u'info_file_handler'], u'level': u'INFO'},
u'version': 1}
def setup_logging(json_filename='logging_conf.json', default_level=logging.INFO,
env_key='LOG_CFG', log_file_basepath=".", customised_file_name=""):
"""
Setup logging configuration
"""
json_filepath = os.path.join(log_file_basepath, json_filename)
env_json_filepath = os.getenv(env_key, None)
if env_json_filepath:
json_filepath = env_json_filepath
if logging_conf:
log_file_name = logging_conf["handlers"]["info_file_handler"]["filename"]
if customised_file_name:
log_file_name = customised_file_name
logging_conf["handlers"]["info_file_handler"]["filename"] = os.path.join(log_file_basepath, log_file_name)
logging.config.dictConfig(logging_conf)
else:
logging.basicConfig(level=default_level)
file-2. <<runner.py>>
import logging
module_logger = logging.getLogger('JIRA.fetch_data')
setup_logging()
module_logger.info("**Start capturing logs using logging.config")
Comments
Post a Comment