cocotb-test
cocotb-test copied to clipboard
configure logger with dictConfig
Allow the python logs to go to a file and console at the same time.
Used the logging.config.dictConfig() to configure the loggers with two handlers: ->console output using logging.StreamHandler ->file output using logging.FileHandler Independent configs can be selected for each handlers.
The ansi characters are stripped by setting the formatter of file output handler to NoAnsiFormatter.
More handlers and settings can easily be added to the dictConfig.
Would not be better to be done by the user in the command line/test code?
Having hooks to control from the calling test scrip would be good. I'm not sure what the best way is. But right now the log file isn't created anywhere. I feel it should happen by default because that is what most user want. For example the cocotb makefiles do output the entire console content to a file.
A more direct alternative only adding a file handler output would be something like:
self.logger = logging.getLogger("cocotb")
self.logger.setLevel(logging.INFO)
logging.basicConfig(format="%(levelname)s %(name)s: %(message)s", handlers=[logging.StreamHandler()])
fileh = logging.FileHandler(self.sim_dir + "/sim.log")
formatter = NoAnsiFormatter("%(message)s")
fileh.setFormatter(formatter)
self.logger.addHandler(fileh)