Login

Log to syslog

Author:
fylb
Posted:
February 9, 2010
Language:
Python
Version:
1.1
Score:
2 (after 2 ratings)

This will use the syslog system to log your logs. This is usefull when you use WSGI and want to have a per day logging file (TimedRotatingFileHandler is not process safe, and you may lose data when using it with WSGI). Under a debian system, you'll have to modify /etc/rsyslog.conf and add:

local0.*                        -/var/log/django/django.log

local1.*                        -/var/log/django/payment.log
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import logging
from logging.handlers import SysLogHandler
from django.conf import settings
from sys import stdout

LOG_AREA_PAYMENT = 'payment'

payment_logger = logging.getLogger(LOG_AREA_PAYMENT)

def init_logging():
    global manage_logger, payment_logger
    """ Initialize the different loggers used in the application """
    # create formatter
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    # default log
    stdoutHandler = logging.StreamHandler(stdout)
    stdoutHandler.setLevel(logging.DEBUG)
    stdoutHandler.setFormatter(formatter)
    logging.getLogger().addHandler(stdoutHandler)
    default_log_handler = SysLogHandler("/dev/log", "local0")
    default_log_handler.setLevel(logging.DEBUG)
    default_log_handler.setFormatter(formatter)
    logging.getLogger().addHandler(default_log_handler)
    
    # payment log
    payment_logger = logging.getLogger(LOG_AREA_PAYMENT)
    payment_logger.setLevel(logging.DEBUG)
    payment_handler = SysLogHandler("/dev/log", "local1")
    payment_handler.setFormatter(formatter)
    payment_logger.addHandler(payment_handler)

logInitDone = False

if not logInitDone:
    logInitDone = True
    init_logging()

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.