日志
在任何一个完整的程序中,日志都是必不可少的一部分。日志的作用无需细说,下面是一个封装了发送日志到本地和syslog服务器的接口。
# -*- coding:utf-8 -*-
"""
File Name : 'log'.py
Description:
Author: 'chengwei'
Date: '2016/5/24' '16:24'
python: 2.7.10
"""
import logging
from logging.handlers import SysLogHandler
import time
import ConfigParser
import os
import codecs
def example():
# 发送到远程syslogserver
logger1 = to_syslog('local2', logging.INFO)
for i in range(0, 20):
time.sleep(0.5)
msg = "abc %s" % i
logger1.error(msg)
# 存储日志到本地指定位置
logger2 = local_log("test.log")
for i in range(0, 20):
time.sleep(1.1)
msg = "abc %s" % i
logger2.error(msg)
def local_log(logname, level=logging.INFO):
"""
:param logname:表示存储路径和文件名
:param level:表示日志输出级别logging.DEBUG,logging.INFO等等,默认为logging.INFO
:return:logger
"""
format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(filename)s[line:%(lineno)d] - %(message)s')
handler = logging.FileHandler(logname)
handler.setFormatter(format)
logger = logging.getLogger()
logger.addHandler(handler)
logger.setLevel(level)
return logger
def to_syslog(user, level=logging.INFO):
"""
发送日志到syslog服务器
:param user:表示自定义local0-7
:param level:表示日志输出级别logging.DEBUG,logging.INFO等等,默认为logging.INFO
:return:logger
"""
sysloghost = init()
format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(filename)s[line:%(lineno)d] - %(message)s')
handler = SysLogHandler(address=(sysloghost, 514), facility=user)
handler.setFormatter(format)
logger = logging.getLogger()
logger.addHandler(handler)
logger.setLevel(level)
return logger
def init(parasecname="LOG"):
"""
从配置文件获取参数
:param parasecname: 配置文件中节点名称
:return:配置信息
"""
cur_script_dir = os.path.split(os.path.realpath(__file__))[0]
cfg_path = os.path.join(cur_script_dir, "db.conf")
cfg_reder = ConfigParser.ConfigParser()
secname = parasecname
cfg_reder.readfp(codecs.open(cfg_path, "r", "utf_8"))
sysloghost = cfg_reder.get(secname, "server")
return sysloghost
if __name__ == '__main__':
example()