import datetime import logging import sys import oci from oci.loggingingestion import LoggingClient from oci.loggingingestion.models import PutLogsDetails, LogEntryBatch, LogEntry from src import config from src.logs.AppLogging import init_logger from src.pojo.ReserveResultPojo import ReserveResultPojo, PublishType from src.pojo.contact_pojo import ContactPojo LOG_SUBJECT_EVENT = "EVENT" LOG_SUBJECT_SMS = "SMS" SUBJECT_SIM_INFO = "sim_card" # Log type TYPE_EVENT_CHECK_RESULTS = "EVENT_CHECK_RESULTS" TYPE_EVENT_READ_DB = "EVENT_READ_DB" TYPE_EVENT_RESET_ALL_SIM_CARDS = "EVENT_RESET_ALL_SIM_CARDS" TYPE_EVENT_CHANGE_SLOT = "EVENT_CHANGE_SLOT" TYPE_SMS_RECEIVED = "TYPE_SMS_RECEIVED" LOG_ERROR = "ERROR" LOG_TYPE_INFO = "INFO" LOG_ERROR_TYPE_DOUBLE_DATA = "DOUBLE_REQUEST_FOR_SAME_DATA" LOG_ERROR_TOO_MANY_REQUEST_TODAY = "TOO_MANY_REQUEST_TODAY" LOG_ERROR_CAPTCHA_ERROR_MESSAGE = "CAPTCHA_ERROR" LOG_SUBJECT_ERROR = "ERROR" LOG_APPOINTMENT_ERROR = "APPOINTMENT_ERROR" LOG_APPOINTMENT_TIMEOUT = "TIMEOUT" LOG_APPOINTMENT_CONTACT_NOT_FOUND = "CONTACT_NOT_FOUND" LOG_APPOINTMENT_SUCCESS = "SUCCESS" custom_retry_strategy = oci.retry.RetryStrategyBuilder( # Make up to 10 service calls max_attempts_check=True, max_attempts=10, # Don't exceed a total of 600 seconds for all service calls total_elapsed_time_check=True, total_elapsed_time_seconds=600, # Wait 45 seconds between attempts retry_max_wait_between_calls_seconds=45, # Use 2 seconds as the base number for doing sleep time calculations retry_base_sleep_time_seconds=2, # Retry on certain service errors: # # - 5xx code received for the request # - Any 429 (this is signified by the empty array in the retry config) # - 400s where the code is QuotaExceeded or LimitExceeded service_error_check=True, service_error_retry_on_any_5xx=True, service_error_retry_config={ 400: ['QuotaExceeded', 'LimitExceeded'], 429: [] }, # Use exponential backoff and retry with full jitter, but on throttles use # exponential backoff and retry with equal jitter backoff_type=oci.retry.BACKOFF_FULL_JITTER_EQUAL_ON_THROTTLE_VALUE ).get_retry_strategy() class LogSender: def __init__(self): self._config = oci.config.from_file("~/.oci/logger_config_appointment") self._identity = oci.identity.IdentityClient(self._config) self._loggingingestion_client = LoggingClient(self._config, timeout=60.0, retry_strategy=custom_retry_strategy) def send_double_data_error(self, contact: ContactPojo): error_msg = contact.mail self.send_log(msg=error_msg, type=LOG_ERROR_TYPE_DOUBLE_DATA, subject=LOG_SUBJECT_ERROR) def send_too_many_error(self, contact: ContactPojo): error_msg = contact.mail self.send_log(msg=error_msg, type=LOG_ERROR_TOO_MANY_REQUEST_TODAY, subject=LOG_SUBJECT_ERROR) def send_captcha_error(self, contact: ContactPojo): error_msg = contact.mail self.send_log(msg=error_msg, type=LOG_ERROR_CAPTCHA_ERROR_MESSAGE, subject=LOG_SUBJECT_ERROR) def send_appoint_result(self, result: ReserveResultPojo): if result.type == PublishType.SUCCESS: # get id self.send_log(result.id, type=LOG_APPOINTMENT_SUCCESS) else: msg = "{}, email: {}".format(result.message, result.email) self.send_log(msg, type=LOG_APPOINTMENT_ERROR) def send_error(self, msg: str): self.send_log(msg=msg, type=LOG_ERROR) def send_read_db_event(self, msg: str): self.send_log(msg=msg, type=TYPE_EVENT_READ_DB, subject=LOG_SUBJECT_EVENT) def send_log(self, msg: str, source=config.LOG_SOURCE, subject="appointment", type: str = "INFO"): log_id = "ocid1.log.oc1.eu-frankfurt-1.amaaaaaas4ft22ya3ub6glkltqqbnmkxo3ui7xwq3dxtjd2scdhme4deyu2q" response = self._loggingingestion_client.put_logs( log_id=log_id, put_logs_details=PutLogsDetails( specversion="1.0", log_entry_batches=[ LogEntryBatch( entries=[LogEntry( data=msg, id=log_id, time=datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ"))], source=source, type=type, defaultlogentrytime=datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ"), subject=subject)]) ) return response def send_sms_reception_log(self, phone, sms_text, ccid): msg = "from:{}, sms:{}, ccid:{}".format(phone, sms_text, ccid) self.send_log(msg=msg, subject=LOG_SUBJECT_SMS, type=TYPE_SMS_RECEIVED) def send_timeout_log(self, contact: ContactPojo): msg = "phone:{}, mail:{}".format(contact.phone, contact.mail) self.send_log(msg, type=LOG_APPOINTMENT_TIMEOUT) def send_contact_not_found(self, msg: str): self.send_log(msg, subject=SUBJECT_SIM_INFO, type=LOG_APPOINTMENT_CONTACT_NOT_FOUND) def send_wait_sms_log(self): self.send_log("等待短信", subject=LOG_SUBJECT_EVENT, type=TYPE_SMS_RECEIVED) if __name__ == '__main__': init_logger() logger = logging.getLogger() logger.addHandler(logging.StreamHandler(stream=sys.stdout)) log_sender = LogSender() response = log_sender.send_log("test") print(response)