Edgewall Software
Modify

Opened 5 years ago

Closed 3 years ago

Last modified 3 years ago

#11874 closed enhancement (fixed)

Allowing logger to be extended with different handlers

Reported by: Ryan J Ollos Owned by: Ryan J Ollos
Priority: normal Milestone: 1.3.1
Component: general Version:
Severity: normal Keywords: logging
Cc: Branch:
Release Notes:

EnvironmentStub logs to a MemoryHandler and a list of (debug_level, message) tuples can be retrieved through the log_messages property.

API Changes:
  • logger_handler_factory no longer returns the handler as the second return parameter. The handler can be accessed through Environment.log.handlers[0].
  • Added function trac.log.shutdown for cleaning up logger handlers.
  • Added dictionary LOG_LEVEL_MAP in trac.log module, for mapping string log levels to integer log levels.

Description (last modified by Ryan J Ollos)

Adding a LoggerManager Component and a ILoggingHandlerProvider interface would allow new handlers to be added through plugins. It should also be possible to eliminate some redundant definitions of log types in log.py and admin/web_ui.py, and provide a cleaner definition of log levels.

One handler that will be of immediate use is the addition of MockLoggingHandler in test.py, taken from SO:1049375/121694.

Attachments (0)

Change History (16)

comment:1 by Ryan J Ollos, 5 years ago

Description: modified (diff)
Keywords: logging added; log removed
Type: defectenhancement

comment:2 by Ryan J Ollos, 5 years ago

Description: modified (diff)

comment:3 by Ryan J Ollos, 5 years ago

Description: modified (diff)

comment:4 by Ryan J Ollos, 5 years ago

Milestone: 1.1.41.1.5

Narrowing focus for 1.1.4.

comment:5 by Ryan J Ollos, 4 years ago

Milestone: 1.1.51.2

comment:6 by Ryan J Ollos, 4 years ago

Milestone: 1.21.1.6

Milestone renamed

comment:7 by Ryan J Ollos, 4 years ago

Milestone: 1.1.6next-dev-1.1.x

comment:8 by Ryan J Ollos, 4 years ago

Owner: Ryan J Ollos removed

comment:9 by Ryan J Ollos, 4 years ago

Milestone: next-dev-1.1.xnext-dev-1.3.x

Narrowing focus for milestone:1.2. Please move ticket to milestone:1.2 if you intend to fix it.

comment:10 by Ryan J Ollos, 4 years ago

The changes in this ticket can also include the addition of LogRecordFilter proposed in #7820. The change was not included in that ticket because it would have resulted in an API change.


class LogRecordFilter(logging.Filter):
    def __init__(self, path, basename, project):
        super(LogRecordFilter, self).__init__()
        self.path = path
        self.basename = basename
        self.project = project

    def filter(self, record):
        record.path = self.path
        record.basename = self.basename
        record.project = self.project

        return True

comment:11 by Ryan J Ollos, 3 years ago

Milestone: next-dev-1.3.x1.3.1
Owner: set to Ryan J Ollos
Status: newassigned

Since #7820 allows the logger to be configured from a file, rather than adding an extension point I think we can just use StreamHandler in EnvironmentStub, and have EnvironmentStub utilize StreamHandler to log to a stream buffer target that we can make assertions again.

Last edited 3 years ago by Ryan J Ollos (previous) (diff)

comment:12 by Ryan J Ollos, 3 years ago

Proposed changes in log:rjollos.git:t11874_mock_logger.

I'd like to avoid use of StringIO.StringIO after #12046, however I'm having some trouble with that.

Use of io.StringIO results in:

Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 882, in emit
    stream.write(fs % msg)
TypeError: unicode argument expected, got 'str'
Logged from file main.py, line 191

Use of io.BytesIO results in:

Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 882, in emit
    stream.write(fs % msg)
TypeError: 'unicode' does not have the buffer interface
Logged from file main.py, line 176
Last edited 3 years ago by Ryan J Ollos (previous) (diff)

in reply to:  12 comment:13 by Ryan J Ollos, 3 years ago

Replying to Ryan J Ollos:

Proposed changes in log:rjollos.git:t11874_mock_logger.

I'd like to avoid use of StringIO.StringIO after #12046, however I'm having some trouble with that.

Changed to use BufferingHandler in [81740a12/rjollos.git].

comment:14 by Ryan J Ollos, 3 years ago

[ab85dc14/rjollos.git] changes EnvironmentStub.log_messages to return a list of tuples with (level_name, message).

comment:15 by Jun Omae, 3 years ago

Looks good to me. However I'm not sure about adding self.assertEqual('DEBUG', level) in [ab85dc14/rjollos.git].

It seems that test case confirms Can't authenticate using ... in log messages from rjollos.git/trac/web/main.py@ab85dc14:174-176#L169. I think the test case should be this:

  • trac/web/tests/main.py

    diff --git a/trac/web/tests/main.py b/trac/web/tests/main.py
    index f9fcb6db2..0cefc6711 100644
    a b class AuthenticateTestCase(unittest.TestCase):  
    120120                         self.request_dispatcher.authenticate(self.req))
    121121        self.assertEqual(1, len(self.req.chrome['warnings']))
    122122        expected = "Can't authenticate using RaisingAuthenticator: "
    123         level = None
    124123        for level, message in self.env.log_messages:
    125124            if expected in message.split('\n'):
     125                self.assertEqual('ERROR', level)
    126126                break
    127127        else:
    128             self.assertEqual('DEBUG', level)
    129128            self.fail("Expected log message not found: \"%s\"" % expected)
    130129
    131130    def test_authenticate_once(self):
Last edited 3 years ago by Jun Omae (previous) (diff)

comment:16 by Ryan J Ollos, 3 years ago

API Changes: modified (diff)
Release Notes: modified (diff)
Resolution: fixed
Status: assignedclosed

Thanks for spotting that error. Changes committed to trunk in r15129, including patch from comment:15. Additional changes in r15130. I'll continue with related work on logging system in #7820.

The Travis CI build failures on OSX were resolved by clearing the caches.

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Ryan J Ollos.
The resolution will be deleted. Next status will be 'reopened'.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.