Robot Framework
pyloggingconf.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 from contextlib import contextmanager
17 import logging
18 
19 from robot.utils import get_error_details, safe_str
20 
21 from . import librarylogger
22 
23 
24 LEVELS = {'TRACE': logging.NOTSET,
25  'DEBUG': logging.DEBUG,
26  'INFO': logging.INFO,
27  'WARN': logging.WARNING,
28  'ERROR': logging.ERROR}
29 
30 
31 @contextmanager
33  root = logging.getLogger()
34  if any(isinstance(h, RobotHandler) for h in root.handlers):
35  yield
36  return
37  handler = RobotHandler()
38  old_raise = logging.raiseExceptions
39  root.addHandler(handler)
40  logging.raiseExceptions = False
41  set_level(level)
42  try:
43  yield
44  finally:
45  root.removeHandler(handler)
46  logging.raiseExceptions = old_raise
47 
48 
49 def set_level(level):
50  try:
51  level = LEVELS[level.upper()]
52  except KeyError:
53  return
54  logging.getLogger().setLevel(level)
55 
56 
57 class RobotHandler(logging.Handler):
58 
59  def __init__(self, level=logging.NOTSET, library_logger=librarylogger):
60  super().__init__(level)
61  self.library_loggerlibrary_logger = library_logger
62 
63  def emit(self, record):
64  message, error = self._get_message_get_message(record)
65  method = self._get_logger_method_get_logger_method(record.levelno)
66  method(message)
67  if error:
68  self.library_loggerlibrary_logger.debug(error)
69 
70  def _get_message(self, record):
71  try:
72  return self.format(record), None
73  except:
74  message = 'Failed to log following message properly: %s' \
75  % safe_str(record.msg)
76  error = '\n'.join(get_error_details())
77  return message, error
78 
79  def _get_logger_method(self, level):
80  if level >= logging.ERROR:
81  return self.library_loggerlibrary_logger.error
82  if level >= logging.WARNING:
83  return self.library_loggerlibrary_logger.warn
84  if level >= logging.INFO:
85  return self.library_loggerlibrary_logger.info
86  if level >= logging.DEBUG:
87  return self.library_loggerlibrary_logger.debug
88  return self.library_loggerlibrary_logger.trace
def __init__(self, level=logging.NOTSET, library_logger=librarylogger)
def debug(msg, html=False)
Writes the message to the log file using the DEBUG level.
Definition: logger.py:104
def get_error_details(full_traceback=True, exclude_robot_traces=EXCLUDE_ROBOT_TRACES)
Returns error message and details of the last occurred exception.
Definition: error.py:39
def safe_str(item)
Definition: unic.py:21