0

I have the following code with a function to write into a log file, for some reason it doesn't work :-|

#!/usr/nin/env python

log_buffer = ""

def print_log(msg):
  global log_buffer
  log_buffer += msg + "\n"
  return
  with open("logging.log","at") as log:
    log.write(msg+"\n")

print_log("Test")

Any idea what am I missing here?

1
  • 2
    why you return before writing to file ? Commented Jan 27, 2014 at 14:26

1 Answer 1

3

Any idea what am I missing here?

your return statement makes the print_log() function leave before you ever write to the file

but instead of reinventing the wheel, you should instead use the logging module!

look at a tutorial and implement it, so that way you'll be compatible with libraries you may include in your project, or keep a standard way to use logs if your project is a library. And it provides ways to filter logs to different destinations and so many more powerful features!

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

2 Comments

yeah, seems my return was the problem and the write never happened.
thanks for the tip with logging module and tutorial, I will follow it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.