python logging to database

后端 未结 4 832
面向向阳花
面向向阳花 2020-12-13 02:49

I\'m seeking a way to let the python logger module to log to database and falls back to file system when the db is down.

So basically 2 things: How to let the logger

4条回答
  •  长情又很酷
    2020-12-13 03:15

    I am digging this out again.

    There is a solution with SqlAlchemy (Pyramid is NOT required for this recipe):

    https://docs.pylonsproject.org/projects/pyramid-cookbook/en/latest/logging/sqlalchemy_logger.html

    And you could improve logging by adding extra fields, here is a guide: https://stackoverflow.com/a/17558764/1115187

    Fallback to FS

    Not sure that this is 100% correct, but you could have 2 handlers:

    1. database handler (write to DB)
    2. file handler (write to file or stream)

    Just wrap the DB-commit with a try-except. But be aware: the file will contain ALL log entries, but not only entries for which DB saving was failed.

提交回复
热议问题