On 23/06/10 16:48, thusjanthan wrote:
Hi,
I am creating a new django framework and figured django would come
with its own logging feature. I found this one that Fraser wrote but
is no longer in development (http://code.google.com/p/django-logging/
wiki/Overview)
That's wasn't really for logging in the operational server system logs
sense, it was for showing log messages arising in the request-response
cycle in the html returned to the browser (which IS very handy during
development/debug, but not something you'd do on a production server...).
django-debug-toolbar has a superset of its functionality.
Can anyone suggest me a django logging project to log
debug/error messages at server level and as a bonus feature perhaps an
email to admin if a critical error happens.
Python itself ships with a logging infrastructure (quite the baroque
one), module "logging". You can just use that in conjunction with django.
http://docs.python.org/release/2.6/library/logging.html
regarding email: Django does fire off certain exception emails when
disaster strikes, python logging has SMTPHandler, and there's also the
"logwatch" tool once you have stuff going to logs.
###
So, most of our django app's python files do something like the
following near the top:
# say for the models.py in a django app "accounts"
import logging
_log = logging.getLogger('accounts.models')
# and then use
_log.error("ohno")
_log.warning("grr")
_log.info("hey")
_log.debug("soturnsout")
We use the leading underscore on the module-level _log so that A doesn't
pick up B's logger if A does a "from B import *" !
###
We then init python's logging in a vaguely django-project-useful fashion
in a function called from our settings.py or somewhere similarly early
(you may need to take steps to make sure to only register the handlers
once per process, say a global "configured" var)
e.g. (not precisely the code we use, untested):
# prefix is just a per-project prefix we set, that way two
# instances of the same app in two different projects are
# distinguishable in combined logging.
# root logger for argument's sake
# you might want something else, see logging docs
rl = logging.getLogger('')
# Stuff you send to stderr with mod_wsgi does end up in the apache log,
# though maybe it's not the best option.
streamformatter = logging.Formatter("django[%(process)d]: %(levelname)s:
[" + prefix + "]%(name)s: %(message)s")
streamhandler = logging.StreamHandler() # defaults to stderr
streamhandler.setLevel(logging.WARNING)
streamhandler.setFormatter(streamformatter)
rl.addHandler(streamhandler)
# and/or you could send to syslog
slformatter = logging.Formatter("django[%(process)d]: %(levelname)s: ["
+ prefix + "]%(name)s: %(message)s")
sysloghandler = logging.handlers.SysLogHandler(
address='/dev/log',
facility=logging.handlers.SysLogHandler.LOG_DAEMON)
sysloghandler.setLevel(logging.WARNING)
sysloghandler.setFormatter(slformatter)
rl.addHandler(sysloghandler)
# And/or to a file
# N.B. python 2.5 lacks WatchedFileHandler, but it's an easy backport
# from 2.6 sources.
# base some base file path+name
fformatter = logging.Formatter("[%(asctime)s] django[%(process)d]:
%(levelname)s: [" + prefix + "]%(name)s: %(message)s")
h = WatchedFileHandler(base + ".log", encoding='utf-8')
h.setLevel(logging.INFO)
h.setFormatter(fformatter)
rl.addHandler(h)
e = WatchedFileHandler(base + ".err", encoding='utf-8')
e.setLevel(logging.ERROR)
e.setFormatter(fformatter)
rl.addHandler(e)
if settings.DEBUG:
debugformatter = logging.Formatter("[%(asctime)s]
django[%(process)d]: %(levelname)s: [" + prefix + "]%(name)s:
%(pathname)s(%(lineno)d): %(message)s")
d = WatchedFileHandler(base + ".dbg", encoding='utf-8')
d.setLevel(logging.DEBUG)
d.setFormatter(debugformatter)
rl.addHandler(d)
# See logging docs for more.
--
You received this message because you are subscribed to the Google Groups "Django
users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.