~brendan-donegan/checkbox/bug1196531

« back to all changes in this revision

Viewing changes to plainbox/plainbox/impl/logging.py

  • Committer: Tarmac
  • Author(s): Zygmunt Krynicki
  • Date: 2013-06-21 14:14:16 UTC
  • mfrom: (2199.1.11 launchpad/misc)
  • Revision ID: tarmac-20130621141416-hv12juje6qdwfbes
"[r=roadmr][bug=][author=zkrynicki] automatic merge by tarmac"

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
from plainbox.impl.color import ansi_on, ansi_off
37
37
 
38
38
 
 
39
logger = logging.getLogger("plainbox.logging")
 
40
 
39
41
# XXX: enable ansi escape sequences if sys.std{out,err} are both TTYs
40
42
#
41
43
# This is a bad place to take this decision (ideally we'd do that per log
91
93
        # defined for all of the logging subsystem in this python runtime
92
94
        logging.config.dictConfig(self.DEFAULT_CONFIG)
93
95
 
94
 
    def adjust_logging(self, level=None, trace_list=None):
 
96
    def adjust_logging(self, level=None, trace_list=None, debug_console=False):
95
97
        # Bump logging on the root logger if requested
96
98
        if level is not None:
97
99
            logging.getLogger(None).setLevel(level)
 
100
            logger.debug("Enabled %r on root logger", level)
98
101
            logging.getLogger("plainbox").setLevel(level)
99
102
        # Enable tracing on specified loggers
100
103
        if trace_list is not None:
101
104
            for name in trace_list:
102
105
                logging.getLogger(name).setLevel(logging.DEBUG)
 
106
                logger.debug("Enabled debugging on logger %r", name)
 
107
        if debug_console and (level == 'DEBUG' or trace_list):
 
108
            # Enable DEBUG logging to console if explicitly requested
 
109
            logging.config.dictConfig(self.DEBUG_CONSOLE_CONFIG)
103
110
 
104
111
    @property
105
112
    def log_dir(self):
115
122
        return {
116
123
            "version": 1,
117
124
            "formatters": {
 
125
                "console_debug": {
 
126
                    "()": "plainbox.impl.logging.ANSIFormatter",
 
127
                    "format": (
 
128
                        "{ansi.f.BLACK}{ansi.s.BRIGHT}"
 
129
                        "%(levelname)s"
 
130
                        "{ansi.s.NORMAL}{ansi.f.RESET}"
 
131
                        " "
 
132
                        "{ansi.f.CYAN}{ansi.s.DIM}"
 
133
                        "%(name)s"
 
134
                        "{ansi.f.RESET}{ansi.s.NORMAL}"
 
135
                        ": "
 
136
                        "{ansi.s.DIM}"
 
137
                        "%(message)s"
 
138
                        "{ansi.s.NORMAL}"
 
139
                    ),
 
140
                },
118
141
                "console_info": {
119
142
                    "()": "plainbox.impl.logging.ANSIFormatter",
120
143
                    "format": (
122
145
                        "%(levelname)s"
123
146
                        "{ansi.s.NORMAL}{ansi.f.RESET}"
124
147
                        " "
125
 
                        "{ansi.f.CYAN}%(name)s{ansi.f.RESET}"
 
148
                        "{ansi.f.CYAN}{ansi.s.BRIGHT}"
 
149
                        "%(name)s"
 
150
                        "{ansi.f.RESET}{ansi.s.NORMAL}"
126
151
                        ": "
127
152
                        "%(message)s"
128
153
                    ),
178
203
                },
179
204
            },
180
205
            "handlers": {
 
206
                "console_debug": {
 
207
                    "class": "logging.StreamHandler",
 
208
                    "stream": "ext://sys.stdout",
 
209
                    "formatter": "console_debug",
 
210
                    "filters": ["only_debug"],
 
211
                    "level": 150,
 
212
                },
181
213
                "console_info": {
182
214
                    "class": "logging.StreamHandler",
183
215
                    "stream": "ext://sys.stdout",
229
261
                "plainbox": {
230
262
                    "level": "WARNING",
231
263
                    "handlers": [
 
264
                        "console_debug",
232
265
                        "console_info",
233
266
                        "console_warning",
234
267
                        "console_error",
248
281
            "disable_existing_loggers": True,
249
282
        }
250
283
 
 
284
    @property
 
285
    def DEBUG_CONSOLE_CONFIG(self):
 
286
        return {
 
287
            "version": 1,
 
288
            "handlers": {
 
289
                "console_debug": {
 
290
                    "level": "DEBUG",
 
291
                },
 
292
            },
 
293
            "incremental": True,
 
294
        }
 
295
 
251
296
 
252
297
# Instantiate the helper
253
298
_LoggingHelper = LoggingHelper()