Updated logger to log a dictionary instead of a string

This commit is contained in:
Kush Bavishi 2019-07-31 11:11:39 -07:00
parent 6bf920e66c
commit 3ba7fdbd49
1 changed files with 61 additions and 19 deletions

View File

@ -1,6 +1,7 @@
import time
import requests
import json
import sys
from .ultradns_zone import Zone
from .ultradns_record import Record
@ -115,7 +116,14 @@ def wait_for_dns_change(change_id, account_number=None):
nameserver = get_authoritative_nameserver(fqdn)
for attempts in range(0, number_of_attempts):
status = _has_dns_propagated(fqdn, token, nameserver)
current_app.logger.debug("Record status on ultraDNS authoritative server for fqdn: {}: {}".format(fqdn, status))
function = sys._getframe().f_code.co_name
log_data = {
"function": function,
"fqdn": fqdn,
"status": status,
"message": "Record status on ultraDNS authoritative server"
}
current_app.logger.debug(log_data)
if status:
time.sleep(10)
break
@ -123,7 +131,14 @@ def wait_for_dns_change(change_id, account_number=None):
if status:
for attempts in range(0, number_of_attempts):
status = _has_dns_propagated(fqdn, token, get_public_authoritative_nameserver())
current_app.logger.debug("Record status on Google DNS for fqdn: {}: {}".format(fqdn, status))
function = sys._getframe().f_code.co_name
log_data = {
"function": function,
"fqdn": fqdn,
"status": status,
"message": "Record status on Public DNS"
}
current_app.logger.debug(log_data)
if status:
metrics.send("wait_for_dns_change_success", "counter", 1)
break
@ -196,15 +211,24 @@ def create_txt_record(domain, token, account_number):
try:
_post(path, params)
current_app.logger.debug(
"TXT record created: {0}, token: {1}".format(fqdn, token)
)
function = sys._getframe().f_code.co_name
log_data = {
"function": function,
"fqdn": fqdn,
"token": token,
"message": "TXT record created"
}
current_app.logger.debug(log_data)
except Exception as e:
current_app.logger.debug(
"Unable to add record. Domain: {}. Token: {}. "
"Record already exists: {}".format(domain, token, e),
exc_info=True,
)
function = sys._getframe().f_code.co_name
log_data = {
"function": function,
"domain": domain,
"token": token,
"Exception": e,
"message": "Unable to add record. Record already exists."
}
current_app.logger.debug(log_data)
change_id = (fqdn, token)
return change_id
@ -222,7 +246,12 @@ def delete_txt_record(change_id, account_number, domain, token):
# has to be deleted.
if not domain:
current_app.logger.debug("delete_txt_record: No domain passed")
function = sys._getframe().f_code.co_name
log_data = {
"function": function,
"message": "No domain passed"
}
current_app.logger.debug(log_data)
return
zone_name = get_zone_name(domain, account_number)
@ -241,7 +270,13 @@ def delete_txt_record(change_id, account_number, domain, token):
# Remove the record from the RRSet locally
record.rdata.remove("{}".format(token))
except ValueError:
current_app.logger.debug("Token not found")
function = sys._getframe().f_code.co_name
log_data = {
"function": function,
"token": token,
"message": "Token not found"
}
current_app.logger.debug(log_data)
return
# Delete the RRSet from UltraDNS
@ -259,16 +294,23 @@ def delete_txt_record(change_id, account_number, domain, token):
def delete_acme_txt_records(domain):
if not domain:
current_app.logger.debug("delete_acme_txt_records: No domain passed")
function = sys._getframe().f_code.co_name
log_data = {
"function": function,
"message": "No domain passed"
}
current_app.logger.debug(log_data)
return
acme_challenge_string = "_acme-challenge"
if not domain.startswith(acme_challenge_string):
current_app.logger.debug(
"delete_acme_txt_records: Domain {} doesn't start with string {}. "
"Cowardly refusing to delete TXT records".format(
domain, acme_challenge_string
)
)
function = sys._getframe().f_code.co_name
log_data = {
"function": function,
"domain": domain,
"acme_challenge_string": acme_challenge_string,
"message": "Domain does not start with the acme challenge string"
}
current_app.logger.debug(log_data)
return
zone_name = get_zone_name(domain)