Skip to content
This repository was archived by the owner on Apr 28, 2023. It is now read-only.

Commit 3a5bad7

Browse files
author
PokestarFan
committed
Add logging
1 parent 7aa93c0 commit 3a5bad7

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

duplicate.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import datetime
2+
import logging
23
import praw
34
from login import reddit
45

6+
7+
58
def action():
69
for sub_id in reddit.subreddit('all').stream.submissions():
10+
logging.debug('Starting submission {}'.format(sub_id))
711
blockeduser = 0
812
for item in reddit.inbox.messages(limit=5):
913
if str(item.subject) == 'Remove me from posts':
@@ -13,6 +17,7 @@ def action():
1317
newstring = username.replace(r'/u/','')
1418
newstring = newstring = '\n'
1519
file.write(newstring)
20+
logger.debug('Cycle 1 for submission {} has finished'.format(sub_id))
1621
duplicates = []
1722
submission = praw.models.Submission(reddit, id = sub_id)
1823
with open('blockusers.txt','r') as newfile:
@@ -22,6 +27,7 @@ def action():
2227
blockeduser = 1
2328
else:
2429
pass
30+
logger.debug('Cycle 2 for submission {} has finished'.format(sub_id))
2531
if blockeduser == 0:
2632
for duplicate in submission.duplicates():
2733
dup_sub = praw.models.Submission(reddit, id = duplicate)
@@ -37,19 +43,40 @@ def action():
3743
for dup in duplicates:
3844
message = str(message + '\n * [{}]({}) on /r/{} (created at {} by {})').format(dup['title'], dup['link'], dup['subreddit'], dup['time'], dup['author'])
3945
message = message + '\n\n ---- \n\n ^^I ^^am ^^a ^^bot ^^[FAQ](https://www.reddit.com/r/DuplicatesBot/wiki/index)-[Code](https://github.com/PokestarFan/DuplicateBot-[Bugs](https://www.reddit.com/r/DuplicatesBot/comments/6ypgmx/bugs_and_problems/)-[Suggestions](https://www.reddit.com/r/DuplicatesBot/comments/6ypg85/suggestion_for_duplicatesbot/)-[Block](https://www.reddit.com/r/DuplicatesBot/wiki/index#wiki_block_bot_from_tagging_on_your_posts)'
46+
logger.debug('Cycle 3 for submission {} has finished'.format(sub_id))
4047
try:
4148
submission.reply(message)
49+
logger.info('Message posted on {}'.format(sub_id))
50+
logger.debug('Message content: \n {}'.format(message))
51+
message = ''
52+
except(praw.exceptions.APIException, UnboundLocalError):
53+
logger.debug('Submission {} has been skipped due to being blocked or missing text'.format(sub_id), exc_info=True)
4254
message = ''
4355
except:
44-
pass
56+
logger.error('Error occured!', exc_info=True)
4557
message = ''
4658

4759

4860
if __name__ == '__main__':
61+
logging.basicConfig(level=logging.WARN)
62+
logger = logging.getLogger(__name__)
63+
handler = logging.FileHandler('debug.log')
64+
handler.setLevel(logging.DEBUG)
65+
handler2 = logging.FileHandler('info.log')
66+
handler2.setLevel(logging.INFO)
67+
handler3 = logging.FileHandler('errors.log')
68+
handler3.setLevel(logging.WARN)
69+
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
70+
handler.setFormatter(formatter)
71+
handler2.setFormatter(formatter)
72+
handler3.setFormatter(formatter)
73+
logger.addHandler(handler)
74+
logger.addHandler(handler2)
75+
logger.addHandler(handler3)
4976
while True:
5077
try:
5178
action()
5279
except(KeyboardInterrupt):
5380
raise KeyboardInterrupt
5481
except:
55-
pass
82+
logger.critical('Error has occured when running main loop, please resolve asap', exc_info=True)

0 commit comments

Comments
 (0)