0

I have been trying to use loggers (https://docs.python.org/2/library/logging.html) in my own scripts and now using django settings via the excellent post by ianalexander here:

http://ianalexandr.com/blog/getting-started-with-django-logging-in-5-minutes.html

but the same problem persists in both cases. My error messages come out in the STDOUT . . the command line that started the server . . and NOT in the file mentioned. What is interesting is the the files are created, however. Does any one have any idea why the files would open but not be written too?

Interestingly this is not true when I call my own scripts via command line so my use of django is very likely involved.

These are my django settings with commented out portions and all. Any help is greatly appreciated!

Thank you!

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': '/path/to/django/debug.log',
#     },
# },
'handlers': {
    'main_file': {
        'level': 'DEBUG',
        'class': 'logging.FileHandler',
        'filename': 'django_main.log',
        'formatter': 'verbose'
    },
},

# 'filters': {
#     'special': {
#         '()': 'project.logging.SpecialFilter',
#         'foo': 'bar',
#     }
# },

# 'loggers': {
#     'django.request': {
#         'handlers': ['file'],
#         'level': 'DEBUG',
#         'propagate': True,
#     },
# },
'loggers': {
    'django': {
        'handlers':['main_file'],
        'propagate': True,
        'level':'DEBUG',
    },
    'jira4': {    ##MYAPP
        'handlers': ['main_file'],
        'level': 'DEBUG',
    },
}

}

Per request, this is the code I wrote to use logs outside of the django system:

def set_logs(self, log_name = 'main_log', dir_name = 'logs', base_name = 'log'):

    print " .. . . SETTING LOGS!"
    this_logger = logging.getLogger(log_name)

    this_logger.setLevel(logging.DEBUG)  ## Basicallty all
    #logger.setLevel(logging.INFO)

    # create file handler which logs even debug messages
    fhe = logging.FileHandler('logs/tick_ERR.log')
    fhe.setLevel(logging.ERROR)

    fhw = logging.FileHandler(dir_name + '/' +base_name+ '_WARN.log', 'w')
    fhw.setLevel(logging.WARNING)

    fhc = logging.FileHandler(dir_name + '/' +base_name+ '_CRITICAL.log', 'w')
    fhc.setLevel(logging.CRITICAL)

    fhd = logging.FileHandler(dir_name + '/' +base_name+ '_DEBUG.log')
    fhd.setLevel(logging.DEBUG)

    fhi = logging.FileHandler(dir_name + '/' +base_name+ '_INFO.log')
    fhi.setLevel(logging.INFO)

    # create console handler with a higher log level
    # ch = logging.StreamHandler()
    # ch.setLevel(logging.ERROR)
    # create formatter and add it to the handlers
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s : %(message)s')
    fhe.setFormatter(formatter)
    fhw.setFormatter(formatter)
    fhc.setFormatter(formatter)
    fhd.setFormatter(formatter)
    fhi.setFormatter(formatter)

    # ch.setFormatter(formatter)
    # add the handlers to the logger
    this_logger.addHandler(fhe)
    this_logger.addHandler(fhw)
    this_logger.addHandler(fhc)
    this_logger.addHandler(fhd)
    this_logger.addHandler(fhi)

    return this_logger

And then later I call

    this_logger = logging.getLogger(log_name)

    print "Got logger: "
    pp.pprint(this_logger)


    this_logger.critical(test_flag + " __________________3__________ IS THI IN ALL ? ___________3_________")
    this_logger.critical(test_flag + " THIS IS CRITICAL!! ")

    this_logger.error(test_flag + " THIS IS A DARN ERROR!  ")

The interesting thing is, when I used almost identical code in a local file and called in via cmd line, it worked. What is it about the django environment that is not allowing me to write yet allows me to create these log files?

R Claven
  • 1,160
  • 2
  • 13
  • 27

1 Answers1

0

I tried your code and it looks like the logger name needs to be modified.

It is a good practice to put at the to of your file views.py

import logging
log = logging.getLogger(__name__)

in your case it would be in e.g. jira4.views.py

this_logger = logging.getLogger(__name__)   # __name__ will produce jira4.views

if your application is named jira4 then the log should start working correctly.

Also see Naming Python loggers

Community
  • 1
  • 1
fragles
  • 680
  • 6
  • 19