Package list logbook / 230271b
New upstream version 1.4.1 IƱaki Malerba 2 years ago
5 changed file(s) with 56 addition(s) and 24 deletion(s). Raw diff Collapse all Expand all
11 =================
22
33 Here you can see the full list of changes between each Logbook release.
4
5 Version 1.4.1
6
7 Released on October 14th, 2018
8
9 - Fixed deprecated regular expression pattern (thanks Tsuyoshi Hombashi)
10 - Fixed TimedRotatingFileHandler rotation (thanks Tucker Beck)
411
512 Version 1.4.0
613 -------------
0 __version__ = "1.4.0"
0 __version__ = "1.4.1"
900900 self.timed_filename_for_current = timed_filename_for_current
901901
902902 self._timestamp = self._get_timestamp(_datetime_factory())
903 timed_filename = self.generate_timed_filename(self._timestamp)
904
905903 if self.timed_filename_for_current:
906 filename = timed_filename
904 filename = self.generate_timed_filename(self._timestamp)
905 elif os.path.exists(filename):
906 self._timestamp = self._get_timestamp(
907 datetime.fromtimestamp(
908 os.stat(filename).st_mtime
909 )
910 )
907911
908912 FileHandler.__init__(self, filename, mode, encoding, level,
909913 format_string, True, filter, bubble)
931935 """
932936 directory = os.path.dirname(self._filename)
933937 files = []
938 rollover_regex = re.compile(self.rollover_format.format(
939 basename=re.escape(self.basename),
940 timestamp='.+',
941 ext=re.escape(self.ext),
942 ))
934943 for filename in os.listdir(directory):
935944 filename = os.path.join(directory, filename)
936 regex = self.rollover_format.format(
937 basename=re.escape(self.basename),
938 timestamp='.+',
939 ext=re.escape(self.ext),
940 )
941 if re.match(regex, filename):
945 if rollover_regex.match(filename):
942946 files.append((os.path.getmtime(filename), filename))
943947 files.sort()
944948 if self.backup_count > 1:
950954 if self.stream is not None:
951955 self.stream.close()
952956
957 if (
958 not self.timed_filename_for_current
959 and os.path.exists(self._filename)
960 ):
961 filename = self.generate_timed_filename(self._timestamp)
962 os.rename(self._filename, filename)
963
953964 if self.backup_count > 0:
954965 for time, filename in self.files_to_delete():
955966 os.remove(filename)
956967
957968 if self.timed_filename_for_current:
958969 self._filename = self.generate_timed_filename(new_timestamp)
959 else:
960 filename = self.generate_timed_filename(self._timestamp)
961 os.rename(self._filename, filename)
962970 self._timestamp = new_timestamp
963971
964972 self._open('w')
10531061 def has_debugs(self):
10541062 """`True` if any :data:`DEBUG` records were found."""
10551063 return any(r.level == DEBUG for r in self.records)
1056
1064
10571065 @property
10581066 def has_traces(self):
10591067 """`True` if any :data:`TRACE` records were found."""
11061114 """
11071115 kwargs['level'] = DEBUG
11081116 return self._test_for(*args, **kwargs)
1109
1117
11101118 def has_trace(self, *args, **kwargs):
11111119 """`True` if a specific :data:`TRACE` log record exists.
11121120
3737 else:
3838 from urllib.parse import parse_qsl, urlencode
3939
40 _ws_re = re.compile(r'(\s+)(?u)')
40 _ws_re = re.compile(r'(\s+)', re.UNICODE)
4141 TWITTER_FORMAT_STRING = u(
4242 '[{record.channel}] {record.level_name}: {record.message}')
4343 TWITTER_ACCESS_TOKEN_URL = 'https://twitter.com/oauth/access_token'
00 import os
11 import pytest
2 import time
23 from datetime import datetime
34
45 import logbook
166167 assert f.readline().rstrip() == '[01:00] Third One'
167168 assert f.readline().rstrip() == '[02:00] Third One'
168169
170
169171 @pytest.mark.parametrize("backup_count", [1, 3])
170 def test_timed_rotating_file_handler__not_timed_filename_for_current(tmpdir, activation_strategy, backup_count):
172 @pytest.mark.parametrize("preexisting_file", [True, False])
173 def test_timed_rotating_file_handler__not_timed_filename_for_current(
174 tmpdir, activation_strategy, backup_count, preexisting_file
175 ):
171176 basename = str(tmpdir.join('trot.log'))
177
178 if preexisting_file:
179 with open(basename, 'w') as file:
180 file.write('contents')
181 jan_first = time.mktime(datetime(2010, 1, 1).timetuple())
182 os.utime(basename, (jan_first, jan_first))
183
172184 handler = logbook.TimedRotatingFileHandler(
173 basename, backup_count=backup_count,
185 basename,
186 format_string='[{record.time:%H:%M}] {record.message}',
187 backup_count=backup_count,
174188 rollover_format='{basename}{ext}.{timestamp}',
175189 timed_filename_for_current=False,
176190 )
177 handler._timestamp = handler._get_timestamp(datetime(2010, 1, 5))
178 handler.format_string = '[{record.time:%H:%M}] {record.message}'
179191
180192 def fake_record(message, year, month, day, hour=0,
181193 minute=0, second=0):
194206 for x in xrange(20):
195207 handler.handle(fake_record('Last One', 2010, 1, 8, x + 1))
196208
197 files = sorted(x for x in os.listdir(str(tmpdir)) if x.startswith('trot'))
198
199 assert files == ['trot.log'] + ['trot.log.2010-01-0{0}'.format(i)
200 for i in xrange(5, 8)][-backup_count:]
209 computed_files = [x for x in os.listdir(str(tmpdir)) if x.startswith('trot')]
210
211 expected_files = ['trot.log.2010-01-01'] if preexisting_file else []
212 expected_files += ['trot.log.2010-01-0{0}'.format(i) for i in xrange(5, 8)]
213 expected_files += ['trot.log']
214 expected_files = expected_files[-backup_count:]
215
216 assert sorted(computed_files) == sorted(expected_files)
217
201218 with open(str(tmpdir.join('trot.log'))) as f:
202219 assert f.readline().rstrip() == '[01:00] Last One'
203220 assert f.readline().rstrip() == '[02:00] Last One'