Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. import os
  2. import time
  3. import logging
  4.  
  5. class RollingLogger(object):
  6. def __init__(self):
  7. self.NUMBER_OF_LOGS = 10
  8. self.initialize_logger_settings()
  9.  
  10. def initialize_logger_settings(self):
  11. """Set logger configuration settings"""
  12.  
  13. self.initialize_log_directory()
  14. logging.basicConfig(filename= self.path + self.filename,
  15. filemode='w',
  16. level=logging.INFO,
  17. format='%(asctime)s.%(msecs)03d,%(message)s',
  18. datefmt='%d-%b-%y,%H:%M:%S')
  19. logging.info('Successfully loaded logger configuration settings')
  20.  
  21. def initialize_log_directory(self):
  22. """Create directory and log file"""
  23.  
  24. self.path = 'logs/'
  25.  
  26. if not os.path.exists(self.path):
  27. os.makedirs(self.path)
  28. self.filename = 'log0000.log'
  29. else:
  30. self.filename = self.get_next_log_file_name()
  31.  
  32. def get_next_log_file_name(self):
  33. """Scans log directory for latest log file and returns a new filename"""
  34.  
  35. def extract_digits(filename):
  36. s = ''
  37. for char in filename:
  38. if char.isdigit():
  39. s += char
  40. return int(s)
  41.  
  42. l = [extract_digits(filename) for filename in os.listdir(self.path)]
  43. # Directory is empty
  44. if not l:
  45. return 'log0000.log'
  46. # Directory has files so find latest
  47. else:
  48. latest_file_number = max(l)
  49. return 'log' + '{0:04d}'.format(latest_file_number + 1) + '.log'
  50.  
  51. logger = RollingLogger()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement