So far I am doing simple logging in files, and if I write a multi-line string, the result is as follows:
Log output:
logging.info('foo\nbar')
Logfile:
2018-03-05 10:51:53 root.main +16: INFO [28302] foo
bar
So far, all statements that do not contain "INFO" or "DEBUG" are reported to the statements.
This means that the string bar being reported. This is a false positive.
Environment: Linux.
How to set up logging in Python to keep INFO foo\nbar on one line and ignore the whole line, since this is only "INFO"?
Note. Yes, you can filter logging in the interpreter. Unfortunately, this is not a question. This question is different. Logging occurs first. Then the logs are analyzed.
Here is a script to reproduce it:
import sys import logging def set_up_logging(level=logging.INFO): root_logger = logging.getLogger() root_logger.setLevel(level) handler = logging.StreamHandler(sys.stdout) handler.setFormatter( logging.Formatter('%(asctime)s %(name)s: %(levelname)-8s [%(process)d] %(message)s', '%Y-%m-%d %H:%M:%S')) root_logger.addHandler(handler) def main(): set_up_logging() logging.info('foo\nbar') if __name__ == '__main__': main()
Thinking about it again, I think the real question is: what logging format is possible? Just deleting lines in messages that span multiple lines makes it difficult to read some data for the human eye. On the other hand, the current 1: 1 ratio between logging.info () and the line in the log file is easy to read .... I'm not sure
source share