Python logging in Railway

hugros-93PRO

a year ago

I was deploying a Python app for the first time and got some questions re: logging.

1/ I got some error logs when deploying the app:

[2024-03-13 09:35:46 +0000] [7] [INFO] Starting gunicorn 20.0.4
[2024-03-13 09:35:46 +0000] [7] [INFO] Listening at: http://0.0.0.0:6127 (7)
[2024-03-13 09:35:46 +0000] [7] [INFO] Using worker: sync
[2024-03-13 09:35:46 +0000] [11] [INFO] Booting worker with pid: 11
[2024-03-13 09:36:18 +0000] [7] [INFO] Handling signal: term
[2024-03-13 09:36:18 +0000] [11] [INFO] Worker exiting (pid: 11)
[2024-03-13 09:36:18 +0000] [7] [INFO] Shutting down: Master

These are labeled as errors in Railway observability logs, but seem to be info. I suppose the logs sent when starting gunicorn are not formatted as expected by Railway.

2/ In my code, I'm using logging.info() and logging.error() to send logs, but all these messages are labeled as errors in Railway observability.

Is there some good practice for Python logs so Railway can correctly label these logs?

Thank you!

Solved

3 Replies

hugros-93PRO

a year ago

Re: 2/ I got a solution working, sharing here in case in can help: you can create a class to format logs properly:

class CustomRailwayLogFormatter(logging.Formatter):
    def format(self, record):
        log_record = {
            "time": self.formatTime(record),
            "level": record.levelname,
            "message": record.getMessage()
        }
        return json.dumps(log_record)

And create a function to return the proper logger to use as logger.info() or logger.error():

def get_logger():
    logger = logging.getLogger()
    logger.setLevel(logging.INFO) # this should be just "logger.setLevel(logging.INFO)" but markdown is interpreting it wrong here...
    handler = logging.StreamHandler()
    for handler in logger.handlers[:]:
        logger.removeHandler(handler)
    formatter = CustomRailwayLogFormatter()
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    return logger

Status changed to Solved railway[bot] about 1 year ago


a year ago

re: why logs where red.

you where logging to stderr without overwriting the level, now you are still logging to stderr but are specifying the level with the level attribute in your json logs.


Thank you for sharing the solution!