def test_log_adapter():
import logging
from takumi.log import MetaAdapter
import sys
ctx = {}
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
logger = MetaAdapter(logging.getLogger('takumi'), {'ctx': ctx})
logger_class = logging.getLoggerClass()
with mock.patch.object(logger_class, '_log') as mock_log:
logger.info('hello world')
mock_log.assert_called_with(20, '[-/- -] hello world', ())
ctx['meta'] = {
'client_name': 'test_client',
'client_version': '1.0.1'
}
ctx['env'] = {'client_addr': '127.0.0.1'}
with mock.patch.object(logger_class, '_log') as mock_log:
logger.info('hello world')
mock_log.assert_called_with(
20, '[test_client/1.0.1 127.0.0.1] hello world', ())
ctx['log_extra'] = '353456436546 xxxx yyyy'
with mock.patch.object(logger_class, '_log') as mock_log:
logger.info('hello world')
mock_log.assert_called_with(
20, '[test_client/1.0.1 127.0.0.1 353456436546 xxxx yyyy] hello world',
())
评论列表
文章目录