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
« 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.
8:Platform: Linux/Windows | Python 3.7+
9:Developer: J Berendt
10:Email: support@s3dev.uk
12:Comments: n/a
14:Example: For a usage example, refer to the :meth:`~reporterror` method's
15 docstring.
17"""
18# pylint: disable=multiple-statements
20import sys
21import traceback
22try:
23 from .log import Log
24except ImportError:
25 from utils4.log import Log
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
33def reporterror(error: Exception, logevent=False, logfilepath=_PATH):
34 """Report an error, derived from the passed ``Exception`` object.
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:
43 - **Linux**: ``'/tmp/reporterror.log'``
44 - **Windows**: ``'c:/temp/reporterror.log'``
45 - **Other**: ``None``
47 Note:
48 The ``logevent`` parameter assumes the log file exists and the
49 header is already written.
51 For general logging help, you can use the :class:`~log.Log`
52 class which is built into ``utils4``.
54 :Example:
56 Report a simple error to the terminal::
58 >>> from utils4.reporterror import reporterror
60 >>> try:
61 >>> 1/0 # Force a known error.
62 >>> except Exception as err:
63 >>> reporterror(err)
65 Output::
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
75 Report a simple error to the terminal and create a log file entry::
77 >>> from utils4.reporterror import reporterror
79 >>> try:
80 >>> 1/0 # Force a known error.
81 >>> except Exception as err:
82 >>> reporterror(err, logevent=True, logfilepath='/tmp/errors.log')
84 .. tip::
85 **Reminder:** The log file must already exist.
87 For help with the :mod:`utils4.log` module, please refer to the
88 documentation here: :class:`~log.Log`.
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)