Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: What are differences between `logger.info` and `logging.info`?

Tags:

python

logging

I wonder what is the differences between two versions below? And how to use it effectively?

Version 1:

import logging
logging.info("Hello world!")

Version 2:

import logging
logger = logging.getLogger(__name__)
logger.info("Hello world!")
like image 526
Luan Pham Avatar asked Nov 04 '25 16:11

Luan Pham


1 Answers

I run my code:

class TestBedLog():
    async def test(self):

        import logging

        logging.info("Log from logging")

        logger = logging.getLogger(__name__)
        logger.info("Log from logger")

And the result is:

root: INFO: Log from logging
src.myserver.test.test_apiv2.test_bedlog: INFO: Log from logger

As you can see, logging is from the root. And logger you will see the file where log the information

like image 89
Albert Nguyen Avatar answered Nov 07 '25 11:11

Albert Nguyen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!