Skip to content
Ethan Zhang edited this page Feb 22, 2016 · 2 revisions

Mownfish Log Module

The log Module in mownfish have some feature that can make our project's service log easy reading, collecting, and managing. Mownfish has two default loggers which is 'Main' and 'Access'. You can add your own logger in cmd/mownfishd.py.

Access Logger

Access log record the request informations with one line per request.

Log struct

Log items in access log was separate with ` symbol and has two partitions, the base log partition and the customized log partition.

Base log partition in access log includes start_time, version, ip, url, status, request_time. Customized log partition in access log includes item that were added by developer.

Module api

Mownfish provides a base Tornado RequestHandler named BaseHandler. So, if you implement your own RequestHandler extended from BaseHandler, you can use the access log function as class method. There are three main method about the access log action.

  1. set_accesslog_item(self, item_name, item_value) Use this method to edit the base log item with the name of item_name and the value of item_value.
  2. add_accesslog_item(self, item='', style='s') Use this method to add a customized log item with the value of item
  3. write_accesslog(self) write the whole line of access log to the log file.
class StatInfoHandler(BaseHandler):

    def get(self):
        try:
            result = {'code': ECODE.SUCCESS, 'msg': EMSG.SUCCESS}
            self.add_accesslog_item('customized item')
            self.finish(result)
            #modify the status
            self.set_accesslog_item('status', 'SUCCESS')

        except BaseError as e:
            LOG.error(e, exc_info=True)
            self.finish({'code':e.e_code, 'msg': '%s' % e})
        except Exception as e:
            LOG.error(e, exc_info=True)
            self.finish({'code':ECODE.DEFAULT, 'msg':
                'Unknown'})

        finally:
            self.write_accesslog()

Main Logger

The root logger of Python logging module. There are two ways for requiring the logger handler.

#first way from python logging module
import logging

logger = logging.getLogger('')
logger.debug('example 1')

# second way from global var LOG in mownfish
from util.log import LOG

LOG.debug('example 2')

Clone this wiki locally