logging.debug() is printing something different than regular print()












2















I have written a code to find the factorial,i used the logging module.
after searching the site, i haven't found relative questions .
here is my code



def factorial(n):
logging.basicConfig(level=logging.DEBUG,format='%(asctime)s-%(levelname)s-%(message)s')
logging.debug('Start of program')
logging.debug('start of action (%s)'%(n))
total=1
for i in range(1,n+1):
#print(i)
total *=i
print(total)
logging.debug('i is '+str(i)+' total='+str(total))
logging.debug('End of factorial(%s) '%(n))
return total
def main():
print(factorial(5))
logging.debug('End of program')
if __name__=='__main__':
import logging
main()


the output of total is not 'what it should be' in the



logging.debug('i is '+str(i)+' total='+str(total))


here is what i get when i run the code



2018-11-21 09:34:56,213-DEBUG-Start of program
2018-11-21 09:34:56,213-DEBUG-start of action (5)
1
2018-11-21 09:34:56,213-DEBUG-i is 1 total=12

2018-11-21 09:34:56,213-DEBUG-i is 2 total=2
6
2018-11-21 09:34:56,213-DEBUG-i is 3 total=6
24
2018-11-21 09:34:56,213-DEBUG-i is 4 total=24
120
2018-11-21 09:34:56,213-DEBUG-i is 5 total=120
2018-11-21 09:34:56,213-DEBUG-End of factorial(5)
120
2018-11-21 09:34:56,213-DEBUG-End of program


The question seems long but it should be quite simple operations.










share|improve this question























  • So what is the problem?

    – Tobias Wilfert
    Nov 21 '18 at 7:45











  • It looks correct, except for the line where the IO is [presumably] interleaved so it [reportedly] results in 1<NONEWLINE>2<NEWLINE><NEWLINE> instead of 1<NEWLINE>2<NEWLINE>; same for the line with 120 near the bottom.

    – user2864740
    Nov 21 '18 at 7:46













  • @TobiasWilfert the total is not corret check the first line of output the total should be 1

    – wishmaster
    Nov 21 '18 at 7:56











  • @timmy the output is 1 the 2 just doesn't get a new line and moves up to the 1 for some reason resulting in a "12" and an empty next line.

    – Tobias Wilfert
    Nov 21 '18 at 8:03











  • @user2864740 the problem i am having that each time i run it,the total in the logging.debug(...) is very different.

    – wishmaster
    Nov 21 '18 at 8:04
















2















I have written a code to find the factorial,i used the logging module.
after searching the site, i haven't found relative questions .
here is my code



def factorial(n):
logging.basicConfig(level=logging.DEBUG,format='%(asctime)s-%(levelname)s-%(message)s')
logging.debug('Start of program')
logging.debug('start of action (%s)'%(n))
total=1
for i in range(1,n+1):
#print(i)
total *=i
print(total)
logging.debug('i is '+str(i)+' total='+str(total))
logging.debug('End of factorial(%s) '%(n))
return total
def main():
print(factorial(5))
logging.debug('End of program')
if __name__=='__main__':
import logging
main()


the output of total is not 'what it should be' in the



logging.debug('i is '+str(i)+' total='+str(total))


here is what i get when i run the code



2018-11-21 09:34:56,213-DEBUG-Start of program
2018-11-21 09:34:56,213-DEBUG-start of action (5)
1
2018-11-21 09:34:56,213-DEBUG-i is 1 total=12

2018-11-21 09:34:56,213-DEBUG-i is 2 total=2
6
2018-11-21 09:34:56,213-DEBUG-i is 3 total=6
24
2018-11-21 09:34:56,213-DEBUG-i is 4 total=24
120
2018-11-21 09:34:56,213-DEBUG-i is 5 total=120
2018-11-21 09:34:56,213-DEBUG-End of factorial(5)
120
2018-11-21 09:34:56,213-DEBUG-End of program


The question seems long but it should be quite simple operations.










share|improve this question























  • So what is the problem?

    – Tobias Wilfert
    Nov 21 '18 at 7:45











  • It looks correct, except for the line where the IO is [presumably] interleaved so it [reportedly] results in 1<NONEWLINE>2<NEWLINE><NEWLINE> instead of 1<NEWLINE>2<NEWLINE>; same for the line with 120 near the bottom.

    – user2864740
    Nov 21 '18 at 7:46













  • @TobiasWilfert the total is not corret check the first line of output the total should be 1

    – wishmaster
    Nov 21 '18 at 7:56











  • @timmy the output is 1 the 2 just doesn't get a new line and moves up to the 1 for some reason resulting in a "12" and an empty next line.

    – Tobias Wilfert
    Nov 21 '18 at 8:03











  • @user2864740 the problem i am having that each time i run it,the total in the logging.debug(...) is very different.

    – wishmaster
    Nov 21 '18 at 8:04














