5
回答
求个 python logging配置文件例子
终于搞明白,存储TCO原来是这样算的>>>   

各位大神有无logging配置文件例子供参考

网上查询到的都差点东西

需要达到error日志和info两种级别即可

两种级别日志输出到不同文件中,按每天输出日志 文件超过10M重新生成文件


有大神帮忙?万分感谢

举报
yy670010378
发帖于2年前 5回/165阅
共有5个答案 最后回答: 2年前

下面是project structure:

log output:

注意:logging.getLogger(arg) arg reflects the project structure.

你需要加 logger clean up code

loggger.py

import logging
import sys


def init():
    """

    This function only run once
    create 2 log outputs : a file and console
    :return:
    """
    #-----------------------------------------------------
    # create an instance of logging.Logger
    #-----------------------------------------------------
    logger=logging.getLogger("myproj")


    #-----------------------------------------------------
    # create an instance of FileHandler for the log file
    #-----------------------------------------------------
    log_file=r'C:\users\bckong\desktop\tmp\test.log'
    fp=logging.FileHandler(filename=log_file, mode='a')


    # output message format: [level ], [module name ],[function name ], [line number ] message
    # you can change output format
    fmt_string = '[%(levelname)-9s] [%(module)-20s] [%(funcName)-20s %(lineno)4d]  %(message)s'


    # create an instance of logging.Formatter
    formatter = logging.Formatter(fmt=fmt_string)
    fp.setFormatter(formatter)


    # add the log file handler to the logger
    logger.addHandler(fp)


    #-----------------------------------------------------
    # create an instance of StreamHandler for console output
    #-----------------------------------------------------
    log_console=logging.StreamHandler(stream=sys.stdout)
    log_console.setFormatter(formatter)


    # add the console output to the logger
    logger.addHandler(log_console)


using_mylogger.py


import logging
import logger


class Foo():


    def __init__(self):
        print "----> ", __file__
        self.log=logging.getLogger("myproj.module.file02")
        self.log.setLevel(logging.DEBUG)


        self.log.info("instance of Foo")


    def greatings(self):
        self.log.debug("Welcome to logging module")


f=Foo()
f.greatings()

--- 共有 1 条评论 ---
yy670010378你好 myproj这个配置文件可以发出了看看? 2年前 回复
[loggers]
keys=root,example


[handlers]
keys=consoleHandler,rotateFileHandler


[formatters]
keys=simpleFormatter


[formatter_simpleFormatter]
format=[%(asctime)s](%(levelname)s)%(name)s : %(message)s


[logger_root]
level=INFO
handlers=consoleHandler,rotateFileHandler


[logger_example]
level=INFO
handlers=consoleHandler,rotateFileHandler
qualname=example
propagate=0


[handler_consoleHandler]
class=StreamHandler
level=INFO
formatter=simpleFormatter
args=(sys.stdout,)


[handler_rotateFileHandler]
class=handlers.RotatingFileHandler
level=INFO
formatter=simpleFormatter
args=('info.log', 'info', 200000, 9)


[handler_rotateFileHandler]
class=handlers.RotatingFileHandler
level=ERROR
formatter=simpleFormatter

args=('error.log', 'error', 200000, 9)

import logging
import logging.config
 
logging.config.fileConfig("logging.conf")
logger = logging.getLogger("example")
 
logger.info('This is info message')
logger.error('This is error message')

为啥运行之后只生成了error.log文件 球大神帮忙看看

顶部