Login

Log to syslog

Author:
fylb
Posted:
February 9, 2010
Language:
Python
Version:
1.1
Tags:
logging syslog
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. Extended logging module by onlinehero 5 years, 10 months ago
  2. Django app WSGI by ofalk 5 years, 11 months ago
  3. showing environment variables in the django admin by tonemcd 5 years, 4 months ago
  4. MODPYTHON Sample Site Logging by adroffner 6 years, 9 months ago
  5. Middleware that fixes URLs generation for admin and {% url %} for SCGI and WSGI by dottedmag 7 years, 10 months ago

Comments

Please login first before commenting.