openlawlibrary / pygls

A pythonic generic language server
https://pygls.readthedocs.io/en/latest/
Apache License 2.0
568 stars 103 forks source link

too many logging when use `black` formatting with vscode - it use my django-project log-file for logging #485

Open shtalinberg opened 1 month ago

shtalinberg commented 1 month ago

Hi

this one line below generate a lot of lines in my django-project https://github.com/openlawlibrary/pygls/blob/0f6005cf522f48889d7eb69e7d76c70ce75c119d/pygls/protocol/json_rpc.py#L387 I have a lot of lines like this [2024-08-05 17:18:37,510] INFO 140162374721536 [pygls.protocol.json_rpc:386] Sending data: {"id": 344, "jsonrpc": "2.0", "result": []}

I think it's related to using black formatting with vscode

1 question - why it use my django-project log-file ? 2 question - how I can disable it?

My log config

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'file_main': {
            'format': "[%(asctime)s] %(levelname)s %(thread)d [%(name)s:%(lineno)s] %(message)s",
        },
        'standard': {'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'},
        'standard_ip': {
            'format': '%(asctime)s %(client_ip)s [%(levelname)s] %(name)s: %(message)s'
        },
        "post_office": {
            "format": "[%(levelname)s]%(asctime)s PID %(process)d: %(message)s",
            "datefmt": "%d-%m-%Y %H:%M:%S",
        },
    },
    'filters': {
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
        'require_debug_false': {'()': 'django.utils.log.RequireDebugFalse'},
    },
    'handlers': {
        'logfile_main': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(PROJECT_LOGS_ROOT, 'main_project.log'),
            'maxBytes': 1024 * 1024 * 20,  # 20 mb
            'formatter': 'file_main',
            'encoding': 'UTF-8',
        },
    },
    'loggers': {
        '': {
            'handlers': ['logfile_main'],
            'level': 'INFO',
            'propagate': True,
        },
    },
}
karthiknadig commented 1 month ago

I think black formatter extension is how this issue manifested. But any project using pygls could trigger this. I suspect, something might have gone wrong with how the logger has been configured.

alcarney commented 1 month ago

1 question - why it use my django-project log-file ?

To be honest, I'm not entirely sure, it looks like your logging configuration is being applied to the Python process hosting the VSCode black formatter.

The only reasons why I can think of are

  1. You are running black and pygls as part of your django project? (Which seems unlikely)
  2. The logging configuration is somehow being applied to all Python processes started in your workspace. I assume there is more to your configuration than the LOGGING variable, where is it/how does it get used?

2 question - how I can disable it?

The best solution would be to try figure and out why the formatting process is picking up your configuration but failing that, it should at least be possible to work around it.

Looking at this part of your configuration

    'loggers': {
        '': {
            'handlers': ['logfile_main'],
            'level': 'INFO',
            'propagate': True,
        },
    },

It's very generic, have you tried only listing the loggers you are interested in e.g.?

    'loggers': {
        'my_django_project': {
            'handlers': ['logfile_main'],
            'level': 'INFO',
            'propagate': True,
        },
    },

Or adding a section to specifically disable pygls

    'loggers': {
        '': {
            'handlers': ['logfile_main'],
            'level': 'INFO',
            'propagate': True,
        },
        'pygls': {
            'level': 'ERROR',
            'handlers': []
        }
    },

(I don't have much experience with this style of logging configuration, so you may have to play around a bit to get the syntax right)

Hope that helps!

shtalinberg commented 1 month ago

Heh ..., sorry guys. I found who does that - it's an extension Pylint - v2023.10.1 from Microsoft