1

I use the Python 2.7 standard logging module to do the logging for our system. Occasionally, we run a simulation of past events, and it would really be useful if instead of using the system time in datetime.now() I would be able to use my own custom system time.

I know there is a way to use Formatter to alter the time string format, but can I actually alter the content of the logged time string as well?

Thank you very much

1 Answer 1

1

From : https://docs.python.org/3/howto/logging.html#formatters

Formatters use a user-configurable function to convert the creation time of a record to a tuple. By default, time.localtime() is used; to change this for a particular formatter instance, set the converter attribute of the instance to a function with the same signature as time.localtime() or time.gmtime(). To change it for all formatters, for example if you want all logging times to be shown in GMT, set the converter attribute in the Formatter class (to time.gmtime for GMT display).

Therefore define your custom system time as a function that matches time.localtime() signature and assign it to the logger.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.