如何使用 Django 1.3 日志字典配置设置 SysLogHandler

发布于 2024-11-11 17:18:57 字数 917 浏览 6 评论 0原文

我没有找到任何有关使用 Django 1.3 字典配置设置 syslog 日志记录的信息。 Django 文档不涵盖 syslog,而 python 文档不太清晰,根本不涵盖字典配置。我已从以下内容开始,但我一直不知道如何配置 SysLogHandler。

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'syslog':{
            'level':'DEBUG',
            'class':'logging.handlers.SysLogHandler',
            'formatter': 'verbose'
        },

    },
    'loggers': {
        'django': {
            'handlers':['syslog'],
            'propagate': True,
            'level':'INFO',
        },
        'myapp': {
            'handlers': ['syslog'],
            'propagate': True,
            'level': 'DEBUG',
        },
    },
}

I'm having no luck finding any information on setting up syslog logging with Django 1.3 dictionary configuration. The Django documents don't cover syslog and the python documentation is less than clear and doesn’t cover dictionary config at all. I've started with the following but I'm stuck on how to configure the SysLogHandler.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'syslog':{
            'level':'DEBUG',
            'class':'logging.handlers.SysLogHandler',
            'formatter': 'verbose'
        },

    },
    'loggers': {
        'django': {
            'handlers':['syslog'],
            'propagate': True,
            'level':'INFO',
        },
        'myapp': {
            'handlers': ['syslog'],
            'propagate': True,
            'level': 'DEBUG',
        },
    },
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伊面 2024-11-18 17:18:57

终于找到了答案,修改原始问题中的配置,使“syslog”具有以下内容:

from logging.handlers import SysLogHandler 
... 
        'syslog':{ 
            'level':'DEBUG', 
            'class': 'logging.handlers.SysLogHandler', 
            'formatter': 'verbose', 
            'facility': SysLogHandler.LOG_LOCAL2, 
        },
...

警告后代:你几乎必须完全按照上面的方式去做,如果你直接指定类等。

更新:我刚刚迁移到 Amazon Linux(和 Django 1.5),并对该环境中“syslog”部分的配置进行了以下更改,请注意“address”参数:

    'syslog':{
        'level':'DEBUG',
        'class': 'logging.handlers.SysLogHandler',
        'formatter': 'verbose',
        'facility': 'local1',
        'address': '/dev/log',
    },

Finally found the answer, modify the configuration in the original question to have the following for 'syslog':

from logging.handlers import SysLogHandler 
... 
        'syslog':{ 
            'level':'DEBUG', 
            'class': 'logging.handlers.SysLogHandler', 
            'formatter': 'verbose', 
            'facility': SysLogHandler.LOG_LOCAL2, 
        },
...

Warning to future generations: you pretty much have to do it exactly like the above, weird errors happen if you specify the class directly etc.

Update: I've just moved to Amazon Linux (and Django 1.5) and used the following change to the configuration for the 'syslog' section in that environment, note the 'address' argument:

    'syslog':{
        'level':'DEBUG',
        'class': 'logging.handlers.SysLogHandler',
        'formatter': 'verbose',
        'facility': 'local1',
        'address': '/dev/log',
    },
我一直都在从未离去 2024-11-18 17:18:57

这对我有用(在默认的 debian 上)。

  1. 我怀疑使用 /dev/log 地址是确保不会尝试使用网络的秘密。
  2. 我认为使用 '' 记录器标签相当于根记录器,因此可以捕获大多数内容

In settings.py:

LOGGING = {
    'version': 1,
    'handlers': {
        'syslog':{
            'address': '/dev/log',
            'class': 'logging.handlers.SysLogHandler'
        }
    },
    'loggers': {
        '': {
            'handlers': ['syslog'],
            'level': 'DEBUG',
        }
    }
}

in the app:

    import logging
    logging.info("freakout info")

提供:

john:/var/log$ sudo tail -1 user.log
Dec 14 17:15:52 john freakout info

This works for me (on default debian).

  1. I suspect using an address of /dev/log is the secret to ensure there is no attempt to use the network.
  2. I think using a logger label of '' equates to the root logger so will catch most stuff

In settings.py:

LOGGING = {
    'version': 1,
    'handlers': {
        'syslog':{
            'address': '/dev/log',
            'class': 'logging.handlers.SysLogHandler'
        }
    },
    'loggers': {
        '': {
            'handlers': ['syslog'],
            'level': 'DEBUG',
        }
    }
}

in the app:

    import logging
    logging.info("freakout info")

provides:

john:/var/log$ sudo tail -1 user.log
Dec 14 17:15:52 john freakout info
给不了的爱 2024-11-18 17:18:57

如果您使用远程系统日志,请务必在地址字段中包含端口,否则您可能会收到 [Errno 9] Bad file detector

日志记录配置将如下所示

LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
    "verbose": {
        "format": "%(asctime)s %(name)-12s %(levelname)-8s %(message)s",
    },
},
"handlers": {
    "console": {
        "level": "INFO",
        "class": "logging.StreamHandler",
        "stream": sys.stdout,
        "formatter": "verbose",
    },
    "syslog": {
        "level": "INFO",
        "class": "logging.handlers.SysLogHandler",
        "formatter": "verbose",
        "facility": "user",
        "address": ["192.168.1.10", 514],
}
},
"loggers": {
    "": {
        "handlers": ["console", "syslog"],
        "level": "INFO",
        "propagate": True,
    },
},

}

If you're using remote syslog, be sure to include the port in the address field, otherwise you could get [Errno 9] Bad file descriptor

The logging config would look something like this

LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
    "verbose": {
        "format": "%(asctime)s %(name)-12s %(levelname)-8s %(message)s",
    },
},
"handlers": {
    "console": {
        "level": "INFO",
        "class": "logging.StreamHandler",
        "stream": sys.stdout,
        "formatter": "verbose",
    },
    "syslog": {
        "level": "INFO",
        "class": "logging.handlers.SysLogHandler",
        "formatter": "verbose",
        "facility": "user",
        "address": ["192.168.1.10", 514],
}
},
"loggers": {
    "": {
        "handlers": ["console", "syslog"],
        "level": "INFO",
        "propagate": True,
    },
},

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文