2












2








2








I have written a code to find the factorial,i used the logging module.
after searching the site, i haven't found relative questions .
here is my code



def factorial(n):
logging.basicConfig(level=logging.DEBUG,format='%(asctime)s-%(levelname)s-%(message)s')
logging.debug('Start of program')
logging.debug('start of action (%s)'%(n))
total=1
for i in range(1,n+1):
#print(i)
total *=i
print(total)
logging.debug('i is '+str(i)+' total='+str(total))
logging.debug('End of factorial(%s) '%(n))
return total
def main():
print(factorial(5))
logging.debug('End of program')
if __name__=='__main__':
import logging
main()


the output of total is not 'what it should be' in the



logging.debug('i is '+str(i)+' total='+str(total))


here is what i get when i run the code



2018-11-21 09:34:56,213-DEBUG-Start of program
2018-11-21 09:34:56,213-DEBUG-start of action (5)
1
2018-11-21 09:34:56,213-DEBUG-i is 1 total=12

2018-11-21 09:34:56,213-DEBUG-i is 2 total=2
6
2018-11-21 09:34:56,213-DEBUG-i is 3 total=6
24
2018-11-21 09:34:56,213-DEBUG-i is 4 total=24
120
2018-11-21 09:34:56,213-DEBUG-i is 5 total=120
2018-11-21 09:34:56,213-DEBUG-End of factorial(5)
120
2018-11-21 09:34:56,213-DEBUG-End of program


The question seems long but it should be quite simple operations.










share|improve this question














I have written a code to find the factorial,i used the logging module.
after searching the site, i haven't found relative questions .
here is my code



def factorial(n):
logging.basicConfig(level=logging.DEBUG,format='%(asctime)s-%(levelname)s-%(message)s')
logging.debug('Start of program')
logging.debug('start of action (%s)'%(n))
total=1
for i in range(1,n+1):
#print(i)
total *=i
print(total)
logging.debug('i is '+str(i)+' total='+str(total))
logging.debug('End of factorial(%s) '%(n))
return total
def main():
print(factorial(5))
logging.debug('End of program')
if __name__=='__main__':
import logging
main()


the output of total is not 'what it should be' in the



logging.debug('i is '+str(i)+' total='+str(total))


here is what i get when i run the code



2018-11-21 09:34:56,213-DEBUG-Start of program
2018-11-21 09:34:56,213-DEBUG-start of action (5)
1
2018-11-21 09:34:56,213-DEBUG-i is 1 total=12

2018-11-21 09:34:56,213-DEBUG-i is 2 total=2
6
2018-11-21 09:34:56,213-DEBUG-i is 3 total=6
24
2018-11-21 09:34:56,213-DEBUG-i is 4 total=24
120
2018-11-21 09:34:56,213-DEBUG-i is 5 total=120
2018-11-21 09:34:56,213-DEBUG-End of factorial(5)
120
2018-11-21 09:34:56,213-DEBUG-End of program


The question seems long but it should be quite simple operations.







python python-3.x






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 7:43









wishmasterwishmaster

1589




1589













  • So what is the problem?

    – Tobias Wilfert
    Nov 21 '18 at 7:45











  • It looks correct, except for the line where the IO is [presumably] interleaved so it [reportedly] results in 1<NONEWLINE>2<NEWLINE><NEWLINE> instead of 1<NEWLINE>2<NEWLINE>; same for the line with 120 near the bottom.

    – user2864740
    Nov 21 '18 at 7:46













  • @TobiasWilfert the total is not corret check the first line of output the total should be 1

    – wishmaster
    Nov 21 '18 at 7:56











  • @timmy the output is 1 the 2 just doesn't get a new line and moves up to the 1 for some reason resulting in a "12" and an empty next line.

    – Tobias Wilfert
    Nov 21 '18 at 8:03











  • @user2864740 the problem i am having that each time i run it,the total in the logging.debug(...) is very different.

    – wishmaster
    Nov 21 '18 at 8:04



















  • So what is the problem?

    – Tobias Wilfert
    Nov 21 '18 at 7:45











  • It looks correct, except for the line where the IO is [presumably] interleaved so it [reportedly] results in 1<NONEWLINE>2<NEWLINE><NEWLINE> instead of 1<NEWLINE>2<NEWLINE>; same for the line with 120 near the bottom.

    – user2864740
    Nov 21 '18 at 7:46













  • @TobiasWilfert the total is not corret check the first line of output the total should be 1

    – wishmaster
    Nov 21 '18 at 7:56











  • @timmy the output is 1 the 2 just doesn't get a new line and moves up to the 1 for some reason resulting in a "12" and an empty next line.

    – Tobias Wilfert
    Nov 21 '18 at 8:03











  • @user2864740 the problem i am having that each time i run it,the total in the logging.debug(...) is very different.

    – wishmaster
    Nov 21 '18 at 8:04

















