How to log with python (when No handlers could be found for logger "__main__" appears)

problem

As a sample code that is rolling around

python


import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.info("hoge")

I see this kind of thing, but it doesn't work even if I copy it as it is.

No handlers could be found for logger "__main__"

This error message appears on the second line. Since __name__ is the file name specified first, it becomes " __ main__ ".

python


import logging
logging.info("ho")
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.info("hoge")

For some reason, it works if you spit something out of the logging. module even once.

INFO:__main__:hoge

solution

logger cannot go without giving a config.

python


import logging
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.info("hoge")

When logging.info ("") is set, the operation equivalent tologging.basicConfig ()is performed internally.

Recommended Posts

How to log with python (when No handlers could be found for logger "__main__" appears)
When I tried to use pip with python, I was told that XML_SetHashSalt could not be found.
What to do if No Python documentation found for ... appears in pydoc
[Python] What to do when No module named'pyproj.datadir' appears when Exe is done with PyInstaller
How to deal with "Type Error: No matching signature found" error when using pandas fillna
How to check when python-memcached server could not be connected
How to log in to AtCoder with Python and submit automatically
How to not escape Japanese when dealing with json in python
How to change the log level of Azure SDK for Python
Be careful of LANG for UnicodeEncodeError when printing Japanese with Python 3
How to deal with errors when installing Python and pip with choco
How to automatically install Chrome Driver for Chrome version with Python + Selenium + Chrome
Python: How to use async with
How to get started with Python
How to use FTP with Python
How to calculate date with python
How to write what to do when an application is first displayed in Qt for Python with Designer
How to resolve "No kernel of grammar Python found" error in Atom
How to deal with OAuth2 error when using Google APIs from Python
How to deal with SSL error when connecting to S3 with boto of Python
Error due to UnicodeDecodeError when reading CSV file with Python [For beginners]
How to access data with object ['key'] for your own Python class
Memo to ask for KPI with python
Output Python log to console with GAE
How to work with BigQuery in Python
How to do portmanteau test with python
[Python] Organizing how to use for statements
How to display python Japanese with lolipop
How to use "deque" for Python data
How to enter Japanese with Python curses
[Python] How to deal with module errors
Remedy for No key could be detected.
How to install python3 with docker centos
How to use Service Account OAuth and API with Google API Client for python
When you can't uninstall the python library. Can't uninstall'hogehoge'. No files were found to uninstall.
What to do when [Errno 2] No such file or directory appears in Python
[Introduction to Python] How to get the index of data with a for statement
How to upload with Heroku, Flask, Python, Git (4)
How to start Python (Flask) when EC2 starts
How to read a CSV file with Python 2/3
How to enjoy programming with Minecraft (Ruby, Python)
How to deal with errors when hitting pip ②
[REAPER] How to play with Reascript in Python
How to do multi-core parallel processing with python
How to delete log with Docker, not to collect log
Strategy on how to monetize with Python Java
[Python] How to draw multiple graphs with Matplotlib
[Python] How to read excel file with pandas
Use logger with Python for the time being
How to crop an image with Python + OpenCV
How to install Python for pharmaceutical company researchers
How to deal with module'tensorflow' has no attribute'〇〇'
How to deal with SessionNotCreatedException when using Selenium
Log in to Yahoo Business with Selenium Python
How to specify attributes with Mock of python
Investigation when import cannot be done with python
How to use tkinter with python in pyenv
[Python] How to handle Japanese characters with openCV
How to make Python faster for beginners [numpy]
Spider not found appears when crawling with Scrapy
Materials to read when getting started with Python