Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions logging/python-structured/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@

from loguru import logger
import sys
import json
import traceback

# This simple Code Engine job demonstrates how to write structured log lines
from loguru import logger

# This simple Code Engine job demonstrates how to write structured log lines
# using the logging library loguru (https://github.com/Delgan/loguru)


# Define a custom JSON sink
def json_sink(message):
record = message.record
Expand Down Expand Up @@ -35,28 +36,22 @@ def json_sink(message):
sys.stdout.flush()


# Remove default handler (which includes timestamp, etc.) and add our custom sink
logger.remove()
logger.add(json_sink, level="DEBUG") # lowest level you want to capture


if __name__ == '__main__':

def main():
# expect to be rendered as INFO level log message
logger.info("This is a structured log message");
logger.info("This is a structured log message")

# expect to be rendered as DEBUG level log message
logger.debug("This is a structured log message");
logger.debug("This is a structured log message")

# expect to be rendered as WARN level log message
logger.warning("This is a structured log message");
logger.warning("This is a structured log message")

# expect to be rendered as ERROR level log message
logger.error("This is a structured log message");
logger.error("This is a structured log message")

# Expect to be rendered as DEBUG level log message. The extra key is available as a searchable, filterable field
logger.bind(extra_key="extra_value").debug("A structured log entry that contains an extra key")

# Expect to be rendered as INFO level log message. The additional JSON struct is available as a searchable, filterable fields
logger.bind(
requestId="some-request-id",
Expand All @@ -65,7 +60,6 @@ def json_sink(message):
metadata={"foo": "bar"},
).info("A structured log entry that carries a ton of additional fields")


# Multi-line example. Expect to be rendered in a single log message
logger.info(
"Multi-line log sample:\n"
Expand All @@ -75,13 +69,23 @@ def json_sink(message):
"Line 4: entering main loop\n"
"End of sample"
)

# Error logging. The error stack trace is rendered in a single log message (see field stack)
try:
raise RuntimeError("boom!")
except Exception:
# logger.exception() automatically attaches the current exception info
logger.exception("An error occurred")

# this will be caught by loguru's logger.catch context
import nonexistentmodule


if __name__ == '__main__':
# Remove default handler (which includes timestamp, etc.) and add our custom sink
logger.remove()
logger.add(json_sink, level="DEBUG") # lowest level you want to capture


# Catch any uncaught errors and properly log them
with logger.catch():
main()
Loading