•  
      Internship #175479 create a python program using flask that generate log files when a api endpoint is triggered or called.
    #175479
    AMAN RAY (amanray2004)
    2024-06-14 12:48
    2024-06-14 12:35
    Details
    create a python program using flask that generate log files when a api endpoint is triggered or called.

    from flask import Flask, request, jsonify import os import logging

    app = Flask(name) # Initialize the Flask application

    Ensure log directory exists

    log_dir = 'logs' if not os.path.exists(log_dir): os.makedirs(log_dir)

    Configuring logger

    log_file = os.path.join(log_dir, 'api_logs.log') logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

    @app.route('/process_name', methods=['POST']) # Define the endpoint and the HTTP method it accepts def process_name(): try:

    Retrieving name from request body

    data = request.get_json() if not data or 'name' not in data: return jsonify({'error': 'Name parameter is missing'}), 400

    name = data.get('name')

    Logging

    log_data = { 'message': 'Processing name', 'name': name } logging.info(log_data)

    return jsonify({'message': 'Name processed successfully', 'name': name}), 200

    except Exception as e: logging.error(f"An error occurred: {str(e)}") return jsonify({'error': 'Internal Server Error'}), 500

    if name == 'main': app.run(debug=True, host='0.0.0.0') # Start the Flask application

    Empty
    5 - Medium
    State of Progress
    2024-05-16
    100
    2024-05-17
    Rituraj Paul (riturajpaul_01), Amisha Ray (_amisha_ray_), AMAN RAY (amanray2004)
    Saurabh Kumar Pandey (saurabh)
    2024-05-17
    Closed
    Attachments
    References
    References list is empty

    Follow-ups