Reputation: 137
I am using following code to log
logging.basicConfig(filename=os.environ['USERPROFILE']+'\\myApp', level=logging.DEBUG, format='%(asctime)s - [Thread-%(thread)d] - [Process-%(process)d] - %(levelname)s - %(funcName)s - %(lineno)d - %(message)s')
With this logging haw can we implement the FileSize limit of logger files, like if the log file size is more than 5MB then I want to write it in new file.
In Java, we can mention the file Size like below in logback.xml
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>10MB</maxFileSize>
</triggeringPolicy>
Upvotes: 2
Views: 2414
Reputation: 99355
Use a RotatingFileHandler
. This takes maxBytes
and backupCount
keyword parameters. From the docs:
You can use the
maxBytes
andbackupCount
values to allow the file to rollover at a predetermined size. When the size is about to be exceeded, the file is closed and a new file is silently opened for output. Rollover occurs whenever the current log file is nearlymaxBytes
in length; ifmaxBytes
is zero, rollover never occurs. IfbackupCount
is non-zero, the system will save old log files by appending the extensions ‘.1’, ‘.2’ etc., to the filename. For example, with abackupCount
of 5 and a base file name of app.log, you would get app.log, app.log.1, app.log.2, up to app.log.5. The file being written to is always app.log. When this file is filled, it is closed and renamed to app.log.1, and if files app.log.1, app.log.2, etc. exist, then they are renamed to app.log.2, app.log.3 etc. respectively.
Upvotes: 2