[Python] logs system not works with python

Discussão em 'Python' iniciado por Stack, Abril 5, 2025.

  1. Stack

    Stack Membro Participativo

    I need to implement a monitoring system with logs and alerts.

    The goal is that, when the system enters a critical state, a message should be sent to our logging microservice, which in this case simulates a Slack webhook.

    The problem is that the local log file (app.log) is being created correctly, but the Slack notification is neither recorded nor sent. The webhook is set via an environment variable.

    Segue o código atual:import os
    from dotenv import load_dotenv
    import requests
    import logging

    logging.basicConfig(filename='app.log', level=logging.INFO)
    logger = logging.getLogger(__name__)

    load_dotenv()

    API_KEY = os.getenv('MONITOR_API_KEY')
    SLACK_WEBHOOK = os.getenv('SLACK_WEBHOOK')
    DB_PASSWORD = "HIK_DEV_DESCUIDADO_"

    def check_system_status():
    try:
    response = requests.get(
    'https://api.monitoring.hikdev.com/status',
    headers={'Authorization': f'Bearer {API_KEY}'}
    )
    return response.json()
    except Exception as e:
    logger.error(f"Erro ao verificar status: {e}")
    return None

    def notify_slack(message):
    try:
    requests.post(SLACK_WEBHOOK, json={'text': message})
    except Exception as e:
    logger.error(f"Erro ao notificar Slack: {e}")

    def main():
    logger.info("Iniciando monitoramento...")
    status = check_system_status()

    if status and status['status'] == 'critical':
    notify_slack("⚠️ Sistema em estado crítico!")

    if __name__ == "__main__":
    main()

    Continue reading...

Compartilhe esta Página