[949] | 1 | from logging import Logger
|
---|
| 2 | import logging
|
---|
| 3 | from typing import cast
|
---|
| 4 |
|
---|
| 5 | from tudelft_utilities_logging.Reporter import Reporter
|
---|
| 6 |
|
---|
| 7 |
|
---|
| 8 | # initialize the logging, otherwise it seems not to work properly
|
---|
| 9 | logging.basicConfig()
|
---|
| 10 |
|
---|
| 11 | class ReportToFile ( Reporter ):
|
---|
| 12 | '''
|
---|
| 13 | dumps the reported messages into a log file with the
|
---|
| 14 | given name.
|
---|
| 15 | This really tries hard to avoid any writing to
|
---|
| 16 | stdout/stderr, because those are used
|
---|
| 17 | for the communicatino to the party.
|
---|
| 18 | '''
|
---|
| 19 |
|
---|
| 20 | def __init__(self, logname:str):
|
---|
| 21 | '''
|
---|
| 22 | @param logname the name for the Logger
|
---|
| 23 | '''
|
---|
| 24 | super().__init__()
|
---|
| 25 | self._logger:Logger = logging.getLogger(logname)
|
---|
| 26 | # HACK to remove parent. Parent prints to stdout and
|
---|
| 27 | # preventing that is exactly why we have a logger here.
|
---|
| 28 | self._logger.parent=cast(Logger, None)
|
---|
| 29 | handler = logging.FileHandler(logname+".log")
|
---|
| 30 | formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
---|
| 31 | handler.setFormatter(formatter)
|
---|
| 32 | self._logger.addHandler(handler)
|
---|
| 33 | self._logger.setLevel(logging.INFO)
|
---|
| 34 |
|
---|
| 35 | def log(self, level:int , msg:str, thrown: BaseException=None) -> None:
|
---|
| 36 | # We use the internal function, the only way to include our exception...
|
---|
| 37 | self._logger._log(level=level, msg=msg, args=[], exc_info=thrown,\
|
---|
| 38 | stack_info=True if thrown else False)
|
---|
| 39 |
|
---|
| 40 |
|
---|
| 41 | |
---|