Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 27 characters in body
Source Link
Kenan Banks
  • 212.8k
  • 36
  • 161
  • 176

The print statement will call the write() method of any object you assign to sys.stdout.

I would spin up a small class to write to two places at once...

import sys class Logger(object): def __init__(self): self.terminal = sys.stdout self.log = open("log.dat", "a") def write(self, message): self.terminal.write(message) self.log.write(message) sys.stdout = Logger() 

Now the print statement will both echo to the screen and append to your log file:

# prints "1 2" to <stdout> AND log.dat print "%d %d" % (1,2) 

This is obviously quick-and-dirty. Some notes:

  • You probably ought to parametize the log filename.
  • You should probably revert sys.stdout to if<stdout> if you won't be logging for the duration of the program.
  • You may want the ability to write to multiple log files at once, or handle different log levels, etc.

These are all straightforward enough that I'm comfortable leaving them as exercises for the reader. The key insight here is that print just calls a "file-like object" that's assigned to sys.stdout.

The print statement will call the write() method of any object you assign to sys.stdout.

I would spin up a small class to write to two places at once...

import sys class Logger(object): def __init__(self): self.terminal = sys.stdout self.log = open("log.dat", "a") def write(self, message): self.terminal.write(message) self.log.write(message) sys.stdout = Logger() 

Now the print statement will both echo to the screen and append to your log file:

# prints "1 2" to <stdout> AND log.dat print "%d %d" % (1,2) 

This is obviously quick-and-dirty. Some notes:

  • You probably ought to parametize the log filename.
  • You should probably revert sys.stdout to if you won't be logging for the duration of the program.
  • You may want the ability to write to multiple log files at once, or handle different log levels, etc.

These are all straightforward enough that I'm comfortable leaving them as exercises for the reader. The key insight here is that print just calls a "file-like object" that's assigned to sys.stdout.

The print statement will call the write() method of any object you assign to sys.stdout.

I would spin up a small class to write to two places at once...

import sys class Logger(object): def __init__(self): self.terminal = sys.stdout self.log = open("log.dat", "a") def write(self, message): self.terminal.write(message) self.log.write(message) sys.stdout = Logger() 

Now the print statement will both echo to the screen and append to your log file:

# prints "1 2" to <stdout> AND log.dat print "%d %d" % (1,2) 

This is obviously quick-and-dirty. Some notes:

  • You probably ought to parametize the log filename.
  • You should probably revert sys.stdout to <stdout> if you won't be logging for the duration of the program.
  • You may want the ability to write to multiple log files at once, or handle different log levels, etc.

These are all straightforward enough that I'm comfortable leaving them as exercises for the reader. The key insight here is that print just calls a "file-like object" that's assigned to sys.stdout.

added 38 characters in body
Source Link
Kenan Banks
  • 212.8k
  • 36
  • 161
  • 176

The print statement will call the write() method of any object you assign to sys.stdout. IThe print statement will call the write() method of any object you assign to sys.stdout.

I would spin up a small class to write to two places at once...

import sys class Logger(object): def __init__(self): self.terminal = sys.stdout self.log = open("log.dat", "a") def write(self, message): self.terminal.write(message) self.log.write(message) sys.stdout = Logger() 

Now the print statement will both echo to the screen and be appended to your log file:Now the print statement will both echo to the screen and append to your log file:

# prints "1 2" to <stdout> AND log.dat print "%d %d" % (1,2) 

This is obviously quick-and-dirty. Some notes:

  • You probably ought to parametize the log filename.
  • You should probably revert sys.stdout to if you won't be logging for the duration of the program.
  • You may want the ability to write to multiple log files at once, or handle different log levels, etc.

These are all straightforward enough that I'm comfortable leaving them as exercises for the reader. The key insight here is that print just calls a "file-like object" that's assigned to sys.stdout.

The print statement will call the write() method of any object you assign to sys.stdout. I would spin up a small class to write to two places at once...

