Logs being flagged as Errors

charlie
PRO

9 months 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?

0 Replies

charlie
PRO

9 months ago

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


9 months 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
PRO

9 months ago


9 months ago

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


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


9 months ago

more people need to use JSON logging


Logs being flagged as Errors - Railway Help Station