Loggers in Python

简介:
  1. Loggers will send to stderr by default. So make sure stderr has a receiver.

Logger manager

Loggers will be stored in manager in Python. Handlers will cause resource leak if not removed.

def getLogger(name=None):
    """
    Return a logger with the specified name, creating it if necessary.

    If no name is specified, return the root logger.
    """
    if name:
        return Logger.manager.getLogger(name)
    else:
        return root

class Manager(object):
    def getLogger(self, name):
        """
        Get a logger with the specified name (channel name), creating it
        if it doesn't yet exist. This name is a dot-separated hierarchical
        name, such as "a", "a.b", "a.b.c" or similar.

        If a PlaceHolder existed for the specified name [i.e. the logger
        didn't exist but a child of it did], replace it with the created
        logger and fix up the parent/child references which pointed to the
        placeholder to now point to the logger.
        """
        rv = None
        if not isinstance(name, basestring):
            raise TypeError('A logger name must be string or Unicode')
        if isinstance(name, unicode):
            name = name.encode('utf-8')
        _acquireLock()
        try:
            if name in self.loggerDict:
                rv = self.loggerDict[name]
                if isinstance(rv, PlaceHolder):
                    ph = rv
                    rv = (self.loggerClass or _loggerClass)(name)
                    rv.manager = self
                    self.loggerDict[name] = rv
                    self._fixupChildren(ph, rv)
                    self._fixupParents(rv)
            else:
                rv = (self.loggerClass or _loggerClass)(name)
                rv.manager = self
                self.loggerDict[name] = rv
                self._fixupParents(rv)
        finally:
            _releaseLock()
        return rv

Loggers in Python are stored in Logger.manager with a specified name. If create a logger with a task rather than a file, loggers stored in manager will increase and never be deleted.
It becomes even worse when the logger has a handler, eg. write logs to database. It may cause a connections leak.

Log emit will hang if stderr is not received

Log info will send to stderr by default, make sure stderr buffer has a removed, otherwise stream write will be blocked.


目录
相关文章
|
13天前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
187 102
|
13天前
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
187 104
|
13天前
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
180 103
|
13天前
|
机器学习/深度学习 人工智能 数据挖掘
Python:现代编程的首选语言
Python:现代编程的首选语言
126 82
|
13天前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的多面手
Python:现代编程的多面手
30 0
|
22天前
|
存储 人工智能 算法
Python实现简易成语接龙小游戏:从零开始的趣味编程实践
本项目将中国传统文化与编程思维相结合,通过Python实现成语接龙游戏,涵盖数据结构、算法设计与简单AI逻辑,帮助学习者在趣味实践中掌握编程技能。
83 0
|
2月前
|
安全 测试技术 数据处理
Python列表推导式进阶:从简洁代码到高效编程的10个核心技巧
列表推导式是Python中高效的数据处理工具,能将多行循环代码压缩为一行,提升代码可读性与执行效率。本文详解其基础语法、嵌套循环、条件表达式、函数融合、性能优化等进阶技巧,并结合实战案例与边界条件处理,帮助开发者写出更优雅、高效的Python代码。
116 0
|
2月前
|
机器学习/深度学习 人工智能 运维
Python:简洁高效的万能编程胶水
Python:简洁高效的万能编程胶水
|
4月前
|
Python
Python编程基石:整型、浮点、字符串与布尔值完全解读
本文介绍了Python中的四种基本数据类型:整型(int)、浮点型(float)、字符串(str)和布尔型(bool)。整型表示无大小限制的整数,支持各类运算;浮点型遵循IEEE 754标准,需注意精度问题;字符串是不可变序列,支持多种操作与方法;布尔型仅有True和False两个值,可与其他类型转换。掌握这些类型及其转换规则是Python编程的基础。
227 33
|
3月前
|
数据采集 分布式计算 大数据
不会Python,还敢说搞大数据?一文带你入门大数据编程的“硬核”真相
不会Python,还敢说搞大数据?一文带你入门大数据编程的“硬核”真相
96 1