Skip to content

Instantly share code, notes, and snippets.

@KyMidd
Created November 8, 2025 22:12
Show Gist options
  • Select an option

  • Save KyMidd/56c490f0ea2a1bcba219d4511096d3b7 to your computer and use it in GitHub Desktop.

Select an option

Save KyMidd/56c490f0ea2a1bcba219d4511096d3b7 to your computer and use it in GitHub Desktop.
def write_audit_log(user_id, session_id, user_query, full_conversation, response, conversation_id, context):
try:
logs_client = boto3.client("logs", region_name="us-east-1")
aws_account_id = context.invoked_function_arn.split(":")[4]
log_entry = {
"timestamp": datetime.now(timezone.utc).isoformat(),
"bot_name": "VeraResearch",
"aws_account_id": aws_account_id,
"user_id": user_id,
"session_id": session_id,
"user_query": user_query,
"full_conversation": full_conversation,
"response": response,
"model_used": model_id,
"conversation_id": conversation_id,
}
log_stream_name = f"VeraResearch/{session_id}"
try:
logs_client.create_log_stream(logGroupName=audit_log_group_name, logStreamName=log_stream_name)
except logs_client.exceptions.ResourceAlreadyExistsException:
pass
logs_client.put_log_events(logGroupName=audit_log_group_name, logStreamName=log_stream_name,
logEvents=[{"timestamp": int(datetime.now(timezone.utc).timestamp() * 1000), "message": json.dumps(log_entry)}])
except Exception as error:
print(f"Error writing audit log: {error}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment