a year ago
I have [logging.info](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?
0 Replies
a year 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)
Can you explain more about the json structured logging thing? First time hearing about this.
a year 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
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?
a year ago
correct
a year 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
a year ago
you can also filter your logs by those attribute values
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. :)
a year ago
no problem!