Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from django.conf import settings 

2from django.core.management.base import BaseCommand, CommandParser 

3 

4 

5class Command(BaseCommand): 

6 help = 'Simple Django log filter. ' \ 

7 'Finds match and then show the line contents following the matched string.' \ 

8 'Use grep for more complex regular expression matching.' 

9 

10 def add_arguments(self, parser: CommandParser): 

11 parser.add_argument('content', type=str) 

12 parser.add_argument('--file', type=str) 

13 

14 def handle(self, *args, **kwargs): 

15 content = kwargs['content'] 

16 file = kwargs['file'] or settings.LOGGING.get('handlers', {}).get('file', {}).get('filename', '') 

17 if not file: 

18 raise Exception("Specify --file or make sure settings.LOGGING['handlers']['file']['filename'] is set") 

19 

20 content_len = len(content) 

21 with open(file, 'rt') as fp: 

22 line = fp.readline() 

23 while line: 

24 i = line.find(content) 

25 if i >= 0: 

26 rest = line[i+content_len:].lstrip() 

27 self.stdout.write(rest) 

28 line = fp.readline()