Code:
import sys
from flask import Flask
import logging
from logging.handlers import RotatingFileHandler
from datetime import datetime
app = Flask(__name__)
formatter = logging.Formatter('%(asctime)s.%(msecs)03d [%(thread)d] [%(levelname)s]: %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
file_hdlr = RotatingFileHandler('logs/debug_{}.log'.format(datetime.utcnow().strftime('%Y_%m_%d')), maxBytes= 1024, backupCount= 10)
file_hdlr.setFormatter(formatter)
file_logger = logging.getLogger('file_logger')
file_logger.setLevel(logging.INFO)
file_logger.addHandler(file_hdlr)
@app.route('/test', methods = ['POST'])
def test():
"""
This function is for testing API
Returns:
This function returns a string.
"""
for i in range(25):
file_logger.info(f"{i}")
return "API working correctly"
if __name__ == "__main__":
app.run(host = '0.0.0.0', port = 5001, debug = True, threaded = True)
Traceback:
--- Logging error ---
Traceback (most recent call last):
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
self.doRollover()
File "c:\users\aniket.bote\appdata\local\programs\python\python38\libr
self.rotate(self.baseFilename, dfn)
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
os.rename(source, dest)
PermissionError: [WinError 32] The process cannot access the file becauUsers\\aniket.bote\\Desktop\\Aniket\\DS_2021_Array_cable_optimization\\iket.bote\\Desktop\\Aniket\\DS_2021_Array_cable_optimization\\logs\\deb
Call stack:
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
self._bootstrap_inner()
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
self.run()
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
self._target(*self._args, **self._kwargs)
File "c:\users\aniket.bote\appdata\local\programs\python\python38\libst_thread
self.finish_request(request, client_address)
File "c:\users\aniket.bote\appdata\local\programs\python\python38\libt
self.RequestHandlerClass(request, client_address, self)
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
self.handle()
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimiz.py", line 342, in handle
BaseHTTPRequestHandler.handle(self)
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
self.handle_one_request()
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimiz.py", line 374, in handle_one_request
self.run_wsgi()
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimiz.py", line 319, in run_wsgi
execute(self.server.app)
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimiz.py", line 310, in execute
for data in application_iter:
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimiz_init__.py", line 309, in debug_application
app_iter = self.app(environ, start_response)
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimizine 2088, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimizine 2070, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimizine 1513, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimizine 1499, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.v
File "c:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimiz
file_logger.info(f"{i}")
Message: '23'
Arguments: ()
--- Logging error ---
Traceback (most recent call last):
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
self.doRollover()
File "c:\users\aniket.bote\appdata\local\programs\python\python38\libr
self.rotate(self.baseFilename, dfn)
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
os.rename(source, dest)
PermissionError: [WinError 32] The process cannot access the file becauUsers\\aniket.bote\\Desktop\\Aniket\\DS_2021_Array_cable_optimization\\iket.bote\\Desktop\\Aniket\\DS_2021_Array_cable_optimization\\logs\\deb
Call stack:
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
self._bootstrap_inner()
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
self.run()
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
self._target(*self._args, **self._kwargs)
File "c:\users\aniket.bote\appdata\local\programs\python\python38\libst_thread
self.finish_request(request, client_address)
File "c:\users\aniket.bote\appdata\local\programs\python\python38\libt
self.RequestHandlerClass(request, client_address, self)
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
self.handle()
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimiz.py", line 342, in handle
BaseHTTPRequestHandler.handle(self)
File "c:\users\aniket.bote\appdata\local\programs\python\python38\lib
self.handle_one_request()
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimiz.py", line 374, in handle_one_request
self.run_wsgi()
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimiz.py", line 319, in run_wsgi
execute(self.server.app)
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimiz.py", line 310, in execute
for data in application_iter:
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimiz_init__.py", line 309, in debug_application
app_iter = self.app(environ, start_response)
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimizine 2088, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimiz return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "c:\Users\aniket.bote\Desktop\Aniket\DS_2021_Array_cable_optimization\app.py", line 33, in test
file_logger.info(f"{i}")
Message: '24'
Arguments: ()
The log file generated contains numbers up to 22. I am trying to create a logger that will create a new log file once the buffer size is reached. The code provided is a standalone code that regenerates the issue.
I am using a windows system.
The same code works if I remove the '.log' extension from the file
If you look at the traceback, there is this line
PermissionError: [WinError 32] The process cannot access the file becauUsers\\\\aniket.bote\\\\Desktop\\\\Aniket\\\\DS_2021_Array_cable_optimization\\\\iket.bote\\\\Desktop\\\\Aniket\\\\DS_2021_Array_cable_optimization\\\\logs\\\\deb
This implies that the path is wrong here, which can also be seen from becauUsers which is probably not a correct path in Windows. You have to find where it uses this path.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.