madzak / python-json-logger

Json Formatter for the standard python logger
BSD 2-Clause "Simplified" License
1.7k stars 231 forks source link

[Help] Basic Logging Usage #105

Closed kaykhancheckpoint closed 1 year ago

kaykhancheckpoint commented 3 years ago

I'm trying to use this library so i can output json logs to stdout.

src/logger.py

import logging
from pythonjsonlogger import jsonlogger

logger = logging.getLogger()

logHandler = logging.StreamHandler()
formatter = jsonlogger.JsonFormatter()
logHandler.setFormatter(formatter)
logger.addHandler(logHandler)

src/testlogger.py

from src.logger import logger

print("here")
logger.info({"hello": "world"})
python src/testlogger.py

When i run the above command, the print statement runs but i don't see the hello world log in json. Theres nothing else outputted.

liampauling commented 3 years ago

You forgot to set the level:

logger.setLevel(logging.INFO)