Skip to content

Instantly share code, notes, and snippets.

@matagus
Created October 29, 2010 02:43
Show Gist options
  • Save matagus/652785 to your computer and use it in GitHub Desktop.
Save matagus/652785 to your computer and use it in GitHub Desktop.
logbook + json + filehandler for django
# -*- coding: utf-8 -*-
import os
from logbook import Logger, TimedRotatingFileHandler
from logbook.queues import ThreadedWrapperHandler
from django.conf import settings
from django.core.serializers.json import simplejson
def json_formatter(record, handler):
return simplejson.dumps(record.message)
def get_logger(name="root"):
logger = Logger(name)
file_path = os.path.join(settings.STATS_LOG_FILEPATH, "%s.log" % name)
handler = ThreadedWrapperHandler(
TimedRotatingFileHandler(file_path, date_format="%Y-%m-%d-%Hhs")
)
handler.formatter=json_formatter
logger.handlers.append(handler)
return logger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment