loggers.py 992 Bytes
Newer Older
Régis Behmo's avatar
Régis Behmo committed
1 2 3 4 5
from datetime import datetime
import logging
import logging.handlers
import os

6 7
import ujson as json

Régis Behmo's avatar
Régis Behmo committed
8 9
from . import VERSION

David Foucher's avatar
Black  
David Foucher committed
10
logger = logging.getLogger("trefle")
Régis Behmo's avatar
Régis Behmo committed
11 12 13
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())

David Foucher's avatar
Black  
David Foucher committed
14
request_logger = logging.getLogger("request_logger")
Régis Behmo's avatar
Régis Behmo committed
15
request_logger.setLevel(logging.INFO)
David Foucher's avatar
Black  
David Foucher committed
16 17
request_logger.addHandler(
    logging.handlers.TimedRotatingFileHandler(
18 19
        os.path.join(
            os.environ.get("TREFLE_LOG_DIR", "/tmp"),
20
            "trefle-simulate.log",
21
        ),
David Foucher's avatar
David Foucher committed
22
        when="D",
23
        backupCount=365,
David Foucher's avatar
Black  
David Foucher committed
24 25
    )
)
Régis Behmo's avatar
Régis Behmo committed
26 27


Yohan Boniface's avatar
Yohan Boniface committed
28
def log_simulate(context, financements=None, errors=None):
Régis Behmo's avatar
Régis Behmo committed
29 30 31
    financements = financements or []
    errors = errors or []
    message = {
David Foucher's avatar
Black  
David Foucher committed
32 33 34 35 36
        "date": datetime.utcnow().isoformat(),
        "context": context,
        "financements": [f["intitule"] for f in financements if f["eligible"]],
        "errors": errors,
        "version": VERSION,
Régis Behmo's avatar
Régis Behmo committed
37 38
    }
    request_logger.info(json.dumps(message, sort_keys=True))