Logs being flagged as Errors
charlie
PROOP

2 years ago

Hi, I'm using the Python module (https://docs.python.org/3/library/logging.html) to differentiate logs however on the Railway logs it doesn't seem to differentiate between Warnings and Errors and just labels everything with an Error level. It could be something with my configuration but curious if anyone else has encountered this or have a logging configuration that works for them?

6 Replies

charlie
PROOP

2 years ago

c61c1785-a8d8-43d0-9d8b-6bd1f69aa3bf


2 years ago

They are counted as errors because they are printed to stderr, if you want colored logs for info, error, warn, debug -- you will need to use JSON logging


charlie
PROOP

2 years ago


2 years ago

I'm not a python guy, but I assume so too


2 years ago

no need for a library, just send the correct log level to the correct output

import sys
import logging

class InfoFilter(logging.Filter):
    def filter(self, rec):
        return rec.levelno in (logging.DEBUG, logging.INFO)


logger = logging.getLogger("__name__")
logger.setLevel(logging.DEBUG)

h1 = logging.StreamHandler(sys.stdout)
h1.setLevel(logging.DEBUG)
h1.addFilter(InfoFilter())
h2 = logging.StreamHandler()
h2.setLevel(logging.WARNING)

logger.addHandler(h1)
logger.addHandler(h2)

any logs greater than warning will go to stderr


2 years ago

more people need to use JSON logging


Welcome!

Sign in to your Railway account to join the conversation.

Loading...