import sys class Logger(object): def __init__(self): self.terminal = sys.stdout self.log = open("log.dat", "a") def write(self, message): self.terminal.write(message) self.log.write(message) sys.stdout = Logger() 

Now the print statement will both echo to the screen and be appended to your log file:

# prints "1 2" to <stdout> AND log.dat print "%d %d" % (1,2) 

This is obviously quick-and-dirty. Some notes:

  • You probably ought to parametize the log filename.
  • You should probably revert sys.stdout to if you won't be logging for the duration of the program.
  • You may want the ability to write to multiple log files at once, or handle different log levels, etc.

These are all straightforward enough that I'm comfortable leaving them as exercises for the reader. The key insight here is that print just calls a "file-like object" that's assigned to sys.stdout.

The print statement will call the write() method of any object you assign to sys.stdout.

I would spin up a small class to write to two places at once...

import sys class Logger(object): def __init__(self): self.terminal = sys.stdout self.log = open("log.dat", "a") def write(self, message): self.terminal.write(message) self.log.write(message) sys.stdout = Logger() 

Now the print statement will both echo to the screen and append to your log file:

# prints "1 2" to <stdout> AND log.dat print "%d %d" % (1,2) 

This is obviously quick-and-dirty. Some notes:

  • You probably ought to parametize the log filename.
  • You should probably revert sys.stdout to if you won't be logging for the duration of the program.
  • You may want the ability to write to multiple log files at once, or handle different log levels, etc.

These are all straightforward enough that I'm comfortable leaving them as exercises for the reader. The key insight here is that print just calls a "file-like object" that's assigned to sys.stdout.

added 18 characters in body
Source Link
Kenan Banks
  • 212.8k
  • 36
  • 161
  • 176

The print statement will call the write() method of any object you assign to sys.stdout. I would spin up a small class to write to two places at once...

import sys class Logger(object): def __init__(self): self.terminal = sys.stdout self.log = open("log.dat", "w""a") def write(self, message): self.terminal.write(message) self.log.write(message) sys.stdout = Logger() 

Now calls to printthe print statement will both echo to the screen and be appended to your log file:

# prints "1 2" to <stdout> AND log.dat print "%d %d" % (1,2) 

This is obviously quick-and-dirty. Some notes:

  • You probably ought to parametize the log filename.
  • You should probably revert sys.stdout to if you won't be logging for the duration of the program.
  • You may want the ability to write to multiple log files at once, or handle different log levels, etc.

These are all straightforward enough that I'm comfortable leaving them as exercises for the reader. The key insight here is that print just calls a "file-like object" that's assigned to sys.stdout.

The print statement will call the write() method of any object you assign to sys.stdout. I would spin up a small class to write to two places at once...

import sys class Logger(object): def __init__(self): self.terminal = sys.stdout self.log = open("log.dat", "w") def write(self, message): self.terminal.write(message) self.log.write(message) sys.stdout = Logger() 

Now calls to print will both echo to the screen and be appended to your log file:

# prints "1 2" to <stdout> AND log.dat print "%d %d" % (1,2) 

The print statement will call the write() method of any object you assign to sys.stdout. I would spin up a small class to write to two places at once...

import sys class Logger(object): def __init__(self): self.terminal = sys.stdout self.log = open("log.dat", "a") def write(self, message): self.terminal.write(message) self.log.write(message) sys.stdout = Logger() 

Now the print statement will both echo to the screen and be appended to your log file:

# prints "1 2" to <stdout> AND log.dat print "%d %d" % (1,2) 

This is obviously quick-and-dirty. Some notes:

  • You probably ought to parametize the log filename.
  • You should probably revert sys.stdout to if you won't be logging for the duration of the program.
  • You may want the ability to write to multiple log files at once, or handle different log levels, etc.

These are all straightforward enough that I'm comfortable leaving them as exercises for the reader. The key insight here is that print just calls a "file-like object" that's assigned to sys.stdout.

Source Link
Kenan Banks
  • 212.8k
  • 36
  • 161
  • 176
Loading