1
votes

I'm sorry to ask about this

I'm trying to set up log to be able to test my code; Django pass variable into template

and I was following the example in https://docs.djangoproject.com/en/2.1/topics/logging/

Here the code they use

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
    'file': {
        'level': 'DEBUG',
        'class': 'logging.FileHandler',
        'filename': r'C:\Users\Sk\Desktop\project\FrounterWeb\FrounterWeb\logs',
    },
},
'loggers': {
    'django': {
        'handlers': ['file'],
        'level': 'DEBUG',
        'propagate': True,
    },
},
}

But i run into this problem, copy and paste and answer i get is this;

Microsoft Windows [Version 10.0.10586]
(c) 2015 Microsoft Corporation. All rights reserved.

C:\Users\Sk\Desktop\project\FrounterWeb>py runserver_plus
(null): can't open file 'runserver_plus': [Errno 2] No such file or directory

C:\Users\Sk\Desktop\project\FrounterWeb>py manage.py  runserver_plus
Traceback (most recent call last):
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\config.py", line 555, in configure
    handler = self.configure_handler(handlers[name])
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\config.py", line 728, in configure_handler
    result = factory(**kwargs)
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\__init__.py", line 1041, in __init__
    StreamHandler.__init__(self, self._open())
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\__init__.py", line 1070, in _open
    return open(self.baseFilename, self.mode, encoding=self.encoding)
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Sk\\Desktop\\project\\FrounterWeb\\FrounterWeb\\logs'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 357, in execute
    django.setup()
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\site-packages\django\__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\log.py", line 76, in configure_logging
    logging_config_func(logging_settings)
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\config.py", line 792, in dictConfig
    dictConfigClass(config).configure()
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\config.py", line 563, in configure
    '%r' % name) from e
ValueError: Unable to configure handler 'files'

I believe this problem with the dictionary, so I remove the 'File':{} & 'django':{} dictionary and i receive another problems

the new code:

    LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
    'file': {
        'level': 'DEBUG',
        'class': 'logging.FileHandler',
        'filename': r'C:\Users\Sk\Desktop\project\FrounterWeb\FrounterWeb\logs',
    },
},
        'loggers': {
        'django': {
        'handlers': ['file'],
        'level': 'DEBUG',
        'propagate': True,
         },
         },
         }

and this is problems;

Microsoft Windows [Version 10.0.10586]
(c) 2015 Microsoft Corporation. All rights reserved.

C:\Users\Sk\Desktop\project\FrounterWeb>py runserver_plus
(null): can't open file 'runserver_plus': [Errno 2] No such file or directory

C:\Users\Sk\Desktop\project\FrounterWeb>py manage.py  runserver_plus
Traceback (most recent call last):
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\config.py", line 555, in configure
    handler = self.configure_handler(handlers[name])
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\config.py", line 728, in configure_handler
    result = factory(**kwargs)
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\__init__.py", line 1041, in __init__
    StreamHandler.__init__(self, self._open())
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\__init__.py", line 1070, in _open
    return open(self.baseFilename, self.mode, encoding=self.encoding)
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Sk\\Desktop\\project\\FrounterWeb\\FrounterWeb\\logs'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 357, in execute
    django.setup()
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\site-packages\django\__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\log.py", line 76, in configure_logging
    logging_config_func(logging_settings)
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\config.py", line 792, in dictConfig
    dictConfigClass(config).configure()
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\config.py", line 563, in configure
    '%r' % name) from e
ValueError: Unable to configure handler 'files'

C:\Users\Sk\Desktop\project\FrounterWeb>py manage.py  runserver_plus
Traceback (most recent call last):
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\config.py", line 555, in configure
    handler = self.configure_handler(handlers[name])
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\config.py", line 688, in configure_handler
    config_copy = dict(config)  # for restoring in case of error
ValueError: dictionary update sequence element #0 has length 1; 2 is required

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 357, in execute
    django.setup()
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\site-packages\django\__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\log.py", line 76, in configure_logging
    logging_config_func(logging_settings)
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\config.py", line 792, in dictConfig
    dictConfigClass(config).configure()
  File "C:\Users\Sk\AppData\Local\Programs\Python\Python37\lib\logging\config.py", line 563, in configure
    '%r' % name) from e
ValueError: Unable to configure handler 'class'

This problem means it can read it own library

I'm sorry trouble you, but pls help me, i really don't know what to since is Django is tell is having problem with own build in library(i think)

2
Did you replace /path/to/django/debug.log with an actual, valid path? If not, you will run into these issues. Replace it with a path such as r'C:\Users\Sk\Desktop\django.log' or somewhere else (make sure the directory exists first, and that you have permissions to create files there). The desktop, Documents and Downloads folders are good candidates. - Burhan Khalid
/path/to/django/debug.log; no because I don't know what to replace as I try to use my a folder but it didn't work. Afterwards don't really know what to do, I'm not sure which to use. - Rookies DJ
Create a directory in the same folder you have settings.py called logs, then replace /path/to/django/debug.log with the full path to the directory, for example r'C:\Users\Sk\Desktop\FrounterWeb\logs\django.log' - Burhan Khalid
burhan khalid, thank you point me the way but it did't work, had this error intsead; SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape I apologies for this problem - Rookies DJ
burhan khalid; I found the answer to syntaxerror, I didn't add 'r' in front of my path. I feel so silly - Rookies DJ

2 Answers

1
votes

You need to add formatter to your LOGGING:

LOGGING = {
'formatters': {
    'verbose': {
         'format': '{levelname} {message}',
         'style': '{',
    },
},
# Rest of the Logging Config
}
1
votes

The answer was very simple, like Burhan Khalid said the logs path to the site not tied a path for the file to logs to.

All i did was add this to my handler

'filename': 'zigview\logging_files\debug.log',

and is working

I feel so embarrass, to make such a simple mistakes