Pergunta

How can we make use of design pattern for log generation in Multi threaded environment. There is one log file and there are multiple threads need to write in this log file. So there has to be a mechanism that each thread can access the same file handler once it is created.
Should i use Singleton or Factory design pattern as there is only point of installation of object or there is a better way to do this.

Foi útil?

Solução

The Python logging module is actually thread-safe by default:

The logging module is intended to be thread-safe without any special work needing to be done by its clients. It achieves this though using threading locks; there is one lock to serialize access to the module’s shared data, and each handler also creates a lock to serialize access to its underlying I/O.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top