1. 把下面的内容保存为util.py
定义logger
# coding: utf8
# luofuwen
import logging
class Util:
@staticmethod
def logger(config):
'''
获取logger
'''
fh = logging.FileHandler(config['logFile'], encoding="utf-8")
fmt = logging.Formatter(config['logFmt'])
fh.setFormatter(fmt)
logger = logging.getLogger(config['logName'])
logger.setLevel(config['logLevel'])
logger.addHandler(fh)
return logger
2. 把下面的内容保存为conf.py
用于定义log的储存位置和格式
# coding:utf8
# luofuwen
import logging
class Config:
# log config
loggerConfig = {
'logFile':'/tmp/workflow_block.log',
'logFmt':'%(asctime)s - %(filename)s:%(lineno)s - %(name)s - %(message)s',
'logLevel': logging.INFO,
'logName': 'WorkflowXBlockLogger',
}
from conf import Config
from util import Util
import logging
class WorkflowXBlock(XBlock):
"""
这是一个工作流测试的xblock
"""
# Fields are defined on the class. You can access them in your code as
# self.<fieldname>.
# TO-DO: delete count, and define your own fields.
logger = Util.logger(Config.loggerConfig)
@XBlock.json_handler
def increment_count(self, data, suffix=''):
"""
An example handler, which increments the data.
"""
# Just to show data coming in...
assert data['hello'] == 'world'
self.count += 1
student = self.runtime.get_real_user(self.runtime.anonymous_student_id)
studentEmail = student.email
studentUsername = student.username
studentIsStaff = student.is_staff
self.logger.info('workflow test:%s, email=%s,username=%s,isStaff=%s',str(self.count),studentEmail,studentUsername,studentIsStaff)
return {"count": self.count}