import logging
import logging.handlers

class CustomAdapter(logging.LoggerAdapter):
    def process(self, msg, kwargs):
        return '[%s] %s' % (self.extra['connid'], msg), kwargs

rootLogger = logging.getLogger('')
rootLogger.setLevel(logging.DEBUG)
socketHander = logging.handlers.SocketHandler('localhost',
                                              logging.handlers.DEFAULT_TCP_LOGGING_PORT)
# don't bother with a formatter, since a socket handler sends the event as
# an unformatted pickle
# 不要加formatter
rootLogger.addHandler(socketHander)

# Now, we can log to the root logger, or any other logger. First the root...
logging.info('Jackdaws love my big sphinx of quartz.')
# Now, define a couple of other loggers which might represent areas in your
# application:

logger1 = logging.getLogger('myapp.area1')
logger2 = logging.getLogger('myapp.area2')

adapter = CustomAdapter(logger1, {'connid': 3})
adapter.debug('send by adapter')
adapter.info('send by adapter too')
logger1.debug('Quick zephyrs blow, vexing daft Jim.')
logger1.info('How quickly daft jumping zebras vex.')
logger2.warning('Jail zesty vixen who grabbed pay from quack.')
logger2.error('The five boxing wizards jump quickly.')