From f025f5e477b943c46806f5d37bbd697326055597 Mon Sep 17 00:00:00 2001 From: Mikhail Korobov Date: Thu, 30 Dec 2010 23:52:48 +0500 Subject: [PATCH] LOGGING option is renamed to JOGGING for django 1.3 compatibility. LOGGING is still supported for django < 1.3. If there wasn't LOGGING option defined in settings.py then empty JOGGING={} options should be defined there in order to get things work with django 1.3. --- README.rst | 24 ++++++------- jogging/models.py | 11 +++--- jogging/tests/tests.py | 82 +++++++++++++++++++++--------------------- setup.py | 0 4 files changed, 60 insertions(+), 57 deletions(-) mode change 100644 => 100755 setup.py diff --git a/README.rst b/README.rst index 2612a18..b803785 100644 --- a/README.rst +++ b/README.rst @@ -2,7 +2,7 @@ Jogging is a thin wrapper around Python's logging functionality to make Django l With Jogging, you can control the destination, format, and verbosity of logs with fine granularity. Configuring module-level logging is just as easy as configuring logging for specific functions. -To use it, you add a few configurations to settings.py, import Jogging's log functions instead of Python's, and then log away like you normally do. +To use it, you add a few configurations to settings.py, import Jogging's log functions instead of Python's, and then log away like you normally do. Python's logging module does all the heavy lifting. As a result, you can use Jogging to configure logging for code that already exists. And great care has been taken to make sure logging's power isn't hidden away behind abstractions. @@ -50,13 +50,13 @@ Intermediate from logging import StreamHandler import logging - LOGGING = { + JOGGING = { # all logs from myapp1 go to the database 'myapp1': { 'handler': DatabaseHandler(), # an initialized handler object 'level': logging.DEBUG, }, - + # ...except for this one view which will log CRITICAL to stderr 'myapp1.views.super_important_view': { 'handler': StreamHandler(), @@ -76,24 +76,24 @@ Advanced from logging import StreamHandler, FileHandler import logging - LOGGING = { + JOGGING = { # all logs from myapp1 go to the database 'myapp1': { 'handler': DatabaseHandler(), 'level': logging.DEBUG, }, - + # this time, we'll have super_important_view log CRITICAL to stderr again, but # we'll also have it log everything to the database 'myapp1.views.super_important_view': { 'handlers': [ - { 'handler': StreamHandler(), 'level': logging.CRITICAL, + { 'handler': StreamHandler(), 'level': logging.CRITICAL, 'format': "%(asctime)-15s %(source): %(message)s %(foo)s" }, { 'handler': DatabaseHandler(), 'level': logging.DEBUG }, ] }, - - # this is the name of a logger that a third party app already logs to. + + # this is the name of a logger that a third party app already logs to. # you can configure it just like the others, without breaking anything. 'simple_example': { 'handler': StreamHandler(), @@ -151,17 +151,17 @@ Implementation ====================== Much inspiration was taken from `Django's logging proposal `_. -Jogging requires a dictionary, ``settings.LOGGING``, that defines the loggers you want to control through Jogging (by name). Here is how Jogging works: +Jogging requires a dictionary, ``settings.JOGGING``, that defines the loggers you want to control through Jogging (by name). Here is how Jogging works: -1. All loggers are created on server startup from ``settings.LOGGING`` (the init code is in models.py, for lack of a better place). Handlers are added to the loggers as defined, and levels are set. -2. When your app calls Jogging's log functions, the calling function is matched against the logger names in ``settings.LOGGING`` and the most specific logger is chosen. For example, say ``myproj.myapp.views.func()`` is the caller; it will match loggers named ``myproj.myapp.views.func``, ``myproj.myapp.views``, ``myproj.myapp``, and ``myproj``. The first (most specific) one that matches will be chosen. +1. All loggers are created on server startup from ``settings.JOGGING`` (the init code is in models.py, for lack of a better place). Handlers are added to the loggers as defined, and levels are set. +2. When your app calls Jogging's log functions, the calling function is matched against the logger names in ``settings.JOGGING`` and the most specific logger is chosen. For example, say ``myproj.myapp.views.func()`` is the caller; it will match loggers named ``myproj.myapp.views.func``, ``myproj.myapp.views``, ``myproj.myapp``, and ``myproj``. The first (most specific) one that matches will be chosen. 3. ``log()`` is called on the chosen logger, and Python's logging module takes over from here. =========== Resources =========== -List of handlers in Python's logging module: +List of handlers in Python's logging module: http://docs.python.org/library/logging.html#handler-objects Format specifiers for Python's logging module: diff --git a/jogging/models.py b/jogging/models.py index 7cabeac..2434730 100644 --- a/jogging/models.py +++ b/jogging/models.py @@ -20,7 +20,7 @@ def abbrev_msg(self, maxlen=500): return u'%s ...' % self.msg[:maxlen] return self.msg abbrev_msg.short_description = u'abbreviated msg' - + class Meta: get_latest_by = 'datetime' @@ -42,8 +42,11 @@ def add_handlers(logger, handlers): else: logger.addHandler(handler) - if hasattr(settings, 'LOGGING') and settings.LOGGING: - for module, properties in settings.LOGGING.items(): + + LOGGING = getattr(settings, 'JOGGING', getattr(settings, 'LOGGING', None)) + + if LOGGING: + for module, properties in LOGGING.items(): logger = py_logging.getLogger(module) if 'level' in properties: @@ -60,7 +63,7 @@ def add_handlers(logger, handlers): "A logger in settings.LOGGING doesn't have its log level set. " + "Either set a level on that logger, or set GLOBAL_LOG_LEVEL.") - handlers = [] + handlers = [] if 'handler' in properties: handlers = [properties['handler']] elif 'handlers' in properties: diff --git a/jogging/tests/tests.py b/jogging/tests/tests.py index 6ff75f6..cd60831 100644 --- a/jogging/tests/tests.py +++ b/jogging/tests/tests.py @@ -8,14 +8,14 @@ from jogging.models import Log, jogging_init class DatabaseHandlerTestCase(DjangoTestCase): - + def setUp(self): from jogging.handlers import DatabaseHandler, MockHandler import logging - - self.LOGGING = getattr(settings, 'LOGGING', None) - - settings.LOGGING = { + + self.JOGGING = getattr(settings, 'JOGGING', None) + + settings.JOGGING = { 'database_test': { 'handler': DatabaseHandler(), 'level': logging.DEBUG, @@ -27,9 +27,9 @@ def setUp(self): ], }, } - + jogging_init() - + def tearDown(self): import logging @@ -37,15 +37,15 @@ def tearDown(self): loggers = [logging.getLogger(""), logging.getLogger("database_test"), logging.getLogger("multi_test")] for logger in loggers: logger.handlers = [] - + # delete all log entries in the database for l in Log.objects.all(): l.delete() - - if self.LOGGING: - settings.LOGGING = self.LOGGING + + if self.JOGGING: + settings.JOGGING = self.JOGGING jogging_init() - + def test_basic(self): logger = logging.getLogger("database_test") logger.info("My Logging Test") @@ -54,18 +54,18 @@ def test_basic(self): self.assertEquals(log_obj.source, "database_test") self.assertEquals(log_obj.msg, "My Logging Test") self.assertTrue(log_obj.host) - + def test_multi(self): logger = logging.getLogger("multi_test") logger.info("My Logging Test") - + log_obj = Log.objects.latest() self.assertEquals(log_obj.level, "INFO") self.assertEquals(log_obj.source, "multi_test") self.assertEquals(log_obj.msg, "My Logging Test") self.assertTrue(log_obj.host) - - log_obj = settings.LOGGING["multi_test"]["handlers"][1]["handler"].msgs[0] + + log_obj = settings.JOGGING["multi_test"]["handlers"][1]["handler"].msgs[0] self.assertEquals(log_obj.levelname, "INFO") self.assertEquals(log_obj.name, "multi_test") self.assertEquals(log_obj.msg, "My Logging Test") @@ -75,10 +75,10 @@ class DictHandlerTestCase(DjangoTestCase): def setUp(self): from jogging.handlers import MockHandler import logging - - self.LOGGING = getattr(settings, 'LOGGING', None) - settings.LOGGING = { + self.JOGGING = getattr(settings, 'JOGGING', None) + + settings.JOGGING = { 'dict_handler_test': { 'handlers': [ { 'handler': MockHandler(), 'level': logging.ERROR }, @@ -86,9 +86,9 @@ def setUp(self): ], }, } - + jogging_init() - + def tearDown(self): import logging @@ -96,19 +96,19 @@ def tearDown(self): loggers = [logging.getLogger(""), logging.getLogger("database_test"), logging.getLogger("multi_test")] for logger in loggers: logger.handlers = [] - + # delete all log entries in the database for l in Log.objects.all(): l.delete() - - if self.LOGGING: - settings.LOGGING = self.LOGGING + + if self.JOGGING: + settings.JOGGING = self.JOGGING jogging_init() - + def test_basic(self): logger = logging.getLogger("dict_handler_test") - error_handler = settings.LOGGING["dict_handler_test"]["handlers"][0]["handler"] - info_handler = settings.LOGGING["dict_handler_test"]["handlers"][1]["handler"] + error_handler = settings.JOGGING["dict_handler_test"]["handlers"][0]["handler"] + info_handler = settings.JOGGING["dict_handler_test"]["handlers"][1]["handler"] logger.info("My Logging Test") @@ -122,25 +122,25 @@ def test_basic(self): class GlobalExceptionTestCase(DjangoTestCase): urls = 'jogging.tests.urls' - + def setUp(self): from jogging.handlers import DatabaseHandler, MockHandler import logging - - self.LOGGING = getattr(settings, 'LOGGING', None) + + self.JOGGING = getattr(settings, 'JOGGING', None) self.GLOBAL_LOG_HANDLERS = getattr(settings, 'GLOBAL_LOG_HANDLERS', None) self.GLOBAL_LOG_LEVEL = getattr(settings, 'GLOBAL_LOG_LEVEL', None) - + loggers = [logging.getLogger("")] for logger in loggers: logger.handlers = [] - - settings.LOGGING = {} + + settings.JOGGING = {} settings.GLOBAL_LOG_HANDLERS = [MockHandler()] settings.GLOBAL_LOG_LEVEL = logging.DEBUG - + jogging_init() - + def tearDown(self): import logging @@ -148,19 +148,19 @@ def tearDown(self): loggers = [logging.getLogger("")] for logger in loggers: logger.handlers = [] - + # delete all log entries in the database for l in Log.objects.all(): l.delete() - - if self.LOGGING: - settings.LOGGING = self.LOGGING + + if self.JOGGING: + settings.JOGGING = self.JOGGING if self.GLOBAL_LOG_HANDLERS: settings.GLOBAL_LOG_HANDLERS = self.GLOBAL_LOG_HANDLERS if self.GLOBAL_LOG_LEVEL: settings.GLOBAL_LOG_LEVEL = self.GLOBAL_LOG_LEVEL jogging_init() - + def test_exception(self): from views import TestException try: diff --git a/setup.py b/setup.py old mode 100644 new mode 100755