user63898
user63898

Reputation: 30915

python how to write error massages to error log and to main log using werkzeug logger in flask

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

Answers (1)

blues
blues

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

Related Questions