Where does Python root logger store a log?
Asked Answered
K

1

34

I'm using the Freebase Python library. It creates a log before executing:

self.log = logging.getLogger("freebase")

Where is this log in the file system? It's not in the executing directory or tmp.

Kreg answered 28/10, 2010 at 11:54 Comment(2)
I noticed a downvote on this question without any explanation. I would appreciate it if the person downvoting would let me know why so I can be sure to be more clear in the future.Kreg
To save the log into a file, we have to use logging.basicConfig(filename = 'blah',...) if we don't use that, the logger just prints stuff to command line, or standard output.Myeshamyhre
F
32

That call does not store anything. It merely creates a logger object which can be bound and configured however you would like.

So if in your Python code, you were to add

logging.basicConfig(level=logging.WARNING)

All warnings and errors would be logged to the standard output (that's what basicConfig does), including the calls that Freebase makes. If you want to log to the filesystem or other target, you'll want to reference the logging module documentation for more information. You may also wish to reference the Logging HOWTO.

Ferdy answered 28/10, 2010 at 12:16 Comment(5)
+1 to what Jason said. You can also do e.g. logging.basicConfig(filename='/path/to/file.log', filemode='w', level=logging.DEBUG) to do simple logging of everything to a file.Flipper
So, after self.log is defined as in @Matt Norris' question, subsequent calls to self.logger.debug will not be saved to disk (assuming no intermediate calls are made)?Dunigan
@dbliss If you configured logging per Vinay's suggestion, subsequent calls to self.log.debug would be written to the file. If you configured logging per my answer instead, calls to self.log.debug would be ignored because they're below the specified level of logging.WARNING.Ferdy
What if you didn't bother to configure it at all? (I'm trying to reverse-engineer a package I didn't write, in which the logger was not configured.) My question is, what's the default location to which self.logger.debug calls are saved?Dunigan
If the code were not to configure any handlers (i.e. nothing calls basicConfig or otherwise configures logging), the default behavior is that messages WARNING and above are emitted to standard error and messages below WARNING (including INFO and DEBUG) are discarded.Ferdy

© 2022 - 2024 — McMap. All rights reserved.