I have this peace of code from a library that raise a warning :
report_methods = {
"missing_bus_indices": diag_report.report_missing_bus_indices,
"disconnected_elements": diag_report.report_disconnected_elements,
"different_voltage_levels_connected": diag_report.report_different_voltage_levels_connected,
"impedance_values_close_to_zero": diag_report.report_impedance_values_close_to_zero,
"nominal_voltages_dont_match": diag_report.report_nominal_voltages_dont_match,
"invalid_values": diag_report.report_invalid_values,
"overload": diag_report.report_overload,
"multiple_voltage_controlling_elements_per_bus": diag_report.report_multiple_voltage_controlling_elements_per_bus,
"wrong_switch_configuration": diag_report.report_wrong_switch_configuration,
"no_ext_grid": diag_report.report_no_ext_grid,
"wrong_reference_system": diag_report.report_wrong_reference_system,
"deviation_from_std_type": diag_report.report_deviation_from_std_type,
"numba_comparison": diag_report.report_numba_comparison,
"parallel_switches": diag_report.report_parallel_switches
}
# separator between log messages
log_message_sep = ("\n --------\n")
logger.warning("\n\n_____________ PANDAPOWER DIAGNOSTIC TOOL _____________ \n")
for key in report_methods:
if (key in diag_results) :
report_methods[key]()
logger.warning(log_message_sep)
logger.warning("_____________ END OF PANDAPOWER DIAGNOSTIC _____________ ")
Here is the kind of result I get in my python console :
_____________ PANDAPOWER DIAGNOSTIC TOOL _____________
--------
disconnected_elements:
disonnected_section: {'buses': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122], 'lines': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120], 'loads': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120]}
--------
--------
impedance_values_close_to_zero:
line 0: r_ohm <= 0.0005 or x_ohm <= 0.0001
--------
--------
invalid_values:
line:
line 0: 'length_km' = 0.0 (restriction: >0)
--------
--------
--------
--------
--------
--------
--------
--------
--------
_____________ END OF PANDAPOWER DIAGNOSTIC _____________
I would like to write this content into a txt file with f.write I tried :
for key in report_methods:
if (key in diag_results) :
if not report_methods[key]() is None :
f.write(report_methods[key]())
f.write(log_message_sep)
But it seams that f.write(report_methods[key]())
is not working properly : the txt file does not contain the information needed.
How should I proceed ?
I think all you need to do is add a FileHandler to your logging config, which will write all log messages (including log messages written by other libraries) to a designated file (unless the other libraries define an alternate set of handlers for their own loggers, which is unlikely since that would be terrible practice, and doesn't look to be the case in the link you provided).
I don't know what your logging config looks like (if you set one), but here's an example:
Note that you can specify different logging levels for each handler. In the example, I set a global level of
DEBUG
(so almost any log message is allowed), but for the FileHandler, I limit the level to justINFO
, but you could limit it to onlyWARNING
if you wish. Here are the available logging levels if you aren't familiar (each level allows every level above it).I'm ~95% sure that this is what you need.