logger configuration to log to file and print to stdout
Just get a handle to the root logger and add the StreamHandler
. The StreamHandler
writes to stderr. Not sure if you really need stdout over stderr, but this is what I use when I setup the Python logger and I also add the FileHandler
as well. Then all my logs go to both places (which is what it sounds like you want).
import logging
logging.getLogger().addHandler(logging.StreamHandler())
If you want to output to stdout
instead of stderr
, you just need to specify it to the StreamHandler
constructor.
import sys
# ...
logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
You could also add a Formatter
to it so all your log lines have a common header.
ie:
import logging
logFormatter = logging.Formatter("%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s")
rootLogger = logging.getLogger()
fileHandler = logging.FileHandler("{0}/{1}.log".format(logPath, fileName))
fileHandler.setFormatter(logFormatter)
rootLogger.addHandler(fileHandler)
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logFormatter)
rootLogger.addHandler(consoleHandler)
Prints to the format of:
2012-12-05 16:58:26,618 [MainThread ] [INFO ] my message
Making Python loggers output all messages to stdout in addition to log file
All logging output is handled by the handlers; just add a logging.StreamHandler()
to the root logger.
Here's an example configuring a stream handler (using stdout
instead of the default stderr
) and adding it to the root logger:
import logging
import sys
root = logging.getLogger()
root.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
root.addHandler(handler)
Python logging to stdout and log file
You just need to add another handler, like a logging.FileHandler
fh = logging.FileHandler(r'/path/to/log.txt')
logger.addHandler(fh)
Simplest way to set up python logging to stdout
If you just need to print the messages to the stdout, then logging.basicConfig
is a handy shortcut for the configuration you listed. It will create a StreamHandler
, attach the default Formatter
to it and attach the handler to the root logger.
import logging
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
logging.getLogger().info('hi')
Check out the docs for more configuration possibilities; for example,
logging.basicConfig(filename='some.log', level=logging.DEBUG)
will configure writing to file some.log
instead of stdout.
Note that logging.basicConfig
won't do a thing if the logger is already configured (meaning that there are handlers attached to the root logger already). So this code:
import logging
logging.getLogger().addHandler(logging.FileHandler(filename='some.log'))
logging.basicConfig(level=logging.DEBUG)
will not configure logging to stdout anymore; you will have to do it yourself.
How to log outputs and errors to file without knowing where errors will occur
Found the answer myself: just type this in the terminal and make sure that you have a log.text file in the right directory. No changes in the script needed.
python script.py &> ./log.txt
Inspired from this question: Redirect stdout and stderr to same file using Python
Python Logger : StreamHandler not controlling my terminal stdout?
It seems it is related to the fact that I have multiple modules - each module can have a logger to console, but the main script must remain in control and have its own logger set (which I did not have).
This page solved my problem: https://docs.python.org/3/howto/logging-cookbook.html#logging-to-multiple-destinations
Related Topics
Variable Assignment and Modification (In Python)
Python Image Library Fails with Message "Decoder Jpeg Not Available" - Pil
Python How to Parse CSS File as Key Value
Equivalent of a Python Dict in R
Equivalent to Python's Findall() Method in Ruby
Create Dynamic Urls in Flask with Url_For()
How to Return a Value from _Init_ in Python
Stream Large Binary Files with Urllib2 to File
Python != Operation VS "Is Not"
How to Convert a Currency String to a Floating Point Number in Python
What Are Some Good Python Orm Solutions
Display Loading Symbol While Waiting for a Result with Plot.Ly Dash
Dealing with the Class Imbalance in Binary Classification
Find in Files Using Ruby or Python
How to Install Python Windows Packages into Virtualenvs
What's the Best Way to Return Multiple Values from a Function