How to disable logging on the standard error stream?
I found a solution for this:
logger = logging.getLogger('my-logger')
logger.propagate = False
# now if you use logger it will not log to console.
This will prevent logging from being send to the upper logger that includes the console logging.
Python logging: disable output to stdout
You have different ways to do so:
-set logger.propagate to false.
you can create tour own stream, like this:
https://docs.python.org/2/library/io.html#io.StringIO
And give it to your logger.
logging.StreamHandler(stream=None)
Why is Spring INFO logging to Standard Error?
You can hide it using log4j. In your log4j.xml, set a logger for spring to warn (or error).
<logger name="org.springframework">
<level value="warn"/>
</logger>
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
Related Topics
How Include Static Files to Setuptools - Python Package
Python: Can Executable Zip Files Include Data Files
Importerror: No Module Named _Ssl
Python: Get the Print Output in an Exec Statement
How to Make a Multidimension Numpy Array with a Varying Row Size
How to Load a Module from Code in a String
Execute a File with Arguments in Python Shell
"Line Contains Null Byte" in CSV Reader (Python)
Python Webbrowser.Open() to Open Chrome Browser
How Would I Stop a While Loop After N Amount of Time
Python Argparse Mutual Exclusive Group
Remove Quotes from String in Python
How to Change Index of a for Loop
Pandas Make New Column from String Slice of Another Column
How to Do/Workaround a Conditional Join in Python Pandas