简体   繁体   中英

Python - How do I get UUID to change on each FOR iteration?

I hope this is a simple fix and that I am just being blind. I'm writing this script for going through some data, but I can't get this UUID value to change.

At the start of the script I am declaring the UUID value inside of the FOR loop in order for the value to be defined on each iteration:

from uuid import uuid4 as uid
...
for entry in df.loc():
    uuid = uid()
    ...
    logs(uuid, text)
    uuid = None

But when the script is ran, instead of it printing a new UUID (which is what I was expecting due to the variable being defined on each iteration), it prints the same UUID indefinitely. As you can see, I tried adding the UUID equals none at the end to try catch this, but it didn't work.

Here is my logging function:

import logging, os
#import uuid

direc = os.path.dirname(__file__)

def logs(uuid: str, message: str):
    formatter = logging.Formatter('[%(asctime)s] [{}] [%(levelname)s] | %(message)s'.format(uuid))

    file_handler = logging.FileHandler(os.path.join(direc, "main.log"))
    file_handler.setLevel(logging.INFO)
    file_handler.setFormatter(formatter)
    console_handler = logging.StreamHandler()
    console_handler.setLevel(logging.INFO)
    console_handler.setFormatter(formatter)

    logger = logging.getLogger()
    if not logger.handlers:
        logger.addHandler(file_handler)
        logger.addHandler(console_handler)
        logger.setLevel(logging.INFO)

    logger.info(message)

    return logger

Output:

[2021-05-05 08:24:25,288] [1cbb447f-df61-490f-990b-3c18d1b465e3] [INFO] | Name Match...
[2021-05-05 08:24:25,291] [1cbb447f-df61-490f-990b-3c18d1b465e3] [INFO] | Number Match...
[2021-05-05 08:24:26,678] [1cbb447f-df61-490f-990b-3c18d1b465e3] [INFO] | Roles Match...
[2021-05-05 08:24:28,809] [1cbb447f-df61-490f-990b-3c18d1b465e3] [INFO] | Name Match...
[2021-05-05 08:24:28,812] [1cbb447f-df61-490f-990b-3c18d1b465e3] [INFO] | Number Match...

I hope you can help!

I answered this as soon as I was writing this out. I had defined the logger formatter with the UUID so as it was set up like that, all UUIDs generated afterward were not getting added to the formatter. I could have ditched the handlers on each call of the function, but I decided to change it so I specify the UUID in the message instead.

Before:

formatter = logging.Formatter('[%(asctime)s] [{}] [%(levelname)s] | %(message)s'.format(uuid))

logger.info(message)

After:

formatter = logging.Formatter('[%(asctime)s] [%(levelname)s] %(message)s')

logger.info("[" + str(uuid) + "]" + " | " + message)

Output:

[2021-05-05 08:48:48,342] [INFO] [1f458c96-6074-4e64-9c09-505f1c296876] | Name Match...
[2021-05-05 08:48:48,344] [INFO] [1f458c96-6074-4e64-9c09-505f1c296876] | Number Match...
[2021-05-05 08:48:49,480] [INFO] [1f458c96-6074-4e64-9c09-505f1c296876] | Roles Match...
[2021-05-05 08:48:51,786] [INFO] [8122f7b9-72ab-4cc8-a45e-4a3c2a43bc0d] | Name Match...
[2021-05-05 08:48:51,788] [INFO] [8122f7b9-72ab-4cc8-a45e-4a3c2a43bc0d] | Number Match...
[2021-05-05 08:48:52,850] [INFO] [8122f7b9-72ab-4cc8-a45e-4a3c2a43bc0d] | Roles Match...

I know this isn't the better way of adding concating strings, but I just need this to work for now. Cleanup will come after!

Thanks all!

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