Basic Example

The following example shows how to use the library to create a simple logger

from bohicalog import logger

logger.debug("hello")
logger.info("info")
logger.warning("warn")
logger.error("error")

# This is how you'd log an exception
try:
    raise Exception("this is a demo exception")
except Exception as e:
    logger.exception(e)

# JSON logging
import bohicalog

bohicalog.json()

logger.info("JSON test")

# Start writing into a logfile
bohicalog.logfile("/tmp/bohicalog-demo.log")

# Set a minimum loglevel
bohicalog.loglevel(bohicalog.WARNING)

...

If you execute the above code, you’ll get the following output:

$ python basic_example.py
[D 2023-01-04 04:35:26 basic_example:3] hello
[I 2023-01-04 04:35:26 basic_example:4] info
[W 2023-01-04 04:35:26 basic_example:5] warn
[E 2023-01-04 04:35:26 basic_example:6] error
[E 2023-01-04 04:35:26 basic_example:12] this is a demo exception
    Traceback (most recent call last):
      File "basic_example.py", line 10, in <module>
        raise Exception("this is a demo exception")
    Exception: this is a demo exception
{"asctime": "2023-01-04 04:35:26,801", "filename": "basic_example.py", "funcName": "<module>", "levelname": "INFO", "levelno": 20, "lineno": 19, "module": "basic_example", "message": "JSON test", "name": "bohicalog_default", "pathname": "basic_example.py", "process": 575, "processName": "MainProcess", "threadName": "MainThread"}