File: logger_plugin.py

package info (click to toggle)
python-apptools 5.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,552 kB
  • sloc: python: 9,868; makefile: 80
file content (109 lines) | stat: -rw-r--r-- 3,485 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# (C) Copyright 2005-2025 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
""" Logger plugin.
"""

# Standard library imports.
import logging

# Enthought library imports.
from envisage.api import ExtensionPoint, Plugin
from apptools.logger.log_queue_handler import LogQueueHandler
from traits.api import Callable, List

# Local imports.
from .logger_preferences import LoggerPreferences
from .logger_service import LoggerService


ID = "apptools.logger"
ILOGGER = ID + ".plugin.logger_service.LoggerService"


class LoggerPlugin(Plugin):
    """Logger plugin."""

    id = ID
    name = "Logger plugin"

    #### Extension points for this plugin #####################################

    MAIL_FILES = "apptools.logger.plugin.mail_files"

    mail_files = ExtensionPoint(
        List(Callable),
        id=MAIL_FILES,
        desc="""

        This extension point allows you to contribute functions which will be
        called to add project files to the zip file that the user mails back
        with bug reports from the Quality Agent.

        The function will be passed a zipfile.ZipFile object.

        """,
    )

    #### Contributions to extension points made by this plugin ################

    PREFERENCES = "envisage.preferences"
    PREFERENCES_PAGES = "envisage.ui.workbench.preferences_pages"
    VIEWS = "envisage.ui.workbench.views"

    preferences = List(contributes_to=PREFERENCES)
    preferences_pages = List(contributes_to=PREFERENCES_PAGES)
    views = List(contributes_to=VIEWS)

    def _preferences_default(self):
        return ["pkgfile://%s/plugin/preferences.ini" % ID]

    def _preferences_pages_default(self):
        from apptools.logger.plugin.view.logger_preferences_page import (
            LoggerPreferencesPage,
        )

        return [LoggerPreferencesPage]

    def _views_default(self):
        return [self._logger_view_factory]

    #### Plugin interface #####################################################

    def start(self):
        """Starts the plugin."""
        preferences = LoggerPreferences()
        service = LoggerService(
            application=self.application, preferences=preferences
        )
        formatter = logging.Formatter("%(levelname)s|%(asctime)s|%(message)s")
        handler = LogQueueHandler()
        handler.setLevel(preferences.level_)
        handler.setFormatter(formatter)
        root_logger = logging.getLogger()
        root_logger.addHandler(handler)
        root_logger.setLevel(preferences.level_)
        service.handler = handler
        self.application.register_service(ILOGGER, service)

    def stop(self):
        """Stops the plugin."""
        service = self.application.get_service(ILOGGER)
        service.save_preferences()

    #### LoggerPlugin private interface #######################################

    def _logger_view_factory(self, **traits):
        from apptools.logger.plugin.view.logger_view import LoggerView

        service = self.application.get_service(ILOGGER)
        view = LoggerView(service=service, **traits)
        # Record the created view on the service.
        service.plugin_view = view
        return view