mirror of
https://gitlab.com/jeancf/twoot.git
synced 2025-02-22 08:12:15 +00:00
Implemented log file truncation
This commit is contained in:
parent
4c38be9194
commit
bbe2f6e5ab
|
@ -74,6 +74,6 @@ subst_reddit = []
|
|||
log_level = "WARNING"
|
||||
|
||||
# How many days to keep log messages for
|
||||
# Log messages older that that log_days will be deleted
|
||||
# Log messages older than log_days will be deleted
|
||||
# Default is 3
|
||||
log_days = 3
|
||||
|
|
52
twoot.py
52
twoot.py
|
@ -19,7 +19,7 @@
|
|||
"""
|
||||
|
||||
import argparse
|
||||
import datetime
|
||||
from datetime import datetime, timedelta
|
||||
import logging
|
||||
import os
|
||||
import random
|
||||
|
@ -90,7 +90,8 @@ def build_config(args):
|
|||
'subst_twitter': [],
|
||||
'subst_youtube': [],
|
||||
'subst_reddit': [],
|
||||
'log_level': "WARNING"
|
||||
'log_level': "WARNING",
|
||||
'log_days': 3,
|
||||
}
|
||||
|
||||
# Create default config object
|
||||
|
@ -573,12 +574,6 @@ def main(argv):
|
|||
|
||||
mast_password = args['p']
|
||||
|
||||
# Remove previous log file
|
||||
# try:
|
||||
# os.remove(TOML['config']['twitter_account'] + '.log')
|
||||
# except FileNotFoundError:
|
||||
# pass
|
||||
|
||||
# Setup logging to file
|
||||
logging.basicConfig(
|
||||
filename=TOML['config']['twitter_account'] + '.log',
|
||||
|
@ -626,6 +621,7 @@ def main(argv):
|
|||
logging.info(' subst_twitter : ' + str(TOML['options']['subst_youtube']))
|
||||
logging.info(' subst_twitter : ' + str(TOML['options']['subst_reddit']))
|
||||
logging.info(' log_level : ' + str(TOML['options']['log_level']))
|
||||
logging.info(' log_days : ' + str(TOML['options']['log_days']))
|
||||
|
||||
# Try to open database. If it does not exist, create it
|
||||
sql = sqlite3.connect('twoot.db')
|
||||
|
@ -717,10 +713,10 @@ def main(argv):
|
|||
# Extract time stamp
|
||||
time_string = status.find('span', class_='tweet-date').a.get('title')
|
||||
try:
|
||||
timestamp = datetime.datetime.strptime(time_string, '%d/%m/%Y, %H:%M:%S').timestamp()
|
||||
timestamp = datetime.strptime(time_string, '%d/%m/%Y, %H:%M:%S').timestamp()
|
||||
except:
|
||||
# Dec 21, 2021 · 12:00 PM UTC
|
||||
timestamp = datetime.datetime.strptime(time_string, '%b %d, %Y · %I:%M %p %Z').timestamp()
|
||||
timestamp = datetime.strptime(time_string, '%b %d, %Y · %I:%M %p %Z').timestamp()
|
||||
|
||||
# Check if time is within acceptable range
|
||||
if not is_time_valid(timestamp):
|
||||
|
@ -977,8 +973,42 @@ def main(argv):
|
|||
|
||||
logging.info('Deleted ' + str(excess_count) + ' old records from database.')
|
||||
|
||||
terminate(0)
|
||||
# Close logger and log file
|
||||
logging.shutdown()
|
||||
|
||||
# Remove older log messages
|
||||
# Max allowed age of log message
|
||||
max_delta = timedelta(TOML['options']['log_days'])
|
||||
|
||||
# Open log file
|
||||
log_file = open('test.log', 'r')
|
||||
|
||||
# Check each line
|
||||
pos = log_file.tell()
|
||||
while log_file:
|
||||
line = log_file.readline()
|
||||
try:
|
||||
# Extract date on log line
|
||||
date = datetime.strptime(line[:10], '%Y-%m-%d')
|
||||
except ValueError:
|
||||
# date was not found on this line, try next one
|
||||
continue
|
||||
# Time difference between log message and now
|
||||
log_delta = datetime.now() - date
|
||||
# Only keep the number of days of the difference
|
||||
log_delta = timedelta(days=log_delta.days)
|
||||
if log_delta < max_delta:
|
||||
# Reset file pointer to position before reading last line
|
||||
log_file.seek(pos)
|
||||
remainder = log_file.read()
|
||||
output_file = open('test_new.log', 'w')
|
||||
output_file.write(remainder)
|
||||
output_file.close()
|
||||
break # Exit while loop
|
||||
|
||||
pos = log_file.tell()
|
||||
|
||||
terminate(0)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv)
|
||||
|
|
Loading…
Reference in New Issue
Block a user