log4z icon indicating copy to clipboard operation
log4z copied to clipboard

File Logger and SYNC mode

Open haoxi911 opened this issue 8 years ago • 1 comments

In current apparoch, if we enable both file logger and SYNC write, the following code will be run:

if (_loggers[pLog->_id]._outfile && LOG4Z_ALL_SYNCHRONOUS_OUTPUT) { if (openLogger(pLog)) { _loggers[pLog->_id]._handle.write(pLog->_content, pLog->_contentLen); closeLogger(pLog->_id); } }

The file handle will be opened before writing each log, and be closed after writing completed. This is not a problem, but I do meet an issue to keep writing logs into same log file. Looking into openLogger()

if (!pLogger->_handle.isOpen()) { pLogger->_curFileCreateTime = pLog->_time; pLogger->_curWriteLen = 0; tm t = timeToTm(pLogger->_curFileCreateTime); sprintf(buf, "%s_%s_%04d%02d%02d%02d%02d_%s_%03u.log", _proName.c_str(), name.c_str(), t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, _pid.c_str(), pLogger->_curFileIndex);

The logger will compose the target log filename based on current log's creation date, it means the logs will be wrote into two different log files, if we run the software for 2 minutes, is it the expected behavior?

Any suggestions if I want to keep writing logs into same log file, and only rolling logs when it exceeded the file size limitation (e.g. 100MB)?

haoxi911 avatar Oct 27 '16 01:10 haoxi911

Also, rolling is not work in SYNC mode, I made some quick fixes here: https://github.com/haoxi911/log4z/commit/7f74981c869b8e690f2af3758f14b3c931e18c3c

By the way, I prefer we close the file handle immediately after writing logs (SYNC mode), instead of taking file handle during the lifecycle (ASYNC mode).

haoxi911 avatar Oct 27 '16 03:10 haoxi911