Skip to content

Cron fails to log, while calling the function in django's shell works #31

Closed

Description

@washougal

I have a tasks.py file that's located in project/app/tasks.py that uses the standard logger:

import logging
logger = logging.getLogger(__name__)

The logging file is configured with loggers for all of the following:
django, app, django_crontab, django_crontab.crontab, crontab, and ''

When is open the shell and run the function, the logging appears:

python manage.py shell
import django
django.setup()
from app.tasks import my_scheduled_task()
my_scheduled_task()

Yet, when I add the cron job using python manage.py crontab add everything works (I can see the end result appear), yet nothing gets logged. Any reason why logging should work using the shell, but not as a cron?

Activity

  1. kraiz commented on Mar 21, 2016

    @kraiz
    Owner

    Shouldn't be there a logger for app.tasks as well? Depending on how log propagation is configured, that may a reason. But sure, if it's working from the django shell...I try to check that soon.

  2. kraiz commented on Mar 29, 2016

    @kraiz
    Owner

    Can you provide your Django & Python version with your (simplified) settings for CRONJOBS, LOGGING?

    Additionally what happens when you run

    $ python manage.py crontab show
    

    and find the logging job's hashed id and run

    $ python manage.py crontab run <id>
    

    Does the logging work in this case?

  3. washougal commented on Mar 29, 2016

    @washougal
    Author

    Django: 1.9.4, Py: 2.7.11

    In settings.py:

    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'verbose': {
                'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
                'datefmt' : "%d/%b/%Y %H:%M:%S"
            },
            'simple': {
                'format': '%(levelname)s %(message)s'
            },
        },
        'handlers': {
            'file': {
                'level': 'DEBUG',
                'class': 'logging.FileHandler',
                'filename': 'mysite.log',
                'formatter': 'verbose'
            },
            'scraper_file' : {
                'level': 'DEBUG',
                'class': 'logging.FileHandler',
                'filename': 'scraping.log',
                'formatter': 'verbose'
            }
        },
        'loggers': {
            'django': {
                'handlers':['file'],
                'propagate': True,
                'level':'DEBUG',
            },
            'scraper': {
                'handlers': ['scraper_file'],
                'level':'DEBUG',
                'propagate': True,
            },
            'django_crontab': {
                'handlers': ['scraper_file'],
                'level':'DEBUG',
                'propagate': True,
            },
    
            'django_crontab.crontab': {
                'handlers': ['scraper_file'],
                'level':'DEBUG',
                'propagate': True,
            },
            'crontab': {
                'handlers': ['scraper_file'],
                'level':'DEBUG',
                'propagate': True,
            },
            'scraper.django_crontab.crontab': {
                'handlers': ['scraper_file'],
                'level':'DEBUG',
                'propagate': True,
            },
            'scraper.tasks': {
                'handlers': ['scraper_file'],
                'level':'DEBUG',
                'propagate': True,
            },
        }
    }
    

    When runnig python manage.py crontab show it returns:
    Currently active jobs in crontab: d2cbb85ad5f39a8d633df70b63312728 -> ('0/8 * * * *', 'scraper.tasks.my_scheduled_job')

    and running python manage.py crontab run d2cbb85ad5f39a8d633df70b63312728 both correctly runs the function, and logs correctly. However, when cron runs the function, I can see the results showing up in the database, but nothing is being logged.

  4. kraiz commented on Apr 1, 2016

    @kraiz
    Owner

    Maybe we're running into a problem because the current working dir differs when started via cron. Please change the FileHandler's filename to be absolute. Does this help?

    Another try: switch to ConsoleHandler and pipe result into a file:

        ('0   0 1 * *', 'myapp.cron.my_scheduled_job', '>> /tmp/scheduled_job.log'),
    

    Does this work?

  5. reata commented on May 6, 2016

    @reata

    I came into the same problem. After changing the FileHandler's filename to be absolute path, it all settled. Probably the working dir issue. Thanks.

  6. kraiz commented on May 8, 2016

    @kraiz
    Owner

    Thanks for sharing @Ryan-ZJU. I added a faq entry about this. Hopefully this helps the next one :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions