1

I'm trying to change all my overused prints, to logging.* mainly because i wanted to have a logfile.

logger = logging.getLogger(__name__) formatter = logging.Formatter("{levelname}:{name}:{message}", style="{") file_handler = logging.FileHandler("log.txt") file_handler.setFormatter(formatter) file_handler.setLevel(logging.DEBUG) logger.addHandler(file_handler) console_handler = logging.StreamHandler() console_handler.setLevel(logging.INFO) console_handler.setFormatter(formatter) logger.addHandler(console_handler) logger.setLevel(logging.DEBUG) 

Thats the code i have for now, and it's printing to the console correctly but not outputting anything at all to the file. The file even gets created randomly, but but when it does, it's always empty.

I'm using logger.debug() and logger.info(), not the logging.* ones.

Update:

Just copying that same code to an empty script, and importing works as expected. Weird.

Update 2:

I found a question that has the same problem as me, he says he found a solution, but he doesn't share it :(

Update 3:

This is my source. The code gets called by uTorrent everytime the status of a torrent changes. This is the command i have setup in uTorrent:

py C:\Users\Gcq\Downloads\Torrent\torrent.py -n %N -d %D -f %F -p %P -c %S -s %M 

py because i have shebang at the top of my script that tells py to use python3. I have python2.7 installed too.
And the the commandline otions are the info that uTorrent exposes.

5
  • what python version/os? Commented Dec 1, 2013 at 1:28
  • @IonutHulub Python 3.3.2 [MSC v.1600 32 bit (Intel)] on win32 Commented Dec 1, 2013 at 1:32
  • Welp, i think i'm not going to get an answer from this... Commented Dec 1, 2013 at 8:29
  • 1
    Random idea: have you check write permission in the folder? Commented Dec 1, 2013 at 11:20
  • @MaximeLorant i think its okay, as i said, the file randomly gets created, so write permission should be ok. Commented Dec 1, 2013 at 16:46

2 Answers 2

1

I'm not able to reproduce the problem (I have Python 3.3.0 installed rather than 3.3.2, but that shouldn't be relevant. See this screenshot:

enter image description here

The behaviour seems as expected, so I would look more closely at your Python installation/configuration, or any other part of your actual code which you haven't shown above.

Sign up to request clarification or add additional context in comments.

3 Comments

does the file log.txt get created?
Yes - if you look at the screenshot, you'll see where I do type log.txt and that its contents are printed and are as expected.
oh yeah, i didn't watch carefully.
0

I thought 'okay, i didn't told python where to put my output file' and indeed, it was on uTorrent base folder.

With all the expected output, so everything is working perfect now.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.