Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch process,thread name,levelname in customized python logger

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.

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

like image 835
user1559873 Avatar asked Oct 20 '12 16:10

user1559873


People also ask

How do you log a thread ID in Python?

get_ident() function. In Python 3.3+, you can use threading. get_ident() function to obtain the thread ID of a thread.

What is logging getLogger (__ Name __)?

To start logging using the Python logging module, the factory function logging. getLogger(name) is typically executed. The getLogger() function accepts a single argument - the logger's name. It returns a reference to a logger instance with the specified name if provided, or root if not.

What is logging basicConfig in Python?

Python logging basicConfig The basicConfig configures the root logger. It does basic configuration for the logging system by creating a stream handler with a default formatter. The debug , info , warning , error and critical call basicConfig automatically if no handlers are defined for the root logger.


2 Answers

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
like image 133
jfs Avatar answered Sep 21 '22 11:09

jfs


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.

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:

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.

like image 32
Thomas Orozco Avatar answered Sep 21 '22 11:09

Thomas Orozco