Skip to content

Commit

Permalink
changes to make logs splunk compatible
Browse files Browse the repository at this point in the history
Signed-off-by: Gulshan Bhatia <[email protected]>
  • Loading branch information
Gulshan Bhatia committed Mar 27, 2024
1 parent cfc048e commit b3d1358
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 28 deletions.
34 changes: 6 additions & 28 deletions numalogic/udfs/_logger.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,23 @@
import logging
import structlog
from structlog import processors, stdlib


def configure_logger(log_level: str = "INFO"):
def configure_logger():
"""Configure struct logger for the UDFs."""
shared_processors = [
stdlib.add_log_level,
stdlib.PositionalArgumentsFormatter(),
processors.TimeStamper(fmt="iso"),
processors.StackInfoRenderer(),
processors.format_exc_info,
processors.UnicodeDecoder(),
]
structlog.configure(
processors=[
stdlib.filter_by_level,
stdlib.add_log_level,
stdlib.PositionalArgumentsFormatter(),
processors.TimeStamper(fmt="iso"),
processors.StackInfoRenderer(),
processors.format_exc_info,
processors.UnicodeDecoder(),
structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
],
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True,
)

formatter = structlog.stdlib.ProcessorFormatter(
foreign_pre_chain=shared_processors,
processors=[
structlog.stdlib.ProcessorFormatter.remove_processors_meta,
processors.KeyValueRenderer(key_order=["uuid", "event"]),
stdlib.ProcessorFormatter.wrap_for_formatter,
],
logger_factory=stdlib.LoggerFactory(),
wrapper_class=stdlib.BoundLogger,
cache_logger_on_first_use=True,
)

handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger = structlog.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(log_level)

return structlog.getLogger(__name__)
1 change: 1 addition & 0 deletions numalogic/udfs/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def exec(self, keys: list[str], datum: Datum) -> Messages:
try:
data_payload = orjson.loads(datum.value)
log = log.bind(uuid=data_payload["uuid"])
log.info("Data payload= %s", data_payload)
except (orjson.JSONDecodeError, KeyError): # catch json decode error only
log.exception("Error while decoding input json")
return Messages(Message.to_drop())
Expand Down

0 comments on commit b3d1358

Please sign in to comment.