简体   繁体   中英

i have done with python flask api. Now how to create log file to know whenever the api is hit?

i have created an api with Python and flask. now i am just trying to get the log of api hit. how to do it?

This is for ubuntu server

def main():
    file = open(“MY_Log_File__%H_%M_%S_%d_%m_%Y.txt”, “w”)
    file.write(“    ”) 
    file.close()

if __name__ == '__main__':
    main()

i want to get a text file in which all the details of the api must be added while it srunning with the file name MY_Log_File__%H_%M_%S_%d_%m_%Y.txt

Try:

from datetime import datetime

def main():
    file = open(“MY_Log_File__%s.txt” %datetime.now().strftime('%H_%M_%S_%d_%m_%Y'), “w”)
    file.write(“    ”) 
    file.close()

if __name__ == '__main__':
    main()

To create a new log file each time including the date use this way;

import time
from datetime import datetime

timeStamp = time.time()
date = datetime.fromtimestamp(timeStamp).strftime("%Y-%m-%d %H:%M:%S")


def main():
    logFile = open(“MY_Log_File__%s.txt” % (date)
    logFile.write("MY API INFORMATION %s" % (date))
    logFile.close()

    if __name__ == '__main__':
    main()

To update an existing file use this way:

import time
from datetime import datetime

timeStamp = time.time()
date = datetime.fromtimestamp(timeStamp).strftime("%Y-%m-%d %H:%M:%S")


def main():
    with open("/path/yourLogFile.txt", "a") as logFile:
        logFile.write("MY API INFORMATION %s" % (date))
        logFile.close()

    if __name__ == '__main__':
    main()

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM