Posts: 12
Threads: 4
Joined: Aug 2018
Aug-25-2018, 03:28 PM
(This post was last modified: Aug-25-2018, 03:29 PM by Sanlus.)
Used video from automate the boring stuff with python course, and copied the course program onto my own computer and try to ran it. Didn't work as the video showed.
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %message)s')
logging.debug('Start of program')
def factorial(n):
logging.debug('Start of factorial(%s)' % (n))
total = 1
for i in range (n+1):
total *= i
logging.debug('i is %s, total is %s' % (i, total)
logging.debug('Return value is %s' % (total))
return total
print (factorial(5))
logging.debug('End of program') Its supposed to give out some debug info, but instead it gives invalid syntax on:
logging.debug('Return value is %s' % (total)) right on the word logging
I have absolutely no idea about logging, I just want to know what's happening and if the video is wrong or I did something incorrect or just anything that would let me know how to use logging.
Posts: 8,159
Threads: 160
Joined: Sep 2016
Aug-25-2018, 03:58 PM
(This post was last modified: Aug-25-2018, 03:58 PM by buran.)
Please, post the entire traceback that you get. We need to see the whole thing. Do not just give us the last line.
Take a time to read What to include in a post
Also, get use more descriptive thread titles
EDIT: You have missing closing parenthesis on line#11
Posts: 12
Threads: 4
Joined: Aug 2018
(Aug-25-2018, 03:58 PM)buran Wrote: Please, post the entire traceback that you get. We need to see the whole thing. Do not just give us the last line.
Take a time to read What to include in a post
Also, get use more descriptive thread titles
EDIT: You have missing closing parenthesis on line#11
i just got a box popped up telling me bout syntax error, wasnt like a big error message. after putting the closing parenthesis i got the following error:
Error: Traceback (most recent call last):
File "C:/Users/MSI/AppData/Local/Programs/Python/Python37-32/Automate the Boring Stuff with Python Course/logging.py", line 1, in <module>
import logging
File "C:/Users/MSI/AppData/Local/Programs/Python/Python37-32/Automate the Boring Stuff with Python Course\logging.py", line 2, in <module>
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %message)s')
AttributeError: module 'logging' has no attribute 'basicConfig'
Posts: 8,159
Threads: 160
Joined: Sep 2016
Aug-25-2018, 04:15 PM
(This post was last modified: Aug-25-2018, 04:15 PM by buran.)
rename your file to something different. At the moment it's name is logging.py and when you import logging it in fact import itself (i.e. you overshadow the built-in module with the same name)
Posts: 12
Threads: 4
Joined: Aug 2018
(Aug-25-2018, 04:15 PM)buran Wrote: rename your file to something different. At the moment it's name is logging.py and when you import logging it in fact import itself (i.e. you overshadow the built-in module with the same name) so i did that, and now it starts giving me even crazier messages
Error: --- Logging error ---
Traceback (most recent call last):
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 983, in emit
msg = self.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 829, in format
return fmt.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 572, in format
s = self.formatMessage(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 541, in formatMessage
return self._style.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 384, in format
return self._fmt % record.__dict__
TypeError: not enough arguments for format string
Call stack:
File "<string>", line 1, in <module>
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 144, in main
ret = method(*args, **kwargs)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 474, in runcode
exec(code, self.locals)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 4, in <module>
logging.debug('Start of program')
Message: 'Start of program'
Arguments: ()
--- Logging error ---
Traceback (most recent call last):
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 983, in emit
msg = self.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 829, in format
return fmt.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 572, in format
s = self.formatMessage(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 541, in formatMessage
return self._style.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 384, in format
return self._fmt % record.__dict__
TypeError: not enough arguments for format string
Call stack:
File "<string>", line 1, in <module>
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 144, in main
ret = method(*args, **kwargs)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 474, in runcode
exec(code, self.locals)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 16, in <module>
print (factorial(5))
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 7, in factorial
logging.debug('Start of factorial(%s)' % (n))
Message: 'Start of factorial(5)'
Arguments: ()
--- Logging error ---
Traceback (most recent call last):
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 983, in emit
msg = self.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 829, in format
return fmt.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 572, in format
s = self.formatMessage(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 541, in formatMessage
return self._style.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 384, in format
return self._fmt % record.__dict__
TypeError: not enough arguments for format string
Call stack:
File "<string>", line 1, in <module>
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 144, in main
ret = method(*args, **kwargs)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 474, in runcode
exec(code, self.locals)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 16, in <module>
print (factorial(5))
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 11, in factorial
logging.debug('i is %s, total is %s' % (i, total))
Message: 'i is 0, total is 0'
Arguments: ()
--- Logging error ---
Traceback (most recent call last):
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 983, in emit
msg = self.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 829, in format
return fmt.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 572, in format
s = self.formatMessage(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 541, in formatMessage
return self._style.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 384, in format
return self._fmt % record.__dict__
TypeError: not enough arguments for format string
Call stack:
File "<string>", line 1, in <module>
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 144, in main
ret = method(*args, **kwargs)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 474, in runcode
exec(code, self.locals)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 16, in <module>
print (factorial(5))
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 11, in factorial
logging.debug('i is %s, total is %s' % (i, total))
Message: 'i is 1, total is 0'
Arguments: ()
--- Logging error ---
Traceback (most recent call last):
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 983, in emit
msg = self.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 829, in format
return fmt.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 572, in format
s = self.formatMessage(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 541, in formatMessage
return self._style.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 384, in format
return self._fmt % record.__dict__
TypeError: not enough arguments for format string
Call stack:
File "<string>", line 1, in <module>
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 144, in main
ret = method(*args, **kwargs)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 474, in runcode
exec(code, self.locals)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 16, in <module>
print (factorial(5))
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 11, in factorial
logging.debug('i is %s, total is %s' % (i, total))
Message: 'i is 2, total is 0'
Arguments: ()
--- Logging error ---
Traceback (most recent call last):
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 983, in emit
msg = self.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 829, in format
return fmt.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 572, in format
s = self.formatMessage(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 541, in formatMessage
return self._style.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 384, in format
return self._fmt % record.__dict__
TypeError: not enough arguments for format string
Call stack:
File "<string>", line 1, in <module>
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 144, in main
ret = method(*args, **kwargs)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 474, in runcode
exec(code, self.locals)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 16, in <module>
print (factorial(5))
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 11, in factorial
logging.debug('i is %s, total is %s' % (i, total))
Message: 'i is 3, total is 0'
Arguments: ()
--- Logging error ---
Traceback (most recent call last):
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 983, in emit
msg = self.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 829, in format
return fmt.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 572, in format
s = self.formatMessage(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 541, in formatMessage
return self._style.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 384, in format
return self._fmt % record.__dict__
TypeError: not enough arguments for format string
Call stack:
File "<string>", line 1, in <module>
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 144, in main
ret = method(*args, **kwargs)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 474, in runcode
exec(code, self.locals)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 16, in <module>
print (factorial(5))
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 11, in factorial
logging.debug('i is %s, total is %s' % (i, total))
Message: 'i is 4, total is 0'
Arguments: ()
--- Logging error ---
Traceback (most recent call last):
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 983, in emit
msg = self.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 829, in format
return fmt.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 572, in format
s = self.formatMessage(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 541, in formatMessage
return self._style.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 384, in format
return self._fmt % record.__dict__
TypeError: not enough arguments for format string
Call stack:
File "<string>", line 1, in <module>
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 144, in main
ret = method(*args, **kwargs)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 474, in runcode
exec(code, self.locals)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 16, in <module>
print (factorial(5))
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 11, in factorial
logging.debug('i is %s, total is %s' % (i, total))
Message: 'i is 5, total is 0'
Arguments: ()
--- Logging error ---
Traceback (most recent call last):
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 983, in emit
msg = self.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 829, in format
return fmt.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 572, in format
s = self.formatMessage(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 541, in formatMessage
return self._style.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 384, in format
return self._fmt % record.__dict__
TypeError: not enough arguments for format string
Call stack:
File "<string>", line 1, in <module>
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 144, in main
ret = method(*args, **kwargs)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 474, in runcode
exec(code, self.locals)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 16, in <module>
print (factorial(5))
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 13, in factorial
logging.debug('Return value is %s' % (total))
Message: 'Return value is 0'
Arguments: ()
0
--- Logging error ---
Traceback (most recent call last):
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 983, in emit
msg = self.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 829, in format
return fmt.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 572, in format
s = self.formatMessage(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 541, in formatMessage
return self._style.format(record)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 384, in format
return self._fmt % record.__dict__
TypeError: not enough arguments for format string
Call stack:
File "<string>", line 1, in <module>
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 144, in main
ret = method(*args, **kwargs)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\lib\idlelib\run.py", line 474, in runcode
exec(code, self.locals)
File "C:\Users\MSI\AppData\Local\Programs\Python\Python37-32\Automate the Boring Stuff with Python Course\loggingstuff.py", line 18, in <module>
logging.debug('End of program')
Message: 'End of program'
Arguments: ()
idk if this is a successful run of logging or not. doesnt look the same as video does.
Posts: 8,159
Threads: 160
Joined: Sep 2016
another missing parenthesis, this time on line#2
it should be
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
Posts: 12
Threads: 4
Joined: Aug 2018
(Aug-25-2018, 04:45 PM)buran Wrote: another missing parenthesis, this time on line#2 it should be logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') thx! it finally worked
Posts: 3
Threads: 0
Joined: Aug 2018
First of all you don't need % sign for formatting, logging allows you to do so. You simply write:
logging.debug('Return value is %s', (total)) or, as another option you could write comma so it is tuple.
logging.debug('Return value is %s' % (total,))
Posts: 7,316
Threads: 123
Joined: Sep 2016
Aug-26-2018, 12:28 PM
(This post was last modified: Aug-26-2018, 12:30 PM by snippsat.)
(Aug-26-2018, 02:08 AM)leotrubach Wrote: First of all you don't need % sign for formatting, logging allows you to do so. You simply write: There is no need to use the very old string formatting %s .
As he use 3.7 then of course use f-string .
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logging.debug('Start of program')
def factorial(n):
logging.debug(f'Start of factorial(n)')
total = 1
for i in range (n+1):
total *= i
logging.debug(f'i is {i}, total is {total}')
logging.debug('Return value is {total}')
return total
print (factorial(5))
logging.debug('End of program')
|