R
Railway2mo ago
cerwind

Logs being flagged as Errors

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?
Python documentation
logging — Logging facility for Python
Source code: Lib/logging/init.py Important: This page contains the API reference information. For tutorial information and discussion of more advanced topics, see Basic Tutorial, Advanced Tutor...
Solution:
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
Jump to solution
7 Replies
Percy
Percy2mo ago
Project ID: c61c1785-a8d8-43d0-9d8b-6bd1f69aa3bf
cerwind
cerwindOP2mo ago
c61c1785-a8d8-43d0-9d8b-6bd1f69aa3bf
Solution
Brody
Brody2mo 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
cerwind
cerwindOP2mo ago
GitHub
GitHub - madzak/python-json-logger: Json Formatter for the standard...
Json Formatter for the standard python logger. Contribute to madzak/python-json-logger development by creating an account on GitHub.
Brody
Brody2mo ago
I'm not a python guy, but I assume so too
ThallesComH
ThallesComH2mo 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)
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
Brody
Brody2mo ago
more people need to use JSON logging
Want results from more Discord servers?
Add your server