How to redirect logging to files in a CherryPy app #1906
-
Hi, I use cherrypy in a windows service. sys.stdout = open(os.path.join(pathOfThisScript, "stdOut.txt"), "w")
sys.stderr = open(os.path.join(pathOfThisScript, "stdErr.txt"), "w") I think cherrpy doesn't care about that and is still writing to stdout. [29/Mar/2021:15:12:27] ENGINE Bus STARTING
[29/Mar/2021:15:12:27] ENGINE Started monitor thread 'Autoreloader'.
[29/Mar/2021:15:12:28] ENGINE Serving on http://127.0.0.1:8000
[29/Mar/2021:15:12:28] ENGINE Bus STARTED This becomes worse if I run the service as a real service. In that case there is no stdout and error. But cherrypy still tries to write to them and produces exceptions that were written to my stderr file. They are repeated many times: --- Logging error ---
Traceback (most recent call last):
File "C:\Python39\lib\logging\__init__.py", line 1082, in emit
stream.write(msg + self.terminator)
AttributeError: 'NoneType' object has no attribute 'write'
Call stack:
File "C:\Python39\lib\site-packages\win32\lib\win32serviceutil.py", line 839, in SvcRun
self.SvcDoRun()
File "C:\GlobalX_Installation\globalx\psbp\psProofDjango\bpservice3.py", line 97, in SvcDoRun
cherrypy.engine.start()
File "C:\Python39\lib\site-packages\cherrypy\process\wspbus.py", line 266, in start
self.log('Bus STARTING')
File "C:\Python39\lib\site-packages\cherrypy\process\wspbus.py", line 584, in log
self.publish('log', msg, level)
File "C:\Python39\lib\site-packages\cherrypy\process\wspbus.py", line 230, in publish
output.append(listener(*args, **kwargs))
File "C:\Python39\lib\site-packages\cherrypy\__init__.py", line 352, in _buslog
log.error(msg, 'ENGINE', severity=level)
File "C:\Python39\lib\site-packages\cherrypy\_cplogging.py", line 213, in error
self.error_log.log(
Message: '[29/Mar/2021:17:25:06] ENGINE Bus STARTING'
Arguments: () Can you please make cherrypy notice that I have redirected stdout and err? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Sounds like you patched stderr/out after CherryPy got the pointers to the old objects. Also, I think patching global objects like that is rather bad. FWIW it sounds like you forgot to configure or disable logging per https://docs.cherrypy.org/en/latest/basics.html#logging: from pathlib import Path
THIS_SCRIPT_DIR = Path(__file__).parent.resolve()
cherrypy.config.update({
'log.screen': False,
'log.access_file': str(THIS_SCRIPT_DIR / 'stdOut.txt'),
'log.error_file': str(THIS_SCRIPT_DIR / 'stdErr.txt'),
}) Also, note that you could set the environment to cherrypy.config.update({'environment': 'production'}) |
Beta Was this translation helpful? Give feedback.
Sounds like you patched stderr/out after CherryPy got the pointers to the old objects. Also, I think patching global objects like that is rather bad.
FWIW it sounds like you forgot to configure or disable logging per https://docs.cherrypy.org/en/latest/basics.html#logging:
Also, note that you could set the environment to
production
per https://docs.cherrypy.org/en/latest/config.html#environments and that sets'log.screen': False
by default:che…