Skip to content

logger

Logger

Bases: AbstractLogger

A global logger proxy to delegating messages to registered loggers.

Whenever something is written to LOGGER in code, all registered loggers are notified. Messages are also cached and cached messages written to new loggers when they are registered.

NOTE: This API is likely to change in future versions.

Source code in src/robot/output/logger.py
 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
class Logger(AbstractLogger):
    """A global logger proxy to delegating messages to registered loggers.

    Whenever something is written to LOGGER in code, all registered loggers are
    notified.  Messages are also cached and cached messages written to new
    loggers when they are registered.

    NOTE: This API is likely to change in future versions.
    """

    def __init__(self, register_console_logger=True):
        self._console_logger = None
        self._syslog = None
        self._xml_logger = None
        self._cli_listeners = None
        self._lib_listeners = None
        self._other_loggers = []
        self._message_cache = []
        self._log_message_cache = None
        self._started_keywords = 0
        self._error_occurred = False
        self._error_listener = None
        self._prev_log_message_handlers = []
        self._enabled = 0
        self._cache_only = False
        if register_console_logger:
            self.register_console_logger()

    @property
    def _listeners(self):
        cli_listeners = list(self._cli_listeners or [])
        lib_listeners = list(self._lib_listeners or [])
        return sorted(cli_listeners + lib_listeners, key=lambda li: -li.priority)

    @property
    def start_loggers(self):
        loggers = (self._other_loggers
                   + [self._console_logger, self._syslog, self._xml_logger]
                   + self._listeners)
        return [logger for logger in loggers if logger]

    @property
    def end_loggers(self):
        loggers = (self._listeners
                   + [self._console_logger, self._syslog, self._xml_logger]
                   + self._other_loggers)
        return [logger for logger in loggers if logger]

    def __iter__(self):
        return iter(self.end_loggers)

    def __enter__(self):
        if not self._enabled:
            self.register_syslog()
        self._enabled += 1

    def __exit__(self, *exc_info):
        self._enabled -= 1
        if not self._enabled:
            self.close()

    def register_console_logger(self, type='verbose', width=78, colors='AUTO',
                                links='AUTO', markers='AUTO', stdout=None, stderr=None):
        logger = ConsoleOutput(type, width, colors, links, markers, stdout, stderr)
        self._console_logger = self._wrap_and_relay(logger)

    def _wrap_and_relay(self, logger):
        self._relay_cached_messages(logger)
        return logger

    def _relay_cached_messages(self, logger):
        if self._message_cache:
            for msg in self._message_cache[:]:
                logger.message(msg)

    def unregister_console_logger(self):
        self._console_logger = None

    def register_syslog(self, path=None, level='INFO'):
        if not path:
            path = os.environ.get('ROBOT_SYSLOG_FILE', 'NONE')
            level = os.environ.get('ROBOT_SYSLOG_LEVEL', level)
        if path.upper() == 'NONE':
            return
        try:
            syslog = FileLogger(path, level)
        except DataError as err:
            self.error("Opening syslog file '%s' failed: %s" % (path, err.message))
        else:
            self._syslog = self._wrap_and_relay(syslog)

    def register_xml_logger(self, logger):
        self._xml_logger = self._wrap_and_relay(logger)

    def unregister_xml_logger(self):
        self._xml_logger = None

    def register_listeners(self, listeners, library_listeners):
        self._cli_listeners = listeners
        self._lib_listeners = library_listeners
        for listener in listeners or ():
            self._relay_cached_messages(listener)

    def register_logger(self, *loggers):
        for logger in loggers:
            logger = self._wrap_and_relay(logger)
            self._other_loggers.append(logger)

    def unregister_logger(self, *loggers):
        for logger in loggers:
            self._other_loggers = [l for l in self._other_loggers if l is not logger]

    def disable_message_cache(self):
        self._message_cache = None

    def register_error_listener(self, listener):
        self._error_listener = listener
        if self._error_occurred:
            listener()

    def message(self, msg):
        """Messages about what the framework is doing, warnings, errors, ..."""
        if not self._cache_only:
            for logger in self:
                logger.message(msg)
        if self._message_cache is not None:
            self._message_cache.append(msg)
        if msg.level == 'ERROR':
            self._error_occurred = True
            if self._error_listener:
                self._error_listener()

    @property
    @contextmanager
    def cache_only(self):
        self._cache_only = True
        try:
            yield
        finally:
            self._cache_only = False

    @property
    @contextmanager
    def delayed_logging(self):
        prev_cache = self._log_message_cache
        self._log_message_cache = []
        try:
            yield
        finally:
            messages = self._log_message_cache
            self._log_message_cache = prev_cache
            for msg in messages or ():
                self._log_message(msg, no_cache=True)

    def _log_message(self, msg, no_cache=False):
        """Log messages written (mainly) by libraries."""
        if self._log_message_cache is not None and not no_cache:
            msg.resolve_delayed_message()
            self._log_message_cache.append(msg)
            return
        for logger in self:
            logger.log_message(msg)
        if msg.level in ('WARN', 'ERROR'):
            self.message(msg)

    log_message = message

    def log_output(self, output):
        for msg in StdoutLogSplitter(output):
            self.log_message(msg)

    def enable_library_import_logging(self):
        self._prev_log_message_handlers.append(self.log_message)
        self.log_message = self.message

    def disable_library_import_logging(self):
        self.log_message = self._prev_log_message_handlers.pop()

    def start_suite(self, data, result):
        for logger in self.start_loggers:
            logger.start_suite(data, result)

    def end_suite(self, data, result):
        for logger in self.end_loggers:
            logger.end_suite(data, result)

    def start_test(self, data, result):
        for logger in self.start_loggers:
            logger.start_test(data, result)

    def end_test(self, data, result):
        for logger in self.end_loggers:
            logger.end_test(data, result)

    @start_body_item
    def start_keyword(self, data, result):
        for logger in self.start_loggers:
            logger.start_keyword(data, result)

    @end_body_item
    def end_keyword(self, data, result):
        for logger in self.end_loggers:
            logger.end_keyword(data, result)

    @start_body_item
    def start_user_keyword(self, data, implementation, result):
        for logger in self.start_loggers:
            logger.start_user_keyword(data, implementation, result)

    @end_body_item
    def end_user_keyword(self, data, implementation, result):
        for logger in self.end_loggers:
            logger.end_user_keyword(data, implementation, result)

    @start_body_item
    def start_library_keyword(self, data, implementation, result):
        for logger in self.start_loggers:
            logger.start_library_keyword(data, implementation, result)

    @end_body_item
    def end_library_keyword(self, data, implementation, result):
        for logger in self.end_loggers:
            logger.end_library_keyword(data, implementation, result)

    @start_body_item
    def start_invalid_keyword(self, data, implementation, result):
        for logger in self.start_loggers:
            logger.start_invalid_keyword(data, implementation, result)

    @end_body_item
    def end_invalid_keyword(self, data, implementation, result):
        for logger in self.end_loggers:
            logger.end_invalid_keyword(data, implementation, result)

    @start_body_item
    def start_for(self, data, result):
        for logger in self.start_loggers:
            logger.start_for(data, result)

    @end_body_item
    def end_for(self, data, result):
        for logger in self.end_loggers:
            logger.end_for(data, result)

    @start_body_item
    def start_for_iteration(self, data, result):
        for logger in self.start_loggers:
            logger.start_for_iteration(data, result)

    @end_body_item
    def end_for_iteration(self, data, result):
        for logger in self.end_loggers:
            logger.end_for_iteration(data, result)

    @start_body_item
    def start_while(self, data, result):
        for logger in self.start_loggers:
            logger.start_while(data, result)

    @end_body_item
    def end_while(self, data, result):
        for logger in self.end_loggers:
            logger.end_while(data, result)

    @start_body_item
    def start_while_iteration(self, data, result):
        for logger in self.start_loggers:
            logger.start_while_iteration(data, result)

    @end_body_item
    def end_while_iteration(self, data, result):
        for logger in self.end_loggers:
            logger.end_while_iteration(data, result)

    @start_body_item
    def start_if(self, data, result):
        for logger in self.start_loggers:
            logger.start_if(data, result)

    @end_body_item
    def end_if(self, data, result):
        for logger in self.end_loggers:
            logger.end_if(data, result)

    @start_body_item
    def start_if_branch(self, data, result):
        for logger in self.start_loggers:
            logger.start_if_branch(data, result)

    @end_body_item
    def end_if_branch(self, data, result):
        for logger in self.end_loggers:
            logger.end_if_branch(data, result)

    @start_body_item
    def start_try(self, data, result):
        for logger in self.start_loggers:
            logger.start_try(data, result)

    @end_body_item
    def end_try(self, data, result):
        for logger in self.end_loggers:
            logger.end_try(data, result)

    @start_body_item
    def start_try_branch(self, data, result):
        for logger in self.start_loggers:
            logger.start_try_branch(data, result)

    @end_body_item
    def end_try_branch(self, data, result):
        for logger in self.end_loggers:
            logger.end_try_branch(data, result)

    @start_body_item
    def start_var(self, data, result):
        for logger in self.start_loggers:
            logger.start_var(data, result)

    @end_body_item
    def end_var(self, data, result):
        for logger in self.end_loggers:
            logger.end_var(data, result)

    @start_body_item
    def start_break(self, data, result):
        for logger in self.start_loggers:
            logger.start_break(data, result)

    @end_body_item
    def end_break(self, data, result):
        for logger in self.end_loggers:
            logger.end_break(data, result)

    @start_body_item
    def start_continue(self, data, result):
        for logger in self.start_loggers:
            logger.start_continue(data, result)

    @end_body_item
    def end_continue(self, data, result):
        for logger in self.end_loggers:
            logger.end_continue(data, result)

    @start_body_item
    def start_return(self, data, result):
        for logger in self.start_loggers:
            logger.start_return(data, result)

    @end_body_item
    def end_return(self, data, result):
        for logger in self.end_loggers:
            logger.end_return(data, result)

    @start_body_item
    def start_error(self, data, result):
        for logger in self.start_loggers:
            logger.start_error(data, result)

    @end_body_item
    def end_error(self, data, result):
        for logger in self.end_loggers:
            logger.end_error(data, result)

    def library_import(self, library, importer):
        for logger in self:
            logger.library_import(library, importer)

    def resource_import(self, resource, importer):
        for logger in self:
            logger.resource_import(resource, importer)

    def variables_import(self, variables, importer):
        for logger in self:
            logger.variables_import(variables, importer)

    def output_file(self, path):
        for logger in self:
            logger.output_file(path)

    def report_file(self, path):
        for logger in self:
            logger.report_file(path)

    def log_file(self, path):
        for logger in self:
            logger.log_file(path)

    def xunit_file(self, path):
        for logger in self:
            logger.xunit_file(path)

    def debug_file(self, path):
        for logger in self:
            logger.debug_file(path)

    def result_file(self, kind, path):
        kind_file = getattr(self, f'{kind.lower()}_file')
        kind_file(path)

    def close(self):
        for logger in self:
            logger.close()
        self.__init__(register_console_logger=False)

message(msg)

Messages about what the framework is doing, warnings, errors, ...

Source code in src/robot/output/logger.py
def message(self, msg):
    """Messages about what the framework is doing, warnings, errors, ..."""
    if not self._cache_only:
        for logger in self:
            logger.message(msg)
    if self._message_cache is not None:
        self._message_cache.append(msg)
    if msg.level == 'ERROR':
        self._error_occurred = True
        if self._error_listener:
            self._error_listener()