Python logging into file as a dictionary or JSON

前端 未结 2 1619
醉梦人生
醉梦人生 2021-01-04 13:17

I am trying to set up logging where I can log in both stdout and on to a file. This i have accomplished using the following code:

logging.basicConfig(
              


        
相关标签:
2条回答
  • 2021-01-04 13:55

    So based on @abarnert, i found this Link which provided a good path to making this concept work for the most part. The code as it stands is:

    logger=logging.getLogger()
    logger.setLevel(logging.DEBUG)
    
    file_handler=logging.FileHandler('foo.log')
    stream_handler=logging.StreamHandler()
    
    stream_formatter=logging.Formatter(
        '%(asctime)-15s %(levelname)-8s %(message)s')
    file_formatter=logging.Formatter(
        "{'time':'%(asctime)s', 'name': '%(name)s', \
        'level': '%(levelname)s', 'message': '%(message)s'}"
    )
    
    file_handler.setFormatter(file_formatter)
    stream_handler.setFormatter(stream_formatter)
    
    logger.addHandler(file_handler)
    logger.addHandler(stream_handler)
    

    Although it does not fully meet the requirement, it doesnt require any pre processing, and allows me to create two log handlers.

    Afterwards, i can use something like:

    with open('foo.log') as f:
        logs = f.read().splitlines()
    for l in logs:
        for key, value in eval(l):
            do something ...
    

    to pull dict objects instead of fighting with improperly formatted JSON to accomplish what i had set out to accomplish.

    Still am hoping for a more elegant solution.

    0 讨论(0)
  • 2021-01-04 14:20

    With this code you can add the full traceback, timestamp and level to a json file of choice.

    import json
    import traceback
    from datetime import datetime
    
    def addLogging(logDict:dict):
        loggingsFile = 'loggings.json'
    
        with open(loggingsFile) as f:
            data = json.load(f)
    
        data.append(logDict)
    
        with open(loggingsFile, 'w') as f:
            json.dump(data, f)
    
    def currentTimeUTC():
        return datetime.now().strftime('%d/%m/%Y %H:%M:%S')
    
    try:
        print(5 / 0)
    except ZeroDivisionError:
        fullTraceback = str(traceback.format_exc())
        addLogging({'timestamp': currentTimeUTC(), 'level': 'error', 'traceback': fullTraceback})
    

    Output:

    [
        {
            "timestamp": "09/06/2020 17:38:00",
            "level": "error",
            "traceback": "Traceback (most recent call last):\n  File \"d:testFullTraceback.py\", line 19, in <module>\n    print(5/0)\nZeroDivisionError: division by zero\n"
        }
    ]
    
    0 讨论(0)
提交回复
热议问题