G
G
Gast0n2020-05-07 08:51:36
Python
Gast0n, 2020-05-07 08:51:36

Why is the log file not being written?

Hello. Added the function of writing to the log, but for some reason the file itself is not created, so logging does not work. Tell me, what's wrong?

from flask import Flask, render_template, request
from vsearch import search4letters

app = Flask(name)

def log_request(req: 'flask_request', res: str) -> None:
  with open('vsearch.log', 'a') as log:
    print(req, res, file=log)

@app.route('/search4', methods=['POST', 'GET'])
def do_search() -> 'html':
  phrase = request.form['phrase']  
  letters = request.form['letters']
  title = 'Here are your results!:'  
  results = str(search4letters(phrase, letters))
  log_request(request, results)  
  return render_template('results.html',
    the_phrase=phrase,
    the_letters=letters,
    the_title=title,
    the_results=results)

@app.route('/') 
@app.route('/entry')
def entry_page() -> 'html':
  return render_template('entry.html', 
    the_title='Welcome to search4letters on the Web!')

if name=='main':
  app.run(debug=True)


OC - ​​Ubuntu

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Pankov, 2020-05-07
@trapwalker

everything is correct, with "a" should be created. You just probably didn't find it. It is created in the current directory. do normal print(os.getcwd())next to the entry, and you will see the path where your file is located.
But it's a bad idea to write logs like that. There is a logging module for this. Here everything is very detailed and with examples.

G
Gast0n, 2020-05-07
@Gast0n

Thank you. found the file in the user's home folder. now how to put it in the folder with the script?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question