使用Python3.7,Windows10Pro,Pywin32
我有一个测试脚本,它启动服务,并在发出不同的命令时将一些基本行推入日志文件。守则如下:
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import logging
class AppServerSvc(win32serviceutil.ServiceFramework):
_svc_name_ = "TestService"
_svc_display_name_ = "Test Service"
_svc_description_ = "New Test Service"
logging.basicConfig(filename='search_server.log', level=logging.INFO)
logging.info('Class opened')
def __init__(self, args):
logging.basicConfig(filename='search_server.log', level=logging.INFO)
logging.info('Init')
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
socket.setdefaulttimeout(60)
def SvcStop(self):
logging.basicConfig(filename='search_server.log', level=logging.INFO)
logging.info('Stop')
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
logging.basicConfig(filename='search_server.log', level=logging.INFO)
logging.info('Run')
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
self.main()
def main(self):
print("running")
logging.basicConfig(filename='search_server.log', level=logging.INFO)
logging.info('Main')
if __name__ == '__main__':
logging.basicConfig(filename='search_server.log', level=logging.INFO)
logging.info('Calling Handle Command Line')
win32serviceutil.HandleCommandLine(AppServerSvc)我已经经历了基本的问题解决与此,服务是安装,启动,重新启动和删除没有任何错误。但是,我期望日志文件接收基本输出,以显示函数正在被击中,但它没有。
我在admin命令提示符下打的电话:
C:\PythonScripts\SearchServer>python servicetest.py install
Installing service TestService
Service installed
C:\PythonScripts\SearchServer>python servicetest.py start
Starting service TestService
C:\PythonScripts\SearchServer>python servicetest.py restart
Restarting service TestService
C:\PythonScripts\SearchServer>python servicetest.py remove
Removing service TestService
Service removed
C:\PythonScripts\SearchServer>日志文件的内容:
INFO:root:Class opened
INFO:root:Calling Handle Command Line
INFO:root:Class opened
INFO:root:Calling Handle Command Line
INFO:root:Class opened
INFO:root:Calling Handle Command Line
INFO:root:Class opened
INFO:root:Calling Handle Command Line正如您所看到的那样,每次发出命令时都会命中服务,但是我也希望调用内部函数。作为服务和Python的新手,我想知道我是否错过了什么?我假设函数名是预先定义的,不需要我设置委托来访问它们。在我遇到的任何问题中,我都没有看到过这种情况。
当然,我假设这些函数应该被击中,并且它们正在被击中,并且能够创建日志?
任何感激的帮助。
发布于 2019-08-20 11:49:16
代码有几个问题:
if __name__ == '__main__':之前,因为代码的自然流。因此,类定义中在logging.basicConfig()中设置的任何内容都将成为整个脚本的最终结果。它不是这个设置的理想场所,所以应该移到其他地方(顶部,最好是在班外)。在所有这些更改之后,脚本将如下所示:
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import logging.handlers
log_file_path = "" # mention full path here
mylogger = logging.getLogger("TestLogger")
mylogger.setLevel(logging.INFO)
handler = logging.handlers.RotatingFileHandler(log_file_path)
mylogger.addHandler(handler)
class AppServerSvc(win32serviceutil.ServiceFramework):
_svc_name_ = "TestService"
_svc_display_name_ = "Test Service"
_svc_description_ = "New Test Service"
mylogger.info('Class opened')
def __init__(self, args):
mylogger.info('Init')
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
socket.setdefaulttimeout(60)
def SvcStop(self):
mylogger.info('Stop')
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
mylogger.info('Run')
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
self.main()
def main(self):
print("running")
mylogger.info('Main')
if __name__ == '__main__':
mylogger.info('Calling Handle Command Line')
win32serviceutil.HandleCommandLine(AppServerSvc)输出:
类打开Init运行主类打开调用句柄命令行
发布于 2021-08-12 12:18:04
我在python日志记录中遇到了无法解释的问题,我通过在程序开始时设置日志记录来解决这些问题:
import logging
logging.basicConfig(filename='convert_st.log', level=logging.INFO)
logging.info('Started')
import all_other_packages
import...
...
def main:
# main comes here
...
if __name__ == '__main__':
main()https://stackoverflow.com/questions/57559440
复制相似问题