I am developing customized logger program,as per the requirement i need to fetch the process,thread and name of object Inside the called function(In below example its obj needs to fetch inside the get_configured_logger function) and class name to which obj belongs. as shown with comments in below code, please give some ideas to achieve this.
我正在开发定制的logger程序,根据需要我需要获取进程,线程和对象的名称在被调用的函数内部(在下面的例子中它的obj需要在get_configured_logger函数内部获取)和obj所属的类名。如下面代码中的注释所示,请提供一些实现此目的的想法。
import logging, logging.handlers
from logging import StreamHandler, Formatter
class A:
def get_configured_logger(self,name):
logger = logging.getLogger(name)
if (len(logger.handlers) == 0):
FORMAT = "%(process)s %(thread)s:-(asctime)s - %(name)s - %(levelname)s - %(message)-%(module)"
#print 'process:',process
#print 'thread:',thread
#print 'levelname:',levelname
#print 'Module:',(name portion of filename).
#print 'obj:,'name of the object(Eg:obj),current function( Eg: get_configured_logger) called by'
#print 'class name:(obj is instance of class)'
formatter = logging.Formatter(fmt=FORMAT)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
return logger
if __name__=="__main__":
obj=A()
logger = obj.get_configured_logger("DEMO")
logger.debug("TEST")
Thanks
谢谢
hema
HEMA
2 个解决方案
#1
17
To add current process/thread name to a log message you could specify in the format string:
要将当前进程/线程名称添加到日志消息中,您可以在格式字符串中指定:
%(processName)s %(threadName)s
To get them as strings:
将它们作为字符串:
process_name = multiprocessing.current_process().name
thread_name = threading.current_thread().name
#2
4
Thread name
The documentation describes an easy interface to access the current Thread
, you can then use its name
attribute to have the Thread name.
该文档描述了一个访问当前Thread的简单界面,然后您可以使用其name属性来获取Thread名称。
You can then use:
然后你可以使用:
import threading
threading.current_thread().name
Be aware that it's not unique.
请注意,它并不是唯一的。
Process name
There is no easy way of getting the process name as this is dependent on the OS you are using. You can however get the process ID (pid
) doing:
获取进程名称没有简单的方法,因为这取决于您使用的操作系统。但是,您可以获取进程ID(pid):
import os
os.getpid()
That is unless you are using the multiprocessing
module and launch subprocesses, in which case you can use the multiprocessing
module which presents an interface closely similar to that of the Threading
module.
这是除非您使用多处理模块并启动子进程,在这种情况下,您可以使用多处理模块,该模块提供与Threading模块非常相似的接口。
#1
17
To add current process/thread name to a log message you could specify in the format string:
要将当前进程/线程名称添加到日志消息中,您可以在格式字符串中指定:
%(processName)s %(threadName)s
To get them as strings:
将它们作为字符串:
process_name = multiprocessing.current_process().name
thread_name = threading.current_thread().name
#2
4
Thread name
The documentation describes an easy interface to access the current Thread
, you can then use its name
attribute to have the Thread name.
该文档描述了一个访问当前Thread的简单界面,然后您可以使用其name属性来获取Thread名称。
You can then use:
然后你可以使用:
import threading
threading.current_thread().name
Be aware that it's not unique.
请注意,它并不是唯一的。
Process name
There is no easy way of getting the process name as this is dependent on the OS you are using. You can however get the process ID (pid
) doing:
获取进程名称没有简单的方法,因为这取决于您使用的操作系统。但是,您可以获取进程ID(pid):
import os
os.getpid()
That is unless you are using the multiprocessing
module and launch subprocesses, in which case you can use the multiprocessing
module which presents an interface closely similar to that of the Threading
module.
这是除非您使用多处理模块并启动子进程,在这种情况下,您可以使用多处理模块,该模块提供与Threading模块非常相似的接口。