dchaley / deepcell-imaging

Tools & guidance to scale DeepCell imaging on Google Cloud Batch
7 stars 2 forks source link

Some logs in Batch show up as errors but aren't #220

Closed dchaley closed 3 days ago

dchaley commented 2 months ago

Some of the logs show up as errors even though they're labeled info or debug. This is disconcerting 😅 figure out why...

Image

lynnlangit commented 2 months ago

yes, we have seen this in other envs too - would be interested to hear what the Batch team says about this...

dchaley commented 3 weeks ago

From Shamel:

Use this library: https://pypi.org/project/JSON-log-formatter/

Then do something like this:

import logging
from json_log_formatter import JSONFormatter

class GCPFormatter(JSONFormatter):
    def json_record(self, message: str, extra: dict, record: logging.LogRecord) -> dict:
        extra['severity'] = record.levelname
        return super(GCPFormatter, self).json_record(message, extra, record)

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
formatter = GCPFormatter()
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.info("Log message", extra={'custom_field_1': "foo", 'custom_field_2': "bar"})