62 lines
2.2 KiB
Python
62 lines
2.2 KiB
Python
import datetime
|
|
import logging
|
|
import sys
|
|
|
|
import oci
|
|
from oci.loggingingestion import LoggingClient
|
|
from oci.loggingingestion.models import PutLogsDetails, LogEntryBatch, LogEntry
|
|
|
|
from logs.AppLogging import init_logger
|
|
from pojo import ReserveResultPojo
|
|
from pojo.ReserveResultPojo import PublishType
|
|
|
|
LOG_ERROR = "ERROR"
|
|
LOG_INFO = "INFO"
|
|
LOG_APPOINTMENT_ERROR = "APPOINTMENT_ERROR"
|
|
LOG_APPOINTMENT_TIMEOUT = "TIMEOUT"
|
|
LOG_APPOINTMENT_SUCCESS = "SUCCESS"
|
|
SUBJECT_SIM_INFO = "sim_card"
|
|
|
|
|
|
class LogSender:
|
|
def __init__(self):
|
|
self._config = oci.config.from_file("~/.oci/logger_config")
|
|
self._identity = oci.identity.IdentityClient(self._config)
|
|
self._loggingingestion_client = LoggingClient(self._config)
|
|
|
|
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_log(self, msg: str, source="Appointment", subject="appointment", type: str = "INFO"):
|
|
log_id = "ocid1.log.oc1.eu-frankfurt-1.amaaaaaacvc3jtia5enufv5jxe6binq4ndaty7pt5mk7uidipfd62ynwdhnq"
|
|
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
|
|
|
|
|
|
if __name__ == '__main__':
|
|
init_logger()
|
|
logger = logging.getLogger()
|
|
logger.addHandler(logging.StreamHandler(stream=sys.stdout))
|
|
log_sender = LogSender()
|
|
response = log_sender.send_log("test2")
|
|
print(response)
|