Can I use generic logging.Logger () instead ?
Yes, you can use the Python / Django generic logger. Instead of logger = get_task_logger(__name__) just select getLogger :
import logging logger = logging.getLogger(__name__)
Why should I inherit from him?
Benefit of using celery.task :
automatically receives the task name and unique identifier as part of the logs.
which itself uses the standard Python logging lib.
This is not enough. Is there more details? In particular, which handlers and formatted strings does it define by default?
By default, it uses the WatchedFileHandler from logging.handlers You can check the celery.app.log documentation, which is set in setup_handler .
What are the best methods for registering in a file from celery tasks (and not Django)? etc.
1. Directly in the module . You can simply place everything at the top of your module - define a handler and at the same time assign it a registrar:
import logging
2. Use the logging_config- dictConfig dictionary : a much better practice.
import logging from logging.config import dictConfig logging_config = dict( version = 1, formatters = { 'f': {'format': '%(asctime)s %(name)-12s %(levelname)-8s %(message)s'} }, handlers = { 'h': {'class': 'logging.StreamHandler', 'formatter': 'f', 'level': logging.DEBUG} }, root = { 'handlers': ['h'], 'level': logging.DEBUG, }, ) dictConfig(logging_config) logger = logging.getLogger() logger.debug('often makes a very good meal of %s', 'visiting tourists')
3. Using a separate logging file format - *. Ini :
[loggers] # list of loggers keys=root,log02 [handlers] # list of handlers keys=hand01,hand02 [formatters] # list of formatters keys=form01,form02 [logger_root] # config for 'root' logger level=NOTSET handlers=hand01 [handler_hand01] # config for handler hand01 class=StreamHandler level=NOTSET formatter=form01 args=(sys.stdout,) [handler_hand02] ## config for handler hand02 class=FileHandler level=DEBUG formatter=form02 args=('python.log', 'w') [formatter_form01] # config for formatter form01 format=F1 %(asctime)s %(levelname)s %(message)s datefmt= class=logging.Formatter
4. Separate class - And last but not least, a more elegant solution is to have it all as a separate module (class) and simply import it with certain flags when required:
import logging from config import LOG, LOGGING class Logger: def __init__(self, logf=None, logger_name=None, debug=None, rotation=None): self.logfile = logf self.logger_name = logger_name if logger_name else logf self.logger = self.get_logger(rotation=rotation) if rotation else self.get_logger() self.debug = debug if debug else False self.debug = debug if debug else LOG["debug"] def logf(self, filename=None): if filename is None: filename = LOG["file"] return "%s%s" % (LOG["dir"], filename) def get_logger(self, rotation=False): logger = logging.getLogger(self.logger_name) logf = self.logf(self.logfile) if rotation: from logging.handlers import TimedRotatingFileHandler self.handler = TimedRotatingFileHandler(logf, when='midnight', interval=1, backupCount=10) else: self.handler = logging.FileHandler(logf) formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')