Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to prevent GET requests logging only? #919

Open
eduardopezzi opened this issue Feb 12, 2024 · 3 comments
Open

How to prevent GET requests logging only? #919

eduardopezzi opened this issue Feb 12, 2024 · 3 comments

Comments

@eduardopezzi
Copy link

import hug
from threading import Thread
from src.libs.versionMiddleware import VersionMiddleware

api = hug.API(__name__)
api.http.add_middleware(
    hug.middleware.CORSMiddleware(api, allow_origins=["*"])
)
api.http.add_middleware(
    VersionMiddleware('prometheus-version')
)

Implementing HUG API I am trying to prevent the GET requests output at the container logs/prompt

I tried to make a custom middleware without success... any suggestion?

@Sahithiaele
Copy link

can i please be assigned to this issue ?

@eduardopezzi
Copy link
Author

sure, I tried this code, but get requests still trashing my container's log

class SilentGetLoggingMiddleware(hug.middleware.LoggingMiddleware):
    def log(self, request, response=None):
        if request.method != 'GET':
            super().log(request, response)

api = hug.API(__name__)
api.http.add_middleware(CORSMiddleware(api, allow_origins=["*"]))
api.http.add_middleware(SilentGetLoggingMiddleware())

@Sahithiaele
Copy link

Sahithiaele commented Feb 16, 2024

it seems there's an issue with the hug.middleware.LoggingMiddleware not being silent for GET requests. To work around this, you can use the standard Python logging module directly to configure logging and filter out GET requests.

`import hug
from hug.middleware import CORSMiddleware
import logging
from src.libs.versionMiddleware import VersionMiddleware

class SilentGetLoggingMiddleware:
def process_request(self, request, response):
if request.method == 'GET':
return None
return True
api = hug.API(name)
api.http.add_middleware(CORSMiddleware(api, allow_origins=["*"]))
api.http.add_middleware(SilentGetLoggingMiddleware())
api.http.add_middleware(VersionMiddleware('prometheus-version'))

logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")

@hug.get('/')
def hello():
return "Hello, World!"
`

In this example, I've created a SilentGetLoggingMiddleware class that implements the process_request method. This method checks if the request method is GET and returns None to skip the logging for GET requests. Otherwise, it returns True to allow the request to be processed and logged.

Additionally, the logging module is configured directly using basicConfig to set the logging level and format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants