Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import logging, os, re, time, datetime
- import multiprocessing
- import sys
- import inspect
- import os
- import time
- from stat import *
- from logging.handlers import TimedRotatingFileHandler
- try:
- import codecs
- except ImportError:
- codecs = None
- _MIDNIGHT = 24 * 60 * 60 # number of seconds in a day
- class MyLoggerHandler(logging.FileHandler):
- def __init__(self, filename, when='D', interval=1, backupCount=0, encoding=None, delay=False):
- self.prefix = filename
- self.when = when.upper()
- # S - Every second a new file
- # M - Every minute a new file
- # H - Every hour a new file
- # D - Every day a new file
- if self.when == 'S':
- self.interval = 1 # one second
- self.suffix = "%Y-%m-%d_%H-%M-%S"
- self.extMatch = r"^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}$"
- elif self.when == 'M':
- self.interval = 60 # one minute
- self.suffix = "%Y-%m-%d_%H-%M"
- self.extMatch = r"^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}$"
- elif self.when == 'H':
- self.interval = 60 * 60 # one hour
- self.suffix = "%Y-%m-%d_%H"
- self.extMatch = r"^\d{4}-\d{2}-\d{2}_\d{2}$"
- elif self.when == 'D' or self.when == 'MIDNIGHT':
- self.interval = 60 * 60 * 24 # one day
- self.suffix = "%Y-%m-%d"
- self.extMatch = r"^\d{4}-\d{2}-\d{2}$"
- elif self.when.startswith('W'):
- self.interval = 60 * 60 * 24 * 7 # one week
- if len(self.when) != 2:
- raise ValueError("You must specify a day for weekly rollover from 0 to 6 (0 is Monday): %s" % self.when)
- if self.when[1] < '0' or self.when[1] > '6':
- raise ValueError("Invalid day specified for weekly rollover: %s" % self.when)
- self.dayOfWeek = int(self.when[1])
- self.suffix = "%Y-%m-%d"
- self.extMatch = r"^\d{4}-\d{2}-\d{2}$"
- else:
- raise ValueError("Invalid rollover interval specified: %s" % self.when)
- self.interval = self.interval * interval # multiply by units requested
- self.filefmt = "%s.%s" % (self.prefix, self.suffix)
- # print "### self.filefmt = %s" % self.filefmt
- self.filePath = datetime.datetime.now().strftime(self.filefmt)
- _dir = os.path.dirname(self.filePath)
- try:
- if os.path.exists(_dir) is False:
- os.makedirs(_dir)
- except Exception:
- print("can not make dirs")
- print("filepath is " + self.filePath)
- pass
- self.backupCount = backupCount
- if codecs is None:
- encoding = None
- logging.FileHandler.__init__(self, self.filePath, 'a', encoding, delay)
- if os.path.exists(filename):
- t = os.stat(filename)[ST_MTIME]
- else:
- t = int(time.time())
- self.rolloverAt = self.computeRollover(t)
- #print "t = %d" % t
- #print "self.rolloverAt = %d" % self.rolloverAt
- def computeRollover(self, currentTime):
- """
- Work out the rollover time based on the specified time.
- """
- result = currentTime + self.interval
- # If we are rolling over at midnight or weekly, then the interval is already known.
- # What we need to figure out is WHEN the next interval is. In other words,
- # if you are rolling over at midnight, then your base interval is 1 day,
- # but you want to start that one day clock at midnight, not now. So, we
- # have to fudge the rolloverAt value in order to trigger the first rollover
- # at the right time. After that, the regular interval will take care of
- # the rest. Note that this code doesn't care about leap seconds. :)
- if self.when == 'MIDNIGHT' or self.when.startswith('W'):
- # This could be done with less code, but I wanted it to be clear
- # if self.utc:
- # t = time.gmtime(currentTime)
- # else:
- t = time.localtime(currentTime)
- currentHour = t[3]
- currentMinute = t[4]
- currentSecond = t[5]
- # r is the number of seconds left between now and midnight
- r = _MIDNIGHT - ((currentHour * 60 + currentMinute) * 60 +
- currentSecond)
- result = currentTime + r
- # If we are rolling over on a certain day, add in the number of days until
- # the next rollover, but offset by 1 since we just calculated the time
- # until the next day starts. There are three cases:
- # Case 1) The day to rollover is today; in this case, do nothing
- # Case 2) The day to rollover is further in the interval (i.e., today is
- # day 2 (Wednesday) and rollover is on day 6 (Sunday). Days to
- # next rollover is simply 6 - 2 - 1, or 3.
- # Case 3) The day to rollover is behind us in the interval (i.e., today
- # is day 5 (Saturday) and rollover is on day 3 (Thursday).
- # Days to rollover is 6 - 5 + 3, or 4. In this case, it's the
- # number of days left in the current week (1) plus the number
- # of days in the next week until the rollover day (3).
- # The calculations described in 2) and 3) above need to have a day added.
- # This is because the above time calculation takes us to midnight on this
- # day, i.e. the start of the next day.
- if self.when.startswith('W'):
- day = t[6] # 0 is Monday
- if day != self.dayOfWeek:
- if day < self.dayOfWeek:
- daysToWait = self.dayOfWeek - day
- else:
- daysToWait = 6 - day + self.dayOfWeek + 1
- newRolloverAt = result + (daysToWait * (60 * 60 * 24))
- if not self.utc:
- dstNow = t[-1]
- dstAtRollover = time.localtime(newRolloverAt)[-1]
- if dstNow != dstAtRollover:
- if not dstNow: # DST kicks in before next rollover, so we need to deduct an hour
- addend = -3600
- else: # DST bows out before next rollover, so we need to add an hour
- addend = 3600
- newRolloverAt += addend
- result = newRolloverAt
- return result
- def shouldRollover(self):
- """
- Determine if rollover should occur.
- record is not used, as we are just comparing times, but it is needed so
- the method signatures are the same
- """
- #_filePath = datetime.datetime.now().strftime(self.filefmt)
- #print "_filePath = %s" % _filePath
- self.filePath = time.strftime(self.filefmt, time.localtime(self.rolloverAt))
- #print "self.filePath = %s" % self.filePath
- t = int(time.time())
- if t >= self.rolloverAt:
- return 1
- #print "No need to rollover: %d, %d" % (t, self.rolloverAt)
- return 0
- def doChangeFile(self):
- self.baseFilename = os.path.abspath(self.filePath)
- if self.stream is not None:
- self.stream.flush()
- self.stream.close()
- # if sys.version_info < (2, 7, 6):
- # # Do stuff for old version...
- # self.stream = self._open()
- # else:
- if not self.delay:
- self.stream = self._open()
- if self.backupCount > 0:
- for s in self.getFilesToDelete():
- os.remove(s)
- def getFilesToDelete(self):
- dirName, baseName = os.path.split(self.baseFilename)
- #print dirName, baseName
- fileNames = os.listdir(dirName)
- #print("fileNames: ", fileNames)
- #print("self.extMatch: ", self.extMatch)
- result = []
- prefix = self.prefix + "."
- #print prefix
- tag = dirName + '/'
- prefix = prefix.split(tag)[1]
- # print prefix
- plen = len(prefix)
- # print plen
- for fileName in fileNames:
- # print fileName[:plen]
- if fileName[:plen] == prefix:
- suffix = fileName[plen:]
- # print suffix
- if re.compile(self.extMatch).match(suffix):
- result.append(os.path.join(dirName, fileName))
- result.sort()
- # print(result)
- if len(result) < self.backupCount:
- result = []
- else:
- result = result[:len(result) - self.backupCount]
- return result
- def emit(self, record):
- """
- Emit a record.
- """
- try:
- # if self.shouldChangeFileToWrite():
- if self.shouldRollover():
- self.doChangeFile()
- self.rolloverAt = self.rolloverAt + self.interval
- logging.FileHandler.emit(self, record)
- except (KeyboardInterrupt, SystemExit):
- raise
- except:
- self.handleError(record)
- class log_helper():
- def __init__(self, filename, logger):
- logdir = os.getcwd()+"/"+"logs"
- if not os.path.exists(logdir):
- os.mkdir(logdir)
- filename = logdir+"/"+filename
- lh = MyLoggerHandler(filename, when='MIDNIGHT',interval=1, backupCount=30)
- # lh = MyLoggerHandler(filename, when='M',interval=2, backupCount=5)
- # lh = logging.handlers.TimedRotatingFileHandler(filename, when='M',interval=1) # , backupCount=5)
- fm = logging.Formatter('%(asctime)s %(filename)s(%(lineno)d) %(levelname)s %(name)s - %(message)s')
- lh.setFormatter(fm)
- self.logger = logging.getLogger(logger)
- self.logger.addHandler(lh)
- self.logger.setLevel(logging.DEBUG)
- console = logging.StreamHandler()
- console.setLevel(logging.DEBUG)
- console.setFormatter(fm)
- logging.getLogger(logger).addHandler(console)
- self.debug_lock = multiprocessing.Lock()
- self.info_lock = multiprocessing.Lock()
- self.warn_lock = multiprocessing.Lock()
- self.error_lock = multiprocessing.Lock()
- self.critical_lock = multiprocessing.Lock()
- def debug(self, message):
- with self.debug_lock:
- frame, filename, line, module, code, unknow = inspect.stack()[1]
- message = "%s(%d) - %s" % (os.path.basename(filename), line, message)
- self.logger.debug(message)
- def info(self, message):
- with self.info_lock:
- frame, filename, line, module, code, unknow = inspect.stack()[1]
- message = "%s(%d) - %s" % (os.path.basename(filename), line, message)
- self.logger.info(message)
- def warn(self, message):
- with self.warn_lock:
- frame, filename, line, module, code, unknow = inspect.stack()[1]
- message = "%s(%d) - %s" % (os.path.basename(filename), line, message)
- self.logger.warn(message)
- def error(self, message):
- with self.error_lock:
- frame, filename, line, module, code, unknow = inspect.stack()[1]
- message = "%s(%d) - %s" % (os.path.basename(filename), line, message)
- self.logger.error(message)
- def critical(self, message):
- with self.critical_lock:
- frame, filename, line, module, code, unknow = inspect.stack()[1]
- message = "%s(%d) - %s" % (os.path.basename(filename), line, message)
- self.logger.critical(message)
- class Logger:
- def __init__(self):
- self.logger = log_helper("active3.log","active3")
- def AddLoghandle(filename):
- return log_helper(filename)
- #lock = multiprocessing.Lock()
- #with lock:
- log = Logger()
- def worker():
- i = 0
- while i < int(sys.argv[2]):
- ss = "Hello world " + ' ' + str(os.getpid()) + ' ' +str(i)
- log.logger.info(ss)
- log.logger.debug(ss)
- i += 1
- # log.logger.warn(ss)
- # time.sleep(1)
- # log.logger.error(ss)
- # time.sleep(1)
- while True:
- pass
- #
- if __name__ == "__main__":
- processnum = int(sys.argv[1])
- record = []
- for i in range(processnum):
- print("1111111111111, index = %d" % i)
- process = multiprocessing.Process(target=worker)
- process.start()
- record.append(process)
- for process in record:
- process.join()
- while True:
- pass
Add Comment
Please, Sign In to add comment