Two approaches to Logging when Unit Testing with Python
up vote
-1
down vote
favorite
What the relative merits of these two approaches to Logging when Unit Testing with Python. Is one approach superior or does each approach suit specific circumstances. How might each approach be improved.
#!/usr/bin/env python
import logging
import unittest
class LoggingTest(unittest.TestCase):
logging.basicConfig(level=logging.INFO)
def setUp(self):
pass
def test_logger(self):
logging.info("logging.info test_logger")
pass
Getting a logger by name
#!/usr/bin/env python
import logging
import unittest
class GetLoggerTest(unittest.TestCase):
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def setUp(self):
pass
def test_logger(self):
self.logger.info("self.logger.info test_logger")
pass
python unit-testing logging
add a comment |
up vote
-1
down vote
favorite
What the relative merits of these two approaches to Logging when Unit Testing with Python. Is one approach superior or does each approach suit specific circumstances. How might each approach be improved.
#!/usr/bin/env python
import logging
import unittest
class LoggingTest(unittest.TestCase):
logging.basicConfig(level=logging.INFO)
def setUp(self):
pass
def test_logger(self):
logging.info("logging.info test_logger")
pass
Getting a logger by name
#!/usr/bin/env python
import logging
import unittest
class GetLoggerTest(unittest.TestCase):
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def setUp(self):
pass
def test_logger(self):
self.logger.info("self.logger.info test_logger")
pass
python unit-testing logging
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
What the relative merits of these two approaches to Logging when Unit Testing with Python. Is one approach superior or does each approach suit specific circumstances. How might each approach be improved.
#!/usr/bin/env python
import logging
import unittest
class LoggingTest(unittest.TestCase):
logging.basicConfig(level=logging.INFO)
def setUp(self):
pass
def test_logger(self):
logging.info("logging.info test_logger")
pass
Getting a logger by name
#!/usr/bin/env python
import logging
import unittest
class GetLoggerTest(unittest.TestCase):
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def setUp(self):
pass
def test_logger(self):
self.logger.info("self.logger.info test_logger")
pass
python unit-testing logging
What the relative merits of these two approaches to Logging when Unit Testing with Python. Is one approach superior or does each approach suit specific circumstances. How might each approach be improved.
#!/usr/bin/env python
import logging
import unittest
class LoggingTest(unittest.TestCase):
logging.basicConfig(level=logging.INFO)
def setUp(self):
pass
def test_logger(self):
logging.info("logging.info test_logger")
pass
Getting a logger by name
#!/usr/bin/env python
import logging
import unittest
class GetLoggerTest(unittest.TestCase):
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def setUp(self):
pass
def test_logger(self):
self.logger.info("self.logger.info test_logger")
pass
python unit-testing logging
python unit-testing logging
asked 9 hours ago
Martin Spamer
24329
24329
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
According to the logging docs:
The logger name hierarchy is analogous to the Python package hierarchy, and identical to it if you organise your loggers on a per-module basis using the recommended construction logging.getLogger(name). That’s because in a module, name is the module’s name in the Python package namespace.
Based on my experience, I would say that you should always use logging.getLogger(name) for bigger projects, as it'll make logging modularization easier.
As your question is broad, I recommend you to read the Logging Cookbook, in this link, as it holds many important use cases in which you could optimize the way you use logging.
It's also possible to mix both cases in order to add a timestamp, in example, which is an improved way of using this module:
#!/usr/bin/env python
import logging
import unittest
class GetLoggerTest(unittest.TestCase):
logger = logging.getLogger(__name__)
logging.basicConfig(format = '%(asctime)s %(module)s %(levelname)s: %(message)s',
datefmt = '%m/%d/%Y %I:%M:%S %p', level = logging.INFO)
def setUp(self):
pass
def test_logger(self):
self.logger.info("self.logger.info test_logger")
pass
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
According to the logging docs:
The logger name hierarchy is analogous to the Python package hierarchy, and identical to it if you organise your loggers on a per-module basis using the recommended construction logging.getLogger(name). That’s because in a module, name is the module’s name in the Python package namespace.
Based on my experience, I would say that you should always use logging.getLogger(name) for bigger projects, as it'll make logging modularization easier.
As your question is broad, I recommend you to read the Logging Cookbook, in this link, as it holds many important use cases in which you could optimize the way you use logging.
It's also possible to mix both cases in order to add a timestamp, in example, which is an improved way of using this module:
#!/usr/bin/env python
import logging
import unittest
class GetLoggerTest(unittest.TestCase):
logger = logging.getLogger(__name__)
logging.basicConfig(format = '%(asctime)s %(module)s %(levelname)s: %(message)s',
datefmt = '%m/%d/%Y %I:%M:%S %p', level = logging.INFO)
def setUp(self):
pass
def test_logger(self):
self.logger.info("self.logger.info test_logger")
pass
add a comment |
up vote
0
down vote
According to the logging docs:
The logger name hierarchy is analogous to the Python package hierarchy, and identical to it if you organise your loggers on a per-module basis using the recommended construction logging.getLogger(name). That’s because in a module, name is the module’s name in the Python package namespace.
Based on my experience, I would say that you should always use logging.getLogger(name) for bigger projects, as it'll make logging modularization easier.
As your question is broad, I recommend you to read the Logging Cookbook, in this link, as it holds many important use cases in which you could optimize the way you use logging.
It's also possible to mix both cases in order to add a timestamp, in example, which is an improved way of using this module:
#!/usr/bin/env python
import logging
import unittest
class GetLoggerTest(unittest.TestCase):
logger = logging.getLogger(__name__)
logging.basicConfig(format = '%(asctime)s %(module)s %(levelname)s: %(message)s',
datefmt = '%m/%d/%Y %I:%M:%S %p', level = logging.INFO)
def setUp(self):
pass
def test_logger(self):
self.logger.info("self.logger.info test_logger")
pass
add a comment |
up vote
0
down vote
up vote
0
down vote
According to the logging docs:
The logger name hierarchy is analogous to the Python package hierarchy, and identical to it if you organise your loggers on a per-module basis using the recommended construction logging.getLogger(name). That’s because in a module, name is the module’s name in the Python package namespace.
Based on my experience, I would say that you should always use logging.getLogger(name) for bigger projects, as it'll make logging modularization easier.
As your question is broad, I recommend you to read the Logging Cookbook, in this link, as it holds many important use cases in which you could optimize the way you use logging.
It's also possible to mix both cases in order to add a timestamp, in example, which is an improved way of using this module:
#!/usr/bin/env python
import logging
import unittest
class GetLoggerTest(unittest.TestCase):
logger = logging.getLogger(__name__)
logging.basicConfig(format = '%(asctime)s %(module)s %(levelname)s: %(message)s',
datefmt = '%m/%d/%Y %I:%M:%S %p', level = logging.INFO)
def setUp(self):
pass
def test_logger(self):
self.logger.info("self.logger.info test_logger")
pass
According to the logging docs:
The logger name hierarchy is analogous to the Python package hierarchy, and identical to it if you organise your loggers on a per-module basis using the recommended construction logging.getLogger(name). That’s because in a module, name is the module’s name in the Python package namespace.
Based on my experience, I would say that you should always use logging.getLogger(name) for bigger projects, as it'll make logging modularization easier.
As your question is broad, I recommend you to read the Logging Cookbook, in this link, as it holds many important use cases in which you could optimize the way you use logging.
It's also possible to mix both cases in order to add a timestamp, in example, which is an improved way of using this module:
#!/usr/bin/env python
import logging
import unittest
class GetLoggerTest(unittest.TestCase):
logger = logging.getLogger(__name__)
logging.basicConfig(format = '%(asctime)s %(module)s %(levelname)s: %(message)s',
datefmt = '%m/%d/%Y %I:%M:%S %p', level = logging.INFO)
def setUp(self):
pass
def test_logger(self):
self.logger.info("self.logger.info test_logger")
pass
answered 5 hours ago
Neves4
85
85
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208251%2ftwo-approaches-to-logging-when-unit-testing-with-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown