Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
washougal opened this issue Mar 19, 2016 · 6 comments
Closed

Comments

@washougal
Copy link

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?

@kraiz
Copy link
Owner

kraiz commented Mar 21, 2016

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.

@kraiz
Copy link
Owner

kraiz commented Mar 29, 2016

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?

@washougal
Copy link
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.

@kraiz
Copy link
Owner

kraiz commented Apr 1, 2016

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?

@reata
Copy link

reata commented May 6, 2016

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.

kraiz added a commit that referenced this issue May 8, 2016
@kraiz
Copy link
Owner

kraiz commented May 8, 2016

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants