•  
      Internship #158803 create a python program using flask that generate log files when a api endpoint is triggered or called.
    #158803
    AMAN RAY (_aman_ray_)
    2024-05-21 20:50
    2024-05-21 20:50
    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
    AMAN RAY (_aman_ray_)
    Saurabh Kumar Pandey (saurabh)
    2024-05-17
    Closed
    Attachments
    completed
    References
    References list is empty