Reputation: 47316
Okay. I have completed my first python program.It has around 1000 lines of code.
During development I placed plenty of print
statements before running a command using os.system()
say something like,
print "running command",cmd
os.system(cmd)
Now I have completed the program. I thought about commenting them but redirecting all these unnecessary print (i can't remove all print
statements - since some provide useful info for user) into a log file will be more useful? Any tricks or tips.
Upvotes: 53
Views: 159429
Reputation: 502
def log(txt):
f = open(__file__ + '.log', "a")
f.write(txt + '\r\n')
f.close()
Usage:
log('Hello World!')
Example:
python3 helloworld.py
Will append to file ./helloworld.py.log
. If file doesn't exist, it will create it.
Upvotes: 0
Reputation: 97
there are many way to write output into the '.log' file
Logging is a means of tracking events it happen when some file runs. Is also indicate that certain events have occurred.
import logging
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
logging.debug('This is debug message')
logging.info('This is information message')
logging.warning('This is warning message')
logging.error('This is warning message')
another method to use to reduce all that thing sinple what ever you print to the console that all will be saved to the ''log'' file
python abc.py > abc.log
by using this method you can write everything to the log file
Upvotes: 3
Reputation: 1300
Just a note about append vs write mode. Change filemode to "w" if you would like it to replace log file. I also had to comment out the stream. Then using logging.info() was outputting to file specified.
if __name__ == '__main__':
LOG_FORMAT = '%(asctime)s:%(levelname)s ==> %(message)s'
logging.basicConfig(
level=logging.INFO,
filename="logfile",
filemode="w",
format=LOG_FORMAT
#stream=sys.stdout
)
Upvotes: 0
Reputation: 61
You can create a log file and prepare it for writing. Then create a function:
def write_log(*args):
line = ' '.join([str(a) for a in args])
log_file.write(line+'\n')
print(line)
and then replace your print() function name with write_log()
Upvotes: 6
Reputation: 3608
Python lets you capture and assign sys.stdout - as mentioned - to do this:
import sys
old_stdout = sys.stdout
log_file = open("message.log","w")
sys.stdout = log_file
print "this will be written to message.log"
sys.stdout = old_stdout
log_file.close()
Upvotes: 76
Reputation:
You should take a look at python logging module
EDIT: Sample code:
import logging
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG, filename="logfile", filemode="a+",
format="%(asctime)-15s %(levelname)-8s %(message)s")
logging.info("hello")
Produce a file named "logfile" with content:
2012-10-18 06:40:03,582 INFO hello
Upvotes: 66
Reputation: 76793
Next time, you'll be happier if instead of using print
statements at all you use the logging
module from the start. It provides the control you want and you can have it write to stdout while that's still where you want it.
Many people here have suggested redirecting stdout. This is an ugly solution. It mutates a global and—what's worse—it mutates it for this one module's use. I would sooner make a regex that changes all print foo
to print >>my_file, foo
and set my_file
to either stdout or an actual file of my choosing.
sys.stdout
for the process.os.system
is virtually always inferior to using the subprocess
module. The latter needn't invoke the shell, doesn't pass signals in a way that usually is unwanted, and can be used in a non-blocking manner.
Upvotes: 13
Reputation: 88855
You can redirect replace sys.stdout with any object which has same interface as sys.stdout, in that object's write you can print to terminal and to file too. e.g. see this recipe http://code.activestate.com/recipes/119404-print-hook/
Upvotes: 2
Reputation: 7065
A simple way to redirect stdout and stderr using the logging module is here: How do I duplicate sys.stdout to a log file in python?
Upvotes: 2
Reputation: 799520
Putting your own file-like in sys.stdout
will let you capture the text output via print
.
Upvotes: 1