<commit_before>##
# See the file COPYRIGHT for copyright information.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##

"""
Monkey patch L{twisted.web} logging, which annoyingly doesn't let you log the
username.
"""

__all__ = []

from zope.interface import provider
from twisted.web.iweb import IAccessLogFormatter
from twisted.web.http import _escape
import twisted.web.http



@provider(IAccessLogFormatter)
def combinedLogFormatter(timestamp, request):
    """
    @return: A combined log formatted log line for the given request.

    @see: L{IAccessLogFormatter}
    """
    referrer = _escape(request.getHeader(b"referer") or b"-")
    agent = _escape(request.getHeader(b"user-agent") or b"-")

    if hasattr(request, "user") and request.user:
        try:
            user = _escape(request.user)
        except Exception:
            user = _escape(repr(request.user))
    else:
        user = b"-"

    line = (
        u'"%(ip)s" %(user)s - %(timestamp)s "%(method)s %(uri)s %(protocol)s" '
        u'%(code)d %(length)s "%(referrer)s" "%(agent)s"' % dict(
            ip=_escape(request.getClientIP() or b"-"),
            timestamp=timestamp,
            method=_escape(request.method),
            uri=_escape(request.uri),
            protocol=_escape(request.clientproto),
            code=request.code,
            length=request.sentLength or u"-",
            referrer=referrer,
            agent=agent,
            user=user,
        )
    )
    return line



twisted.web.http.combinedLogFormatter = combinedLogFormatter
<commit_msg>Clean up; do patching in a function.<commit_after>##
# See the file COPYRIGHT for copyright information.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##

"""
Monkey patch L{twisted.web} logging, which annoyingly doesn't let you log the
username.
"""

__all__ = []

from zope.interface import provider
from twisted.web.iweb import IAccessLogFormatter
from twisted.web.http import _escape
import twisted.web.http



@provider(IAccessLogFormatter)
def combinedLogFormatter(timestamp, request):
    """
    @return: A combined log formatted log line for the given request.

    @see: L{IAccessLogFormatter}
    """
    referrer = _escape(request.getHeader(b"referer") or b"-")
    agent = _escape(request.getHeader(b"user-agent") or b"-")

    if hasattr(request, "user") and request.user is not None:
        uid = request.user.uid
        try:
            username = _escape(uid)
        except Exception:
            username = _escape(repr(uid))
    else:
        username = u"-"

    line = (
        u'"%(ip)s" %(user)s - %(timestamp)s "%(method)s %(uri)s %(protocol)s" '
        u'%(code)d %(length)s "%(referrer)s" "%(agent)s"' % dict(
            ip=_escape(request.getClientIP() or b"-"),
            timestamp=timestamp,
            method=_escape(request.method),
            uri=_escape(request.uri),
            protocol=_escape(request.clientproto),
            code=request.code,
            length=request.sentLength or u"-",
            referrer=referrer,
            agent=agent,
            user=username,
        )
    )
    return line



def patchCombinedLogFormatter():
    twisted.web.http.combinedLogFormatter = combinedLogFormatter

{'fraction_non_alphanumeric': 0.11018760389456186, 'fraction_numerical': 0.00189978627404417, 'mean_word_length': 3.0038022813688214, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5586821', 'n_tokens_mistral': 1219, 'n_tokens_neox': 1201, 'n_words': 379}