新版python logging 封装,支持同时向console,file,socket输出

将python的logging封装更新了一下,目前支持同时向console,file,socket输出,同时在config_logging或者config_logging_plus的时候先清除根logger的所有handler,避免在某些情况下的重复输出。
具体代码如下:

# -*- coding: utf-8 -*-
'''
Modified on 2012-11-27
@summary: clear old root logger handlers when reconfig logging
@author: JerryKwan

Created on 2012-06-14 19:50
@summary:  logging config
@author: JerryKwan
'''

import logging

import logging.handlers

import os
import sys

LEVELS = {'NOSET': logging.NOTSET,
          'DEBUG': logging.DEBUG,
          'INFO': logging.INFO,
          'WARNING': logging.WARNING,
          'ERROR': logging.ERROR,
          'CRITICAL': logging.CRITICAL}

#set up logging to file

#logging.basicConfig(level = logging.NOTSET,
#                    format = "%(asctime)s %(name)-12s %(levelname)-8s %(message)s"
#                    )

##                    filename = "./log.txt",

##                    filemode = "w")

# create logs file folder
def config_logging(file_name = "log.txt", log_level = "NOSET"):
    '''
    @summary: config logging to write logs to local file
    @param file_name: name of log file
    @param log_level: log level
    '''
    logs_dir = os.path.join(os.path.dirname(__file__), "logs")
    if os.path.exists(logs_dir) and os.path.isdir(logs_dir):
        pass
    else:
        os.makedirs(logs_dir)

    # clear old root logger handlers
    logging.getLogger("").handlers = []

    file_name = os.path.join(logs_dir, file_name)
    # define a rotating file handler
    rotatingFileHandler = logging.handlers.RotatingFileHandler(filename =file_name,
                                                      maxBytes = 1024 * 1024 * 50,
                                                      backupCount = 5)
    formatter = logging.Formatter("%(asctime)s %(name)-12s %(levelname)-8s %(message)s")
    rotatingFileHandler.setFormatter(formatter)
    logging.getLogger("").addHandler(rotatingFileHandler)

    # define a handler whitch writes messages to sys
    console = logging.StreamHandler()
    # set a format which is simple for console use
    formatter = logging.Formatter("%(name)-12s: %(levelname)-8s %(message)s")
    # tell the handler to use this format
    console.setFormatter(formatter)
    # add the handler to the root logger
    logging.getLogger("").addHandler(console)
    # set initial log level
    logger = logging.getLogger("")
    level = LEVELS[log_level.upper()]
    logger.setLevel(level)

def config_logging_plus(file_name = "log.txt", log_level = "NOSET", remote_address = ("127.0.0.1", 8888), write_console = False):
    '''
    @summary: config logging to write logs to remote service
    @param host: hostname or ip address of the log server
    @param port: port to be used for log server
    @log_level: log level
    '''
    logs_dir = os.path.join(os.path.dirname(__file__), "logs")
    if os.path.exists(logs_dir) and os.path.isdir(logs_dir):
        pass
    else:
        os.makedirs(logs_dir)
    # format file name
    if file_name is None:
        file_name = os.path.splitext(sys.argv[0])[0]
        file_name = os.path.join(logs_dir, "%s_%s.log"%(file_name, os.getpid()))
    else:
        file_name = os.path.join(logs_dir, file_name)

    # clear old root logger handlers
    logging.getLogger("").handlers = []
    
    # define a rotating file handler
    rotatingFileHandler = logging.handlers.RotatingFileHandler(filename =file_name,
                                                      maxBytes = 1024 * 1024 * 50,
                                                      backupCount = 5)
    formatter = logging.Formatter("%(asctime)s %(name)-12s %(levelname)-8s %(message)s")
    rotatingFileHandler.setFormatter(formatter)
    # add log handler
    logging.getLogger("").addHandler(rotatingFileHandler)

    if write_console is not None and  write_console == True:
        # define a handler whitch writes messages to sys
        console = logging.StreamHandler()
        console.setLevel(logging.NOTSET)
        # set a format which is simple for console use
        formatter = logging.Formatter("%(name)-12s: %(levelname)-8s %(message)s")
        # tell the handler to use this format
        console.setFormatter(formatter)
        # add log handler
        logging.getLogger("").addHandler(console)

    if remote_address is not None and hasattr(remote_address, "__iter__") and len(remote_address) > 1:
        # define a socket handler
        socketHandler = logging.handlers.SocketHandler(remote_address[0], remote_address[1])
        formatter = logging.Formatter("%(asctime)s %(processName)s %(process)s %(name)-12s %(levelname)-8s %(message)s")
        socketHandler.setFormatter(formatter)
        # add log handler
        logging.getLogger("").addHandler(socketHandler)

    # set initial log level
    logger = logging.getLogger("")
    level = LEVELS[log_level.upper()]
    logger.setLevel(level)

使用方法很简单,只需要需要config logging的时候config一下即可,示例代码如下:

import loggingconfig
loggingconfig.config_logging(file_name = "log.log", log_level = "NOSET")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值