Reputation: 4387
I would to log in different files. Currently, all my logs are writing on the same file.
I've two files:
The extract.py calls insert.py
In my extract.py:
import insert
import logging
logging.basicConfig(filename='log/extract.log', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
In my insert.py:
import logging
logging.basicConfig(filename='log/insert.log', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
The problem is that every log are sent in insert.log. How can I send the log generate by extract.py in extract.log and log generate by insert.py in insert.log?
Thanks
Upvotes: 0
Views: 970
Reputation: 23332
You need to have two loggers, each with its own file handler. At each file:
log= logging.getLogger(__name__)
hdlr = logging.FileHandler(__name__+'.log', mode="w")
log.addHandler(hdlr)
log.setLevel(logging.DEBUG)
then call logging functions in log
instead of the logging
module
log.debug("my message")
Generally speaking, the quality of python's documentation is extraordinary. The advanced tutorial included in the logging
documentation covers this and much more
Upvotes: 1
Reputation: 12054
Put this in your extract.py only (this will work in python2.7+ and python3.2+):
import logging
import logging.config
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': '%(levelname)s:%(name)s: %(message)s '
'(%(asctime)s; %(filename)s:%(lineno)d)',
'datefmt': "%Y-%m-%d %H:%M:%S",
}
},
'handlers': {
'extract_rotate_file': {
'level': 'DEBUG',
'formatter': 'standard',
'class' : 'logging.handlers.RotatingFileHandler',
'filename' : 'extract.log',
'encoding': 'utf8',
'maxBytes': 1024*1024*2, # 2 MB
'backupCount': 5,
},
'insert_rotate_file': {
'level': 'DEBUG',
'formatter': 'standard',
'class' : 'logging.handlers.RotatingFileHandler',
'filename' : 'insert.log',
'encoding': 'utf8',
'maxBytes': 1024*1024*2, # 2 MB
'backupCount': 5,
}
},
'loggers': {
'extract': { # <-- put here name of your logger in extract.py
'handlers': ['extract_rotate_file'],
'level': 'DEBUG',
},
'insert': { # <-- put here name of your logger in insert.py
'handlers': ['insert_rotate_file'],
'level': 'DEBUG',
},
}
}
logging.config.dictConfig(LOGGING)
But precise name of you loggers, look for # <--
in code above.
Here is post about python logging with more details and examples
Upvotes: 0