0

I did this to save info to my file, but I also got output in my console. What can I do not to see info in my console, but to see it in my file?

logger = logging.getLogger('resource_manager')
logger.setLevel(logging.DEBUG)
info_log = logging.FileHandler('/home/nvidia/Videos/Info.log')
info_log.setFormatter(logging.Formatter('%(asctime)s - %(message)s'))
info_log.setLevel(logging.INFO)
logger.addHandler(info_log)

logger.info(f'{(time.time()-delay)}')
Helen
  • 27
  • 5

3 Answers3

0

Here, you have added a handler to write to a file. What you need to do is remove the default handler that writes to STDERR.

Cargo23
  • 3,064
  • 16
  • 25
0

You can disable propagation of events from this logger to its parent (the root logger), which will prevent them from being handled by the root logger's StreamHandler (which is what writes to standard error).

logger = logging.getLogger('resource_manager')
logger.propagate = False
chepner
  • 497,756
  • 71
  • 530
  • 681
0

I did this to save info to my file, but I also got output in my console.

That may mean that some other logging configuration is already active (for example, are you sure that logging.basicConfig() has not been called in the same Python program?); in particular, that the root logger has a StreamHandler attached to it.

Loggers are organized in a hierarchy, based on their names; for example, foo is the parent of foo.bar and foo.baz, and foo.baz is the parent of foo.baz.spam. At the apex of that hierarchy is the root logger (i.e., the one whose name is empty). By default loggers propagate emitted records (unless the level of a record is too low for the given logger) to their parents (so that finally those records reach the root logger).

An easy way to prevent your logger from propagating emitted records to the parent is to set the logger's propagate attribute to False (by default it is True).

So try this:

logger.propagate = False

Recommended further reading:

zuo
  • 176
  • 3