So what is the problem?

– Tobias Wilfert
Nov 21 '18 at 7:45





So what is the problem?

– Tobias Wilfert
Nov 21 '18 at 7:45













It looks correct, except for the line where the IO is [presumably] interleaved so it [reportedly] results in 1<NONEWLINE>2<NEWLINE><NEWLINE> instead of 1<NEWLINE>2<NEWLINE>; same for the line with 120 near the bottom.

– user2864740
Nov 21 '18 at 7:46







It looks correct, except for the line where the IO is [presumably] interleaved so it [reportedly] results in 1<NONEWLINE>2<NEWLINE><NEWLINE> instead of 1<NEWLINE>2<NEWLINE>; same for the line with 120 near the bottom.

– user2864740
Nov 21 '18 at 7:46















@TobiasWilfert the total is not corret check the first line of output the total should be 1

– wishmaster
Nov 21 '18 at 7:56





@TobiasWilfert the total is not corret check the first line of output the total should be 1

– wishmaster
Nov 21 '18 at 7:56













@timmy the output is 1 the 2 just doesn't get a new line and moves up to the 1 for some reason resulting in a "12" and an empty next line.

– Tobias Wilfert
Nov 21 '18 at 8:03





@timmy the output is 1 the 2 just doesn't get a new line and moves up to the 1 for some reason resulting in a "12" and an empty next line.

– Tobias Wilfert
Nov 21 '18 at 8:03













@user2864740 the problem i am having that each time i run it,the total in the logging.debug(...) is very different.

– wishmaster
Nov 21 '18 at 8:04





@user2864740 the problem i am having that each time i run it,the total in the logging.debug(...) is very different.

– wishmaster
Nov 21 '18 at 8:04












1 Answer
1






active

oldest

votes


















0














This is a guess but it looks like you have a race condition within your prints. I actually thought logging takes case of this but to check you can try the following:
remove the print statement and run the program. If the program is correct remove the debug and return the print to check again.



The possible (apparently) race condition is that while the logging.debug didn't finish printing the whole line your code now did the print statement. thats why you have empty lines.






share|improve this answer
























  • it does fix it,total is now consistent and correct. i wonder why it was giving those results with the print() on line 9 of code.

    – wishmaster
    Nov 21 '18 at 9:14











  • @timmy It is a very classic race condition where the shared resource is the screen (printing stream). To learn more you can look here: en.wikipedia.org/wiki/Race_condition#Software If the answer helped you please accept it.

    – Eytan
    Nov 21 '18 at 10:34













  • A race condition implies concurrent work. print should only return after having finished all operations.

    – MisterMiyagi
    Nov 21 '18 at 10:41











  • @MisterMiyagi it looks like a duck and quaks like a duck. I suspect it is the debug print that is problamatic here. Don't know the implementation though so it is still a guess.

    – Eytan
    Nov 22 '18 at 16:35











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407338%2flogging-debug-is-printing-something-different-than-regular-print%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














This is a guess but it looks like you have a race condition within your prints. I actually thought logging takes case of this but to check you can try the following:
remove the print statement and run the program. If the program is correct remove the debug and return the print to check again.



The possible (apparently) race condition is that while the logging.debug didn't finish printing the whole line your code now did the print statement. thats why you have empty lines.






share|improve this answer
























  • it does fix it,total is now consistent and correct. i wonder why it was giving those results with the print() on line 9 of code.

    – wishmaster
    Nov 21 '18 at 9:14











  • @timmy It is a very classic race condition where the shared resource is the screen (printing stream). To learn more you can look here: en.wikipedia.org/wiki/Race_condition#Software If the answer helped you please accept it.

    – Eytan
    Nov 21 '18 at 10:34













  • A race condition implies concurrent work. print should only return after having finished all operations.

    – MisterMiyagi
    Nov 21 '18 at 10:41











  • @MisterMiyagi it looks like a duck and quaks like a duck. I suspect it is the debug print that is problamatic here. Don't know the implementation though so it is still a guess.

    – Eytan
    Nov 22 '18 at 16:35
















0














This is a guess but it looks like you have a race condition within your prints. I actually thought logging takes case of this but to check you can try the following:
remove the print statement and run the program. If the program is correct remove the debug and return the print to check again.



