Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
Some exception objects carry additional fields. For example, a subprocess.CalledProcessError() exception carries returncode, output (the stdout), stderr, etc. It’s useful information, but self.fo...
#2: Post edited
- Some exception objects carry additional fields. For example, a `subprocess.CalledProcessError()` exception carries `returncode`, `output` (the stdout), `stderr`, etc. It’s useful information, but self.formatException() ignores it. You can add a few lines which format the extra fields, and save them as JSON next to the common exception data.
```- def format(self, record) -> str:
- log_record_dict = { "time": self.formatTime(record), "level": record.levelname, "message": record.message }
- if record.exc_info is not None:
- _, exc_value, _ = record.exc_info
- log_record_dict.update(vars(exc_value)) # <-- log the extra fields
- log_record_dict["exception"] = self.formatException(record.exc_info)
- log_record_dict.update(record.args)
- return json.dumps(log_record_dict, indent=None)
- ```
- Some exception objects carry additional fields. For example, a `subprocess.CalledProcessError()` exception carries `returncode`, `output` (the stdout), `stderr`, etc. It’s useful information, but self.formatException() ignores it. You can add a few lines which format the extra fields, and save them as JSON next to the common exception data.
- ``` python
- def format(self, record) -> str:
- log_record_dict = { "time": self.formatTime(record), "level": record.levelname, "message": record.message }
- if record.exc_info is not None:
- _, exc_value, _ = record.exc_info
- log_record_dict.update(vars(exc_value)) # <-- log the extra fields
- log_record_dict["exception"] = self.formatException(record.exc_info)
- log_record_dict.update(record.args)
- return json.dumps(log_record_dict, indent=None)
- ```
#1: Initial revision
Some exception objects carry additional fields. For example, a `subprocess.CalledProcessError()` exception carries `returncode`, `output` (the stdout), `stderr`, etc. It’s useful information, but self.formatException() ignores it. You can add a few lines which format the extra fields, and save them as JSON next to the common exception data.
```
def format(self, record) -> str:
log_record_dict = { "time": self.formatTime(record), "level": record.levelname, "message": record.message }
if record.exc_info is not None:
_, exc_value, _ = record.exc_info
log_record_dict.update(vars(exc_value)) # <-- log the extra fields
log_record_dict["exception"] = self.formatException(record.exc_info)
log_record_dict.update(record.args)
return json.dumps(log_record_dict, indent=None)
```
