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: