码迷,mamicode.com
首页 > 其他好文 > 详细

logger示例

时间:2018-05-28 16:50:30      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:date   console   _for   inf   atm   env   log   return   OLE   

胜哥版

#!/usr/bin/env python
# encoding: utf-8  
# Date: 2018/5/27

import logging


def get_logger(name):

    logger = logging.getLogger(name)
    logger.setLevel(logging.INFO)
    ch = logging.StreamHandler()
    fh = logging.FileHandler(‘atm.log‘)
    logger.addHandler(ch)
    logger.addHandler(fh)

    file_formatter = logging.Formatter(‘%(asctime)s - %(name)s - %(levelname)s - %(message)s‘)
    console_formatter = logging.Formatter(‘%(asctime)s - %(name)s - %(levelname)s - %(lineno)d - %(message)s‘)

    ch.setFormatter(console_formatter)
    fh.setFormatter(file_formatter)

    return logger

 

调用:

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2018/5/28 16:23
# @File    : test_logger.py

from logger_demo import get_logger


logger = get_logger(‘atm‘)
logger.info(‘test‘)

logger = get_logger(‘shopping‘)
logger.info(‘shopping test‘)

logger示例

标签:date   console   _for   inf   atm   env   log   return   OLE   

原文地址:https://www.cnblogs.com/fmgao-technology/p/9100788.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!