The possible (apparently) race condition is that while the logging.debug didn't finish printing the whole line your code now did the print statement. thats why you have empty lines.






share|improve this answer
























  • it does fix it,total is now consistent and correct. i wonder why it was giving those results with the print() on line 9 of code.

    – wishmaster
    Nov 21 '18 at 9:14











  • @timmy It is a very classic race condition where the shared resource is the screen (printing stream). To learn more you can look here: en.wikipedia.org/wiki/Race_condition#Software If the answer helped you please accept it.

    – Eytan
    Nov 21 '18 at 10:34













  • A race condition implies concurrent work. print should only return after having finished all operations.

    – MisterMiyagi
    Nov 21 '18 at 10:41











  • @MisterMiyagi it looks like a duck and quaks like a duck. I suspect it is the debug print that is problamatic here. Don't know the implementation though so it is still a guess.

    – Eytan
    Nov 22 '18 at 16:35














0












0








0







This is a guess but it looks like you have a race condition within your prints. I actually thought logging takes case of this but to check you can try the following:
remove the print statement and run the program. If the program is correct remove the debug and return the print to check again.



The possible (apparently) race condition is that while the logging.debug didn't finish printing the whole line your code now did the print statement. thats why you have empty lines.






share|improve this answer













This is a guess but it looks like you have a race condition within your prints. I actually thought logging takes case of this but to check you can try the following:
remove the print statement and run the program. If the program is correct remove the debug and return the print to check again.



The possible (apparently) race condition is that while the logging.debug didn't finish printing the whole line your code now did the print statement. thats why you have empty lines.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 8:20









EytanEytan

301113




301113













  • it does fix it,total is now consistent and correct. i wonder why it was giving those results with the print() on line 9 of code.

    – wishmaster
    Nov 21 '18 at 9:14











  • @timmy It is a very classic race condition where the shared resource is the screen (printing stream). To learn more you can look here: en.wikipedia.org/wiki/Race_condition#Software If the answer helped you please accept it.

    – Eytan
    Nov 21 '18 at 10:34













  • A race condition implies concurrent work. print should only return after having finished all operations.

    – MisterMiyagi
    Nov 21 '18 at 10:41











  • @MisterMiyagi it looks like a duck and quaks like a duck. I suspect it is the debug print that is problamatic here. Don't know the implementation though so it is still a guess.

    – Eytan
    Nov 22 '18 at 16:35



















  • it does fix it,total is now consistent and correct. i wonder why it was giving those results with the print() on line 9 of code.

    – wishmaster
    Nov 21 '18 at 9:14











  • @timmy It is a very classic race condition where the shared resource is the screen (printing stream). To learn more you can look here: en.wikipedia.org/wiki/Race_condition#Software If the answer helped you please accept it.

    – Eytan
    Nov 21 '18 at 10:34













  • A race condition implies concurrent work. print should only return after having finished all operations.

    – MisterMiyagi
    Nov 21 '18 at 10:41











  • @MisterMiyagi it looks like a duck and quaks like a duck. I suspect it is the debug print that is problamatic here. Don't know the implementation though so it is still a guess.

    – Eytan
    Nov 22 '18 at 16:35

















it does fix it,total is now consistent and correct. i wonder why it was giving those results with the print() on line 9 of code.

– wishmaster
Nov 21 '18 at 9:14





it does fix it,total is now consistent and correct. i wonder why it was giving those results with the print() on line 9 of code.

– wishmaster
Nov 21 '18 at 9:14













@timmy It is a very classic race condition where the shared resource is the screen (printing stream). To learn more you can look here: en.wikipedia.org/wiki/Race_condition#Software If the answer helped you please accept it.

– Eytan
Nov 21 '18 at 10:34







@timmy It is a very classic race condition where the shared resource is the screen (printing stream). To learn more you can look here: en.wikipedia.org/wiki/Race_condition#Software If the answer helped you please accept it.

– Eytan
Nov 21 '18 at 10:34















A race condition implies concurrent work. print should only return after having finished all operations.

– MisterMiyagi
Nov 21 '18 at 10:41





A race condition implies concurrent work. print should only return after having finished all operations.

– MisterMiyagi
Nov 21 '18 at 10:41













@MisterMiyagi it looks like a duck and quaks like a duck. I suspect it is the debug print that is problamatic here. Don't know the implementation though so it is still a guess.

– Eytan
Nov 22 '18 at 16:35





@MisterMiyagi it looks like a duck and quaks like a duck. I suspect it is the debug print that is problamatic here. Don't know the implementation though so it is still a guess.

– Eytan
Nov 22 '18 at 16:35




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407338%2flogging-debug-is-printing-something-different-than-regular-print%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?