Reputation: 30915
i like to write error messages to the additional error log AND to the main log as currently all the output is written to the main log i set my log as this :
logger = logging.getLogger('werkzeug')
logger.setLevel(logging.INFO)
server_logs_directory = 'logs/app/'
# Create handler
handler = RotatingFileHandler(server_logs_directory + "my_app_logger.log", maxBytes=20480000, backupCount=50)
# Create formatter
formatter = logging.Formatter('%(asctime)s |--| %(name)s |--| %(levelname)s |--| %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p')
# Add formatter to handler
handler.setFormatter(formatter)
# Add handler to logger
logger.addHandler(handler)
i can't find any way to add another handler for errors log file .
Upvotes: 0
Views: 78
Reputation: 5195
You can set a level on the handler itself. Just add another handler to the same logger but only log errors with that handler.
err_handler = RotatingFileHandler(server_logs_directory + "my_app_errors.log", maxBytes=20480000, backupCount=50)
err_handler.setLevel(logging.ERROR)
logger.addHandler(err_handler)
Upvotes: 2