Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is celery.utils.log.ProcessAwareLoggerobject doing in logging.Logger.manager.loggerDict

I am inspecting the logging.Logger.manager.loggerDict by doing:

import logging
logging.Logger.manager.loggerDict

and the dict is as follows:

{
  'nose.case': <celery.utils.log.ProcessAwareLoggerobjectat0x112c8dcd0>,
  'apps.friends': <logging.PlaceHolderobjectat0x1147720d0>,
  'oauthlib.oauth2.rfc6749.grant_types.client_credentials': <celery.utils.log.ProcessAwareLoggerobjectat0x115c48710>,
  'apps.adapter.views': <celery.utils.log.ProcessAwareLoggerobjectat0x116a847d0>,
  'apps.accounts.views': <celery.utils.log.ProcessAwareLoggerobjectat0x116976990>,

  }
  There are more but I truncated it

My questions are :

  1. How come celery is involved in logging of various other non-celery apps? Is it because logging is done in an async way and somehow logging framework detects presence of celery and uses it?
  2. For two of my own files that are logging using logger = logging.getLogger(__name__) , I see one is PlaceHolderObject and other two it is celery.utils.log.ProcessAwareLogger object - although these latter two are called in views and not in celery processes. How did it become this way then

Thanks

like image 676
dowjones123 Avatar asked Feb 14 '16 11:02

dowjones123


People also ask

What does logging getLogger (__ Name __) do?

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. Multiple calls to getLogger() with the same name will return a reference to the same logger object.

What does import logging do in Python?

With the logging module imported, you can use something called a “logger” to log messages that you want to see. By default, there are 5 standard levels indicating the severity of events. Each has a corresponding method that can be used to log events at that level of severity.

What is logger setLevel Python?

"The setLevel() method, just as in logger objects, specifies the lowest severity that will be dispatched to the appropriate destination. Why are there two setLevel() methods? The level set in the logger determines which severity of messages it will pass to its handlers.

What is a logging handler?

The log handler is the component that effectively writes/displays a log: Display it in the console (via StreamHandler), in a file (via FileHandler), or even by sending you an email via SMTPHandler, etc. Each log handler has 2 important fields: A formatter which adds context information to a log.


1 Answers

Celery itself replaces the (global) logger class, using the logging.setLoggerClass method, with a ProcessAwareLogger class that does a couple of things: avoid trying to log while in a signal handler, and add a process name to logs. This happens as soon as Celery's logging system is set up. You're seeing this class even on your own loggers because of the global nature of setLoggerClass.

As for why, exactly, Celery is designed like that, I think you'd have to ask a developer of Celery, but effectively it allows Celery to ensure that signal handler safety and process name are taken care of even if you use your own loggers in your app.

The python logging docs note:

If you are implementing asynchronous signal handlers using the signal module, you may not be able to use logging from within such handlers. This is because lock implementations in the threading module are not always re-entrant, and so cannot be invoked from such signal handlers.

Celery uses signal so this may be a reason for wanting to globally enforce its logger class.

like image 93
Jason S Avatar answered Oct 05 '22 11:10

Jason S