Coverage for /var/devmt/py/utils4_1.7.0/utils4/reporterror.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-21 20:06 +0000

1#!/usr/bin/env python 

2# -*- coding: utf-8 -*- 

3""" 

4:Purpose: This module is designed to report program errors to the 

5 console and/or a log file, using the :class:`~log.Log` 

6 class. 

7 

8:Platform: Linux/Windows | Python 3.7+ 

9:Developer: J Berendt 

10:Email: support@s3dev.uk 

11 

12:Comments: n/a 

13 

14:Example: For a usage example, refer to the :meth:`~reporterror` method's 

15 docstring. 

16 

17""" 

18# pylint: disable=multiple-statements 

19 

20import sys 

21import traceback 

22try: 

23 from .log import Log 

24except ImportError: 

25 from utils4.log import Log 

26 

27 

28if 'linux' in sys.platform.lower(): _PATH = '/tmp/reporterror.log' 

29elif 'win' in sys.platform.lower(): _PATH = 'c:/temp/reporterror.log' # pragma: nocover 

30else: _PATH = None # pragma: nocover 

31 

32 

33def reporterror(error: Exception, logevent=False, logfilepath=_PATH): 

34 """Report an error, derived from the passed ``Exception`` object. 

35 

36 Args: 

37 error (Exception): Python ``Exception`` object from the built-in 

38 try/except error handler. Refer to the use example below. 

39 logevent (bool, optional): Send the error to a log file. 

40 Defaults to False. 

41 logfilename (str, optional): Full path to the log file. Defaults to: 

42 

43 - **Linux**: ``'/tmp/reporterror.log'`` 

44 - **Windows**: ``'c:/temp/reporterror.log'`` 

45 - **Other**: ``None`` 

46 

47 Note: 

48 The ``logevent`` parameter assumes the log file exists and the 

49 header is already written. 

50 

51 For general logging help, you can use the :class:`~log.Log` 

52 class which is built into ``utils4``. 

53 

54 :Example: 

55 

56 Report a simple error to the terminal:: 

57 

58 >>> from utils4.reporterror import reporterror 

59 

60 >>> try: 

61 >>> 1/0 # Force a known error. 

62 >>> except Exception as err: 

63 >>> reporterror(err) 

64 

65 Output:: 

66 

67 ERROR: division by zero 

68 TYPE: <class 'ZeroDivisionError'> 

69 MODU: <./module/with/error.py> 

70 FUNC: <func_name> 

71 LINE: 2 

72 CMD: 1/0 

73 

74 

75 Report a simple error to the terminal and create a log file entry:: 

76 

77 >>> from utils4.reporterror import reporterror 

78 

79 >>> try: 

80 >>> 1/0 # Force a known error. 

81 >>> except Exception as err: 

82 >>> reporterror(err, logevent=True, logfilepath='/tmp/errors.log') 

83 

84 .. tip:: 

85 **Reminder:** The log file must already exist. 

86 

87 For help with the :mod:`utils4.log` module, please refer to the 

88 documentation here: :class:`~log.Log`. 

89 

90 """ 

91 exc_type, _, exc_tb = sys.exc_info() 

92 fnam, line, func, text = traceback.extract_tb(exc_tb)[-1] 

93 msg = (f'\n' 

94 f'ERROR:\t{error}\n' 

95 f'TYPE:\t{exc_type}\n' 

96 f'MODU:\t{fnam}\n' 

97 f'FUNC:\t{func}\n' 

98 f'LINE:\t{line}\n' 

99 f'CMD:\t{text}\n') 

100 print(msg) 

101 if all([logevent, logfilepath]): 

102 logger = Log(filepath=logfilepath, sep=',') 

103 msg = f'ERROR: {error}; CMD: {text}; MODULE: {fnam}; FUNC: {func}; LINE: {line}' 

104 logger.write(msg) 

105 del (exc_type, exc_tb)