Prevent cherrypy from logging access

时间:2015-07-28 23:53:43

标签: python logging cherrypy

I'm trying to set up a logger to only catch my ERROR level messages, but my logger always seems to write Option Explicit Public Sub Test(Item As Outlook.MailItem) Dim xlApp As Excel.Application Dim xlWB As Excel.Workbook Dim xlSheet As Excel.Worksheet Dim vText As Variant Dim sText As String Dim vItem As Variant Dim i As Long Dim rCount As Long Dim XStarted As Boolean Dim FileName As String Dim FilePath As String '// SaveAs CSV File Path Dim sPath As String '// .CSV File Path '// the path of the workbook sPath = "C:\temp\temp.csv" On Error Resume Next Set xlApp = GetObject(, "Excel.Application") If Err <> 0 Then Application.StatusBar = "Please wait while Excel source is opened ... " Set xlApp = CreateObject("Excel.Application") XStarted = True End If ' On Error GoTo 0 '// Open the workbook to input the data Set xlWB = xlApp.Workbooks.Open(sPath) Set xlSheet = xlWB.Sheets("Report") '// Process received Mail sText = Item.Body vText = Split(sText, Chr(13)) ' Chr(13)) carriage return '// Find the next empty line of the worksheet rCount = xlSheet.Range("B" & xlSheet.Rows.Count).End(xlUp).Row rCount = rCount + 1 '// Check each line of text in the message body For i = UBound(vText) To 0 Step -1 '// Customer Name If InStr(1, vText(i), "Customer") > 0 Then vItem = Split(vText(i), Chr(9)) ' Chr(9) horizontal tab xlSheet.Range("A" & rCount) = Trim(vItem(1)) End If '// Ref Number If InStr(1, vText(i), "Order #") > 0 Then vItem = Split(vText(i), Chr(9)) xlSheet.Range("B" & rCount) = Trim(vItem(1)) End If '// Service Level If InStr(1, vText(i), "Service Level") > 0 Then vItem = Split(vText(i), Chr(9)) xlSheet.Range("J" & rCount) = Trim(vItem(1)) End If Next i FilePath = Environ("USERPROFILE") & "\Documents\Temp\" FileName = Sheets(1).Range("B2").Value xlWB.SaveAs FileName:=FilePath & FileName '// Close & SaveChanges xlWB.Close SaveChanges:=True If XStarted Then xlApp.Quit End If Set xlApp = Nothing Set xlWB = Nothing Set xlSheet = Nothing Set Item = Nothing End Sub messages to my log file, which I dont want. I tried setting the INFO:cherrypy.access global, and using the standard python logging module log.error_file, but even though I specify the threshold to be ERROR level messages, I still get those INFO level messages written to my log file. Is there any way to prevent this?

3 个答案:

答案 0 :(得分:0)

似乎INFO的{​​{1}}级别日志即将发布。根据{{​​3}} -

  

您应该在全局级别或每个应用程序中设置它们(请参阅下一个),但通常不能同时设置这两个。

     

log.screen :将此项设置为True可将“错误”和“访问”消息打印到stdout。    log.access_file :将此设置为您希望“访问”写入消息的绝对文件名。    log.error_file :将此设置为您希望写入“错误”消息的绝对文件名。

您还应该尝试设置cherrypy.access(类似于log.access_file)。或者您可以为其添加处理程序,例如 -

log.error_file

答案 1 :(得分:0)

我可以通过设置我自己来禁用访问消息:

logging.basicConfig(filename='error.log', filemode='w', level=logging.ERROR)

然后设置:

cherrypy.log.access_log.propagate = False

答案 2 :(得分:0)

我也有这个问题,我想出来了。所以在这里发布并希望它可以帮助人们有类似的问题。

CherryPy版本14.2.0

要禁用access日志记录,您只需将空字符串保留为'log.access_file': ''

即可
if __name__ == '__main__':
    cherrypy.config.update(
        {
            'server.socket_host': settings.HOST,
            'server.socket_port': settings.PORT,
            'log.screen': True,
            'log.access_file': '',
            'log.error_file': ''
        }
    )

    config = {
        '/': {
            'tools.sessions.on': True,
            'tools.staticdir.on': True,
            'tools.staticdir.dir': settings.PROJECT_ROOT
        }
    }

下面我发布了自定义日志记录配置logging_configuration.py,我在设置中添加了自定义记录器:

# logger settings
LOGGER_NAME = 'custom'
_LOGGER_HANDLER = 'cherrypy_' + LOGGER_NAME
_MULTIMEDIA_LOGFILE = LOGGER_NAME + '.log'
_ACCESS_LOGFILE = 'access.log'
_ERRORS_LOGFILE = 'errors.log'

LOG_CONF = {
    'version': 1,
    'formatters': {
        'void': {
            'format': ''
        },
        'standard': {
            'format': '%(asctime)s (%(module)15s:%(lineno)2s) [%(levelname)s] %(message)s'
        },
    },
    'handlers': {
        _LOGGER_HANDLER: {
            'level':'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'formatter': 'standard',
            'filename': _MULTIMEDIA_LOGFILE,
            'maxBytes': 10485760,
            'backupCount': 20,
            'encoding': 'utf8'
        },
        'cherrypy_access': {
            'level':'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'formatter': 'void',
            'filename': _ACCESS_LOGFILE,
            'maxBytes': 10485760,
            'backupCount': 20,
            'encoding': 'utf8'
        },
        'cherrypy_error': {
            'level':'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'formatter': 'void',
            'filename': _ERRORS_LOGFILE,
            'maxBytes': 10485760,
            'backupCount': 20,
            'encoding': 'utf8'
        },
    },
    'loggers': {
        LOGGER_NAME: {
            'handlers': [_LOGGER_HANDLER],
            'level': 'INFO',
            'propagate': False
        },
        'cherrypy.access': {
            'handlers': ['cherrypy_access'],
            'level': 'INFO',
            'propagate': False
        },
        'cherrypy.error': {
            'handlers': ['cherrypy_console', 'cherrypy_error'],
            'level': 'INFO',
            'propagate': False
        },
    }
}

我如何使用它?

import cherrypy
from docs import logging_configuration as loggingconf

if __name__ == '__main__':
    cherrypy.config.update(
        {
            'server.socket_host': settings.HOST,
            'server.socket_port': settings.PORT,
            'log.screen': True,
        }
    )

    cherrypy.engine.unsubscribe('graceful', cherrypy.log.reopen_files)
    logging.config.dictConfig(loggingconf.LOG_CONF)  # add it here

    config = {
        '/': {
            'tools.sessions.on': True,
            'tools.staticdir.on': True,
            'tools.staticdir.dir': settings.PROJECT_ROOT
        }
    }
    cherrypy.quickstart(CampPage(), '/', config)

要登录cherrypy_custom.log,请使用:

logger = logging.getLogger(loggingconf.LOGGER_NAME)

logger.info('Hello There...')  # this line will be written into cherrypy_custom.log

要禁用访问日志,请将_ACCESS_FILELOG = ''设置为空字符串

相关问题