# -*- coding: utf-8 -*-
import logging
from logging import handlers
log = logging.getLogger()
hl = handlers.SysLogHandler(address=(ipaddr, 514), facility=22)
log.addHandler(hl)
log.setLevel(logging.DEBUG)
a = '中文测试'
log.debug(a)
来自syslog服务器的结果
Feb 11 12:55:12 10.20.51.39 涓枃娴嬭瘯
Feb 11 12:55:47 10.20.51.39 涓枃娴嬭瘯
Feb 11 12:56:16 10.20.51.39 涓枃娴嬭瘯
将编码改为GB2312
# -*- coding: gb2312 -*-
import logging
from logging import handlers
log = logging.getLogger()
hl = handlers.SysLogHandler(address=(ipaddr, 514), facility=22)
log.addHandler(hl)
log.setLevel(logging.DEBUG)
a = '中文测试'
log.debug(a)
结果总是相同的
Feb 11 12:57:04 10.20.51.39 涓枃娴嬭瘯
Feb 11 12:58:18 10.20.51.39 涓枃娴嬭瘯
但是这个python脚本适用于python2.7
syslog服务器区域设置
LANG=zh_CN.gb2312
LC_CTYPE="zh_CN.gb2312"
LC_NUMERIC="zh_CN.gb2312"
LC_TIME="zh_CN.gb2312"
LC_COLLATE="zh_CN.gb2312"
LC_MONETARY="zh_CN.gb2312"
LC_MESSAGES="zh_CN.gb2312"
LC_PAPER="zh_CN.gb2312"
LC_NAME="zh_CN.gb2312"
LC_ADDRESS="zh_CN.gb2312"
LC_TELEPHONE="zh_CN.gb2312"
LC_MEASUREMENT="zh_CN.gb2312"
LC_IDENTIFICATION="zh_CN.gb2312"
LC_ALL=zh_CN.gb2312
所有结果
Feb 11 12:48:22 10.20.51.39 涓枃娴嬭瘯
Feb 11 12:48:46 10.20.51.39 涓枃娴嬭瘯
Feb 11 12:55:06 10.20.51.39 涓枃娴嬭瘯
Feb 11 12:55:12 10.20.51.39 涓枃娴嬭瘯
Feb 11 12:55:47 10.20.51.39 涓枃娴嬭瘯
Feb 11 12:56:16 10.20.51.39 涓枃娴嬭瘯
Feb 11 12:57:04 10.20.51.39 涓枃娴嬭瘯
Feb 11 12:58:18 10.20.51.39 涓枃娴嬭瘯
Feb 11 13:00:59 10.20.51.39 中文测试 << send via python2 with gb2312 encodidng
Feb 11 13:01:17 10.20.51.39 涓枃娴嬭瘯
Feb 11 13:01:28 10.20.51.39 中文测试 << send via python2 with gb2312 encodidng