R
Railway2mo ago
Blue

Python deployment showing wrong logging type

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
@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?
No description
Solution:
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)
Jump to solution
9 Replies
Percy
Percy2mo ago
Project ID: e1ece322-0918-4d47-a1fd-f7d31ed137fa
Blue
Blue2mo ago
The project ID is e1ece322-0918-4d47-a1fd-f7d31ed137fa
Solution
Brody
Brody2mo 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)
Blue
Blue2mo ago
Can you explain more about the json structured logging thing? First time hearing about this.
Brody
Brody2mo 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
Blue
Blue2mo ago
Ah, I get that idea now. 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?
Brody
Brody2mo ago
correct 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 you can also filter your logs by those attribute values
Blue
Blue2mo ago
Ah, gotcha. 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. :)
Brody
Brody2mo ago
no problem!