#!/usr/bin/env python
# encoding: utf-8
import sys
import json
import pickle
import traceback
import web
urls = (
'/ad_word_pv', 'AdWordPV',
)
def getFileRotatingLog(logFilePath, logLevel="info"):
'''
生成n个相同大小的日志文件.
'''
logger = None
logger = logging.getLogger()
if logLevel == "debug":
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)
rh = logging.handlers.RotatingFileHandler(logFilePath, 'a', 10*1024*1024, 5)
formatStr = "[%(asctime)s] %(filename)s:%(lineno)d(%(funcName)s): [%(levelname)s] %(message)s"
format = logging.Formatter(formatStr)
rh.setFormatter(format)
logger.addHandler(rh)
return logger
logger = getFileRotatingLog("logs/svr.log", "info")
web.config.debug = True
class AdWordPV:
def __init__(self,):
pass
def GET(self):
logger.info("request word pv") # 将在日志文件中输出两行一模一样的日志 #
resp = {}
return json.dumps( resp )
if __name__ == "__main__":
app = web.application(urls, globals())
web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
app.run()
|
import pdb
import sys
import logging
from cgitb import html
from logging.handlers import HTTPHandler, SysLogHandler
from logging.handlers import TimedRotatingFileHandler, SMTPHandler
__all__ = ['WsgiLog', 'log']
# File rotation constants
BACKUPS = 1
INTERVAL = 'h'
# Default logger name (should be changed)
LOGNAME = 'wsgilog.log'
# Default 'environ' entries
CATCHID = 'wsgilog.catch'
LOGGERID = 'wsgilog.logger'
# Current proposed 'environ' key signalling no middleware exception handling
THROWERR = 'x-wsgiorg.throw_errors'
# HTTP error messages
HTTPMSG = '500 Internal Error'
ERRORMSG = 'Server got itself in trouble'
# Default log formats
DATEFORMAT = '%a, %d %b %Y %H:%M:%S'
LOGFORMAT = '%(name)s: %(asctime)s %(levelname)-4s %(message)s'
def _errapp(environ, start_response):
'''Default error handling WSGI application.'''
start_response(HTTPMSG, [('Content-type', 'text/plain')], sys.exc_info())
return [ERRORMSG]
def log(**kw):
'''Decorator for logging middleware.'''
def decorator(application):
return WsgiLog(application, **kw)
return decorator
class
WsgiLog(object):
'''Class for WSGI logging and event recording middleware.'''
def __init__(self, application, **kw):
self.application = application
# Error handling WSGI app
self._errapp = kw.get('errapp', _errapp)
# Flag controlling logging
self.log = kw.get('log', True)
# Log if set
if self.log:
# Log error message
self.message = kw.get('logmessage', ERRORMSG)
# Individual logger for WSGI app with custom name 'logname'
self.logger = logging.getLogger(kw.get('logname', LOGNAME))
# Set logger level
self.logger.setLevel(kw.get('loglevel', logging.DEBUG))
# Log formatter
format = logging.Formatter(
# Log entry format
kw.get('logformat', LOGFORMAT),
# Date format
kw.get('datefmt', DATEFORMAT))
# Coroutine for setting individual log handlers
def setlog(logger):
logger.setFormatter(format)
self.logger.addHandler(logger)
# Log to STDOUT
if 'tostream' in kw:
setlog(logging.StreamHandler())
# Log to a rotating file that with periodic backup deletions
if 'tofile' in kw:
setlog(TimedRotatingFileHandler(
# Log file path
kw.get('file', LOGNAME),
# Interval to backup log file
kw.get('interval', INTERVAL),
# Number of backups to keep
kw.get('backups', BACKUPS)))
# Send log entries to an email address
if 'toemail' in kw:
setlog(SMTPHandler(
# Mail server
kw.get('mailserver'),
# From email address
kw.get('frommail'),
# To email address
kw.get('toemail'),
# Email subject
kw.get('mailsubject')))
# Send log entries to a web server
if 'tohttp' in kw:
setlog(HTTPHandler(
# Web server host
kw.get('httphost'),
# Web URL
kw.get('httpurl'),
# HTTP method
kw.get('httpmethod', 'GET')))
# Log to syslog
if 'tosyslog' in kw:
setlog(SysLogHandler(
# syslog host
kw.get('syshost', ('localhost', 514)),
# syslog user
kw.get('facility', 'LOG_USER')))
assert self.logger.handlers, 'At least one logging handler must be configured'
# Redirect STDOUT to the logger
if 'toprint' in kw:
sys.stdout = LogStdout(self.logger,
# Sets log level STDOUT is displayed under
kw.get('prnlevel', logging.DEBUG))
# Flag for turning on PDB in situ
self.debug = kw.get('debug', False)
# Flag for sending HTML-formatted exception tracebacks to the browser
self.tohtml = kw.get('tohtml', False)
# Write HTML-formatted exception tracebacks to a file if provided
self.htmlfile = kw.get('htmlfile')
|
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
#
import sys, logging
from wsgilog import WsgiLog
import config
class Log(WsgiLog):
def __init__(self, application):
WsgiLog.__init__(
self,
application,
logformat = config.logformat,
datefmt = config.datefmt,
tofile = True,
file = config.file,
interval = config.interval,
backups = config.backups
)
|
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
#
import logging
file = "logs/webpy.log" # 日志文件路径 #
logformat = "[%(asctime)s] %(filename)s:%(lineno)d(%(funcName)s): [%(levelname)s] %(message)s" # 日志格式 #
datefmt = "%Y-%m-%d %H:%M:%S" # 日志中显示的时间格式 #
loglevel = logging.DEBUG
interval = "d" # 每隔一天生成一个日志文件#
backups = 3 # 后台保留3个日志文件 #
|
#!/usr/bin/env python
# encoding: utf-8
import sys
import json
import pickle
import traceback
import time
import web
urls = (
'/ad_word_pv', 'AdWordPV',
)
print urls
web.config.debug = True
class AdWordPV:
def __init__(self,):
self.logger = web.ctx.environ['wsgilog.logger'] # 使用日志 #
def GET(self):
btime = time.time()
self.logger.info("request word pv")
resp = {"errcode":0, "data":[test]}
etime = time.time()
use_time = etime - btime
self.logger.info("get word pv used %s seconds" % use_time)
return json.dumps( resp )
if __name__ == "__main__":
from MyLog import Log
app = web.application(urls, globals())
web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
app.run(Log) # 将Log类传入 #
|