[Twisted-Python] Logging to both console and file (using twisted logging API)?
![](https://secure.gravatar.com/avatar/bc0ff9a78eb210aad0b1c362d7b0889b.jpg?s=120&d=mm&r=g)
Hi, I wanted to log in a file and keep the console also going with it. I am using log.startLogging(open(logname, 'w')) whatever documentation I could find about this API, it suggested me to pass setStdout=False(in order to keep stdout away from putting into log) somewhere in the startlogging function. Something like this I tried: log.startLogging(sys.stdout,setStdout=False) Doesnt help in solving the problem of getting both things: Requirement: log.msg("messageforlog") -> goes to log print "message for console" -> goes to console Could anyone give some ideas on this? Regards Naman
![](https://secure.gravatar.com/avatar/a61e243764490913906c773c9acb0d3c.jpg?s=120&d=mm&r=g)
naman jain <namanvit@gmail.com> writes:
I wanted to log in a file and keep the console also going with it.
I can read this as either: * Keep file logs and stdout/console completely separate, or * File gets explicit logs and copies of stdout, but stdout also goes to console as well. The latter is something I'd more commonly do, but the former is what you explain further below with your rules.
I am using log.startLogging(open(logname, 'w'))
whatever documentation I could find about this API, it suggested me to pass setStdout=False(in order to keep stdout away from putting into log) somewhere in the startlogging function.
Something like this I tried: log.startLogging(sys.stdout,setStdout=False)
Why did you switch to sys.stdout here? While this will prevent sys.stdout and sys.stderr from being redirected, it will use sys.stdout for any log.msg() calls, so nothing would end up in your log file.
Doesnt help in solving the problem of getting both things:
Requirement: log.msg("messageforlog") -> goes to log print "message for console" -> goes to console
Could anyone give some ideas on this?
I think you had the right idea with setStdout, but changed more than just that when you tested it. If you use a call like log.startLogging(open(logname, 'w'), setStdout=False) I believe you'll get what you want. Only calls to log.msg() will go to the file, and stdout/stderr will be left unchanged. -- David
![](https://secure.gravatar.com/avatar/bc0ff9a78eb210aad0b1c362d7b0889b.jpg?s=120&d=mm&r=g)
Hi, Using log.startLogging(open(logname, 'w'), setStdout=False) very well solves the issue.. of stdout remaining untouched and log.msg() calls going to log Can we do this ?: Log file gets -> both the log.msg() calls , the stdout +stderr console -> gets stdout +stderr as usual Regards Naman On Fri, Oct 30, 2009 at 9:25 PM, David Bolen <db3l.net@gmail.com> wrote:
naman jain <namanvit@gmail.com> writes:
I wanted to log in a file and keep the console also going with it.
I can read this as either:
* Keep file logs and stdout/console completely separate, or * File gets explicit logs and copies of stdout, but stdout also goes to console as well.
The latter is something I'd more commonly do, but the former is what you explain further below with your rules.
I am using log.startLogging(open(logname, 'w'))
whatever documentation I could find about this API, it suggested me to pass setStdout=False(in order to keep stdout away from putting into log) somewhere in the startlogging function.
Something like this I tried: log.startLogging(sys.stdout,setStdout=False)
Why did you switch to sys.stdout here? While this will prevent sys.stdout and sys.stderr from being redirected, it will use sys.stdout for any log.msg() calls, so nothing would end up in your log file.
Doesnt help in solving the problem of getting both things:
Requirement: log.msg("messageforlog") -> goes to log print "message for console" -> goes to console
Could anyone give some ideas on this?
I think you had the right idea with setStdout, but changed more than just that when you tested it. If you use a call like log.startLogging(open(logname, 'w'), setStdout=False) I believe you'll get what you want. Only calls to log.msg() will go to the file, and stdout/stderr will be left unchanged.
-- David
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
![](https://secure.gravatar.com/avatar/bc0ff9a78eb210aad0b1c362d7b0889b.jpg?s=120&d=mm&r=g)
People, Can anyone help me with this issue? log.startLogging(open(logname,'w') or log.startLogging(open(logname,'w'), setStdout=True) Both print the console output in the log. But nothing is displayed on console! So I get a empty console I would like to keep the console going also: Precisely: Console -> gets the print calls Not Happening Log -> gets the print calls + log.msg calls Happening I understand that this might be a trivial issue, but I am not able to figure it out in the documentation available :( Thanks for any help Regards Naman On Wed, Nov 4, 2009 at 7:43 AM, naman jain <namanvit@gmail.com> wrote:
Hi,
Using log.startLogging(open(logname, 'w'), setStdout=False) very well solves the issue.. of stdout remaining untouched and log.msg() calls going to log
Can we do this ?: Log file gets -> both the log.msg() calls , the stdout +stderr console -> gets stdout +stderr as usual
Regards Naman
On Fri, Oct 30, 2009 at 9:25 PM, David Bolen <db3l.net@gmail.com> wrote:
naman jain <namanvit@gmail.com> writes:
I wanted to log in a file and keep the console also going with it.
I can read this as either:
* Keep file logs and stdout/console completely separate, or * File gets explicit logs and copies of stdout, but stdout also goes to console as well.
The latter is something I'd more commonly do, but the former is what you explain further below with your rules.
I am using log.startLogging(open(logname, 'w'))
whatever documentation I could find about this API, it suggested me to pass setStdout=False(in order to keep stdout away from putting into log) somewhere in the startlogging function.
Something like this I tried: log.startLogging(sys.stdout,setStdout=False)
Why did you switch to sys.stdout here? While this will prevent sys.stdout and sys.stderr from being redirected, it will use sys.stdout for any log.msg() calls, so nothing would end up in your log file.
Doesnt help in solving the problem of getting both things:
Requirement: log.msg("messageforlog") -> goes to log print "message for console" -> goes to console
Could anyone give some ideas on this?
I think you had the right idea with setStdout, but changed more than just that when you tested it. If you use a call like log.startLogging(open(logname, 'w'), setStdout=False) I believe you'll get what you want. Only calls to log.msg() will go to the file, and stdout/stderr will be left unchanged.
-- David
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
![](https://secure.gravatar.com/avatar/607cfd4a5b41fe6c886c978128b9c03e.jpg?s=120&d=mm&r=g)
On 08:59 am, namanvit@gmail.com wrote:
People,
Can anyone help me with this issue?
log.startLogging(open(logname,'w') or log.startLogging(open(logname,'w'), setStdout=True)
Both print the console output in the log. But nothing is displayed on console! So I get a empty console I would like to keep the console going also:
Precisely: Console -> gets the print calls Not Happening Log -> gets the print calls + log.msg calls Happening
I understand that this might be a trivial issue, but I am not able to figure it out in the documentation available :(
Asking the same thing over and over again isn't going to get you an answer. Try making your question more clear. I haven't answered because I don't understand what you're trying to achieve. Jean-Paul
![](https://secure.gravatar.com/avatar/bc0ff9a78eb210aad0b1c362d7b0889b.jpg?s=120&d=mm&r=g)
I ll try to rephrase it better: I simply want the print statement to appear in both the log file and the console. print("goes in both") -> the log also gets this and the console also. Thanks On Thu, Nov 5, 2009 at 1:29 PM, <exarkun@twistedmatrix.com> wrote:
On 08:59 am, namanvit@gmail.com wrote:
People,
Can anyone help me with this issue?
log.startLogging(open(logname,'w') or log.startLogging(open(logname,'w'), setStdout=True)
Both print the console output in the log. But nothing is displayed on console! So I get a empty console I would like to keep the console going also:
Precisely: Console -> gets the print calls Not Happening Log -> gets the print calls + log.msg calls Happening
I understand that this might be a trivial issue, but I am not able to figure it out in the documentation available :(
Asking the same thing over and over again isn't going to get you an answer. Try making your question more clear. I haven't answered because I don't understand what you're trying to achieve.
Jean-Paul
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
![](https://secure.gravatar.com/avatar/607cfd4a5b41fe6c886c978128b9c03e.jpg?s=120&d=mm&r=g)
On 01:20 pm, namanvit@gmail.com wrote:
I ll try to rephrase it better:
I simply want the print statement to appear in both the log file and the console. print("goes in both") -> the log also gets this and the console also.
Thanks
How about installing two log observers? from sys import stdout from twisted.python.log import FileLogObserver, startLogging, addObserver, msg # First, startLogging to capture stdout startLogging(stdout) # Now add an observer that logs to a file addObserver(FileLogObserver(file("foo.log")).emit) msg("Hello, world") print "Goodbye, world" Jean-Paul
![](https://secure.gravatar.com/avatar/bc0ff9a78eb210aad0b1c362d7b0889b.jpg?s=120&d=mm&r=g)
Thanks. that does the job for me very well. On Thu, Nov 5, 2009 at 7:18 PM, <exarkun@twistedmatrix.com> wrote:
On 01:20 pm, namanvit@gmail.com wrote:
I ll try to rephrase it better:
I simply want the print statement to appear in both the log file and the console. print("goes in both") -> the log also gets this and the console also.
Thanks
How about installing two log observers?
from sys import stdout from twisted.python.log import FileLogObserver, startLogging, addObserver, msg
# First, startLogging to capture stdout startLogging(stdout)
# Now add an observer that logs to a file addObserver(FileLogObserver(file("foo.log")).emit)
msg("Hello, world") print "Goodbye, world"
Jean-Paul
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
participants (3)
-
David Bolen
-
exarkun@twistedmatrix.com
-
naman jain