1

I would like to add a timestamp for each output in console.

My tool is using selenium and clicks on specific things on a website. when specific events happen (for example see below code block) I would like to print it with a timestamp.

print(f'Bought player: {str_player_name} for {int_player_price}')
print(f'Estimated profit: {int_expected_profit}')
print(f'Player counter {str(int_players_bought)}/{str(int_max_players_bought)}')

I have many prints in my console - is there a way to automatically add a timestamp to each print('')?

2 Answers 2

2

You could define a printf function for that too

from datetime import datetime


def printf(*arg, **kwarg):
    timestamp = datetime.now().strftime("%H:%M:%S")
    print(timestamp, *arg, **kwarg)

Then, replace all your print call into printf.


But what you are trying to do is the job of the logger. I suggest you should add loggers to your code before it becomes complicated.

Sign up to request clarification or add additional context in comments.

Comments

1

add this to your code

from datetime import datetime
print(datetime.now().strftime(%H:%M:%S))

2 Comments

hm would i have to add it once in my code? or everytime I print a message?
Everytime you print a message

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.