Python deployment showing wrong logging type
b1uedev
HOBBYOP

2 years ago

I have logging.info() to log some information about my application while running but for some reason, Railway show it as ERROR, which is quite annoying (as seen in the image).
This is how I handle the log for my deploy:

    @classmethod
    def logger_config(cls) -> logging.Logger:
        """Setup the logging environment."""

        log = logging.getLogger()
        logging.Formatter.converter = (
            lambda *args: Init.get_local_time().timetuple()
        )
        log.setLevel(logging.INFO)
        formatter = colorlog.ColoredFormatter(
            fmt="%(log_color)s" + "[%(asctime)s] %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s",
            datefmt="%d/%m/%Y %H:%M:%S",
            reset=True,
            log_colors={
                "DEBUG": "white",
                "INFO": "white",
                "WARNING": "fg_bold_yellow",
                "ERROR": "fg_bold_red",
                "CRITICAL": "red",
            },
        )
        stream_handler = logging.StreamHandler()
        stream_handler.setFormatter(formatter)
        log.addHandler(stream_handler)
        return log

Is there anyway to fix this?

12 Replies

b1uedev
HOBBYOP

2 years ago

The project ID is e1ece322-0918-4d47-a1fd-f7d31ed137fa


2 years ago

logging.info() logs to stderr, thus it's being treated as an error, two options - log to stdout, or use json structured logging (railway has amazing support for structured logging)


b1uedev
HOBBYOP

2 years ago

Can you explain more about the json structured logging thing? First time hearing about this.


2 years ago

there are many great resources online about it that would do a better job at describing it than I could, but tl;dr you would use a logging library that supports logging in json, your log message and level would be printed to the console as attributes in a json object and railway will parse the message and level out, then railway will colour the log accordingly to the level


b1uedev
HOBBYOP

2 years ago

Ah, I get that idea now.


b1uedev
HOBBYOP

2 years ago

The first option you mentioned, if I set the log to stdout, then imagine if it is actually an ERROR, Railway still treat it as with the stdout, which no error color?


2 years ago

correct


2 years ago

here's an example of railways support for structured logging, this of course contains more than just a message and level, and that's how railway will display additional attributes

1242475690876469200


2 years ago

you can also filter your logs by those attribute values


b1uedev
HOBBYOP

2 years ago

Ah, gotcha.


b1uedev
HOBBYOP

2 years ago

I think for now, I will try to display the information with stdout, and handle the error through stderr. Later, when I have time, I will learn how to use the structured logging. Thanks for the help, Brody. :)


2 years ago

no problem!


Loading...