source: loggingpy/tudelft/utilities/logging/ReportToLogger.py@ 204

Last change on this file since 204 was 204, checked in by wouter, 3 years ago

#87

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