Logging πŸ““οƒ

Importing and creating a logging instance πŸ§ͺ

Example 1: Get a default logger

[ ]:
from peak.tools import logging
from peak.tools.logging import PeakLogger

logger: PeakLogger = logging.get_logger()

logger.info("This is an info message")
# gives you: {"level": "info", "logger": "root", "message": "This is an info message", "source": "peak-sdk", "timestamp": "2023-10-02T06:26:37.963649Z"}

Example 2: Get a customised logger

[ ]:
from peak.tools import logging
from peak.tools.logging import LogHandler, LogLevel, PeakLogger

logger: PeakLogger = logging.get_logger(
    name=__name__,
    level=LogLevel.ERROR,
    pretty_print=True,
    handlers=[LogHandler.CONSOLE],
)

logger.error("This is an error message")
# gives you:
#  {
#    "level": "error",
#    "logger": "__main__",
#    "message": "This is an error message",
#    "source": "peak-sdk",
#    "timestamp": "2023-10-02T07:05:21.883308Z"
#  }

logger.info("This is an info message and hence will not be printed")
# gives you: nothing

Masking sensetive data πŸ§ͺ

Example 1: Masking is enabled by default

[ ]:
from peak.tools import logging
from peak.tools.logging import LogHandler, LogLevel, PeakLogger

logger: PeakLogger = logging.get_logger(__name__)

logger.error("This is my email user@someorg.com")
# gives you: {"level": "error", "logger": "__main__", "message": "This is my email use****************com", "source": "peak-sdk", "timestamp": "2024-01-31T10:55:03.822710Z"}

Example 2: Disabling masking

[ ]:
from peak.tools import logging
from peak.tools.logging import LogHandler, LogLevel, PeakLogger

logger: PeakLogger = logging.get_logger(name=__name__, disable_masking=True)

logger.error("This is my email user@someorg.com")
# gives you: {"level": "error", "logger": "__main__", "message": "This is my email user@someorg.com", "source": "peak-sdk", "timestamp": "2024-01-31T10:56:42.486516Z"}

Binding and unbinding contexts πŸ§ͺ

Example 1: Bind additional context to logger

[ ]:
from peak.tools import logging
from peak.tools.logging import PeakLogger

logger: PeakLogger = logging.get_logger(
    name=__name__,
)

# bind the context
logger.bind({"key1": "value1", "key2": "value2"})

logger.info("Info msg with additional context")
# gives you: {"key1": "value1", "key2": "value2", "level": "info", "logger": "__main__", "message": "Info msg with additional context", "source": "peak-sdk", "timestamp": "2023-10-02T06:29:41.181591Z"}

Example 2: Unbind key(s) from the context

[ ]:
from peak.tools import logging
from peak.tools.logging import PeakLogger

logger: PeakLogger = logging.get_logger(
    name=__name__,
)

# bind the context
logger.bind({"key1": "value1", "key2": "value2"})

logger.info("Info msg with additional context")
# gives you: {"key1": "value1", "key2": "value2", "level": "info", "logger": "__main__", "message": "Info msg with additional context", "source": "peak-sdk", "timestamp": "2023-10-02T06:31:24.920581Z"}

# unbind key1 from the context
logger.unbind(["key1", "key3"])

logger.info("Info msg with partial context")
# gives you: {"key2": "value2", "level": "info", "logger": "__main__", "message": "Info msg with partial context", "source": "peak-sdk", "timestamp": "2023-10-02T06:31:24.920856Z"}

Changing log level πŸ§ͺ

Example 1: Change or Set the log level

[ ]:
from peak.tools import logging
from peak.tools.logging import LogLevel, PeakLogger

logger: PeakLogger = logging.get_logger(
    name=__name__,
    level=LogLevel.INFO,
)

# log level is INFO
logger.info("This is the first info msg")
# gives you: {"level": "info", "logger": "__main__", "message": "This is the first info msg", "source": "peak-sdk", "timestamp": "2023-10-02T06:34:12.380974Z"}

logger.error("This is the first error msg")
# gives you: {"level": "error", "logger": "__main__", "message": "This is the first error msg", "source": "peak-sdk", "timestamp": "2023-10-02T06:34:12.381258Z"}

# change the log level to ERROR
logger.set_log_level(LogLevel.ERROR)

logger.info("This is the second info msg hence will not be printed")
# gives you : nothing

logger.error("This is the second error msg")
# gives you : {"level": "error", "logger": "__main__", "message": "This is the second error msg", "source": "peak-sdk", "timestamp": "2023-10-02T06:34:12.381356Z"}

Additional examples πŸ§ͺ

Example 1: Using Formatting Placeholders

[ ]:
from peak.tools import logging
from peak.tools.logging import PeakLogger

logger: PeakLogger = logging.get_logger()

name = "John"
age = 30

logger.info("User: %s, Age: %d", name, age)
# gives you: {"level": "info", "logger": "root", "message": "User: John, Age: 30", "source": "peak-sdk", "timestamp": "2023-10-02T06:35:42.477169Z"}

Example 2: Logging with Structured Data

[ ]:
from typing import Any

from peak.tools import logging
from peak.tools.logging import PeakLogger

logger: PeakLogger = logging.get_logger()

data: Any = {"user_id": 123, "action": "Login"}

logger.info("User performed an action", data=data)
# gives you: {"data": {"action": "Login", "user_id": 123}, "level": "info", "logger": "root", "message": "User performed an action", "source": "peak-sdk", "timestamp": "2023-10-02T06:39:25.509306Z"}

Example 3: Logging Mixed Data Types

[ ]:
from typing import Any

from peak.tools import logging
from peak.tools.logging import PeakLogger

logger: PeakLogger = logging.get_logger()

user_id = 123
action = "Login"
result: Any = {"success": True, "message": "Logged in successfully"}

logger.info("Logging mixed data types", user_id=user_id, action=action, result=result)
# gives you: {"action": "Login", "level": "info", "logger": "root", "message": "Logging mixed data types", "result": {"message": "Logged in successfully", "success": true}, "source": "peak-sdk", "timestamp": "2023-10-02T06:41:42.279838Z", "user_id": 123}

Example 4: Logging Exceptions

[ ]:
from peak.tools import logging
from peak.tools.logging import PeakLogger

logger: PeakLogger = logging.get_logger()

try:
    result: float = 10 / 0
except Exception:
    logger.exception("An exception occurred")

# gives you: {"exception": "Traceback (most recent call last):\n  File \"/var/folders/xs/mq31rfn90m7fhf4yy151437w0000gn/T/ipykernel_12129/3339137748.py\", line 7, in <module>\n    result: float = 10 / 0\nZeroDivisionError: division by zero", "level": "error", "logger": "root", "message": "An exception occurred", "source": "peak-sdk", "timestamp": "2023-10-02T06:46:03.964036Z"