masonite icon indicating copy to clipboard operation
masonite copied to clipboard

[5.X] Add `logging` feature

Open girardinsamuel opened this issue 3 years ago • 2 comments

Fix MasoniteFramework/masonite#663.

Specifications

Available drivers are: terminal, single, daily, syslog, slack. Logging configuration is done in config/logging.py

CHANNELS = {
    "default": {
        "driver": "console",
        # those settings will be applied to each channel if not overriden
        "level": "info", 
        "timezone": "UTC",
        "format": "{timestamp} - {levelname}: {message}",
        "date_format": "YYYY-MM-DD HH:mm:ss",
    },
    "console": {
        "driver": "terminal",
    },
    "single": {
        "driver": "single",
        "path": "logs/single.log",
    },
    "daily": {
        "driver": "daily",
        "path": "logs/daily.log",
        "days": 7,
        "keep": 10,
    },
    "all": {"driver": "stack", "channels": ["single", "daily", "console"]},
    "syslog": {"driver": "syslog", "address": "/var/log/system.log"},
    "papertrail": {
        "driver": "syslog",
        "host": "logs.papertrailapp.com",
        "port": None,  # specify here the port as an integer
    },
    "slack": {
        "driver": "slack",
        "webhook_url": "",
    },
}
from masonite.facades import Log

# Log a message to the default configured channel
Log.debug("hello !")
Log.info("hello !")
Log.notice("hello !")
Log.warning("hello !")
Log.critical("hello !")
Log.critical("hello !")
Log.alert("hello !")
Log.emergency("hello !")

# You can change channel on the fly
Log.channel("daily").info("Some message")

# You can stack channels to log a message to multiple channels
Log.stack("console", "daily", "slack").critical("Some message")

Actually Sentry can be used in two different ways. One can add a custom exception handler and use sentry python binding to send the info or there could be a Sentry logging driver, but it would not do much more than the python sentry binding. So what I propose is to maybe create a package for integrating a sentry exception handler a bit like sentry-laravel.

What's left to do

  • [x] add syslog, slack drivers.
  • [ ] ensure exceptions are logged with enough information -> needs to work a bit ExceptionHandler code
  • [ ] add propagate option
  • [ ] add documentation https://github.com/MasoniteFramework/docs/pull/221
  • [x] add catch all listener to log exceptions

girardinsamuel avatar Oct 10 '22 14:10 girardinsamuel

great feature and great code looks awesome💥

BSN4 avatar Oct 11 '22 17:10 BSN4

Nice work @girardinsamuel

This looks exactly like what I was getting at in #663. Functional and flexible.

Keep up the great work. Cheers

circulon avatar Oct 13 '22 23:10 circulon