def __init__(self, filename, mode='w', when='H', prefix=True, utc=True, encoding=None, delay=0):
        """
        Open a file and use it as a stream for logging.
    
        Open a new file at specified times.
    
        By default, the time is used as a prefix on the output filename.
    
        Parameters
        ----------
    
        Returns
        -------
    
        """
        BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
        self.when = when
        self.prefix = prefix
        self.utc = utc

        if self.when == 'S':
            self.interval = 1
            self.tstring = '%Y%m%d_%H%M%S' #TODO allow optional tstr arg?
        elif self.when == 'M':
            self.interval = 60;
            self.tstring = '%Y%m%d_%H%M'
        elif self.when == 'H':
            self.interval = 60 * 60;
            self.tstring = '%Y%m%d_%H%M' # still keep minutes
         elif self.when == 'D':
            self.interval = 24 * 60 * 60;
            self.tstring = '%Y%M%D'
        else:
            raise ValueError("Invalid rollover interval specified: %s" % self.when)
        self.openNewFile(time.time())
Esempio n. 2
0
    def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False):
        BaseRotatingHandler.__init__(self, filename, 'a', encoding, delay)
        self.when = when.upper()
        self.backupCount = backupCount
        self.utc = utc
        if  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 == '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.extMatch = re.compile(self.extMatch)
        self.interval = self.interval * interval # multiply by units requested
        if os.path.exists(filename):
            t = os.stat(filename)[ST_MTIME]
        else:
            t = int(time.time())
        self.rolloverAt = self.computeRollover(t)
Esempio n. 3
0
    def __init__(self,
                 filename,
                 lockfile='',
                 mode='a',
                 maxBytes=0,
                 backupCount=0,
                 encoding=None,
                 delay=0,
                 debug=True):
        filepath = os.path.abspath(filename)

        # Initially, if we are in degrade mode, process the filepath
        if self.isDegradeMode: filepath = self._getDegradeFilename(filepath)

        BaseRotatingHandler.__init__(self, filepath, mode, encoding, delay)

        self._rotateFailed = False
        self.maxBytes = maxBytes
        self.backupCount = backupCount

        self._open_lockfile(lockfile)

        if debug: self._degrade = self._degrade_debug

        if self.isDegradeMode:
            self._delete_logfiles(
            )  # Perform log cleaning action every time a new process is initialized
Esempio n. 4
0
 def __init__(self, filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=0):
     # if maxBytes > 0:
     #    mode = 'a'
     BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
     self.maxBytes = maxBytes
     self.backupCount = backupCount
     self.placeholder = str(len(str(backupCount)))
Esempio n. 5
0
    def __init__(self,
                 filename,
                 mode='a',
                 timestring_format="%Y%m%d_%H%M%S",
                 prefix=True,
                 encoding=None):
        '''Open the specified file and use it as the stream for logging.

        File grows indefinitely, until rollover is called.

        A rollover will close the stream; rename the file to a new name, 
        with a prefix or suffix (prefix if prameter prefix=True)
        to the filename of the current date and time, in the format specified 
        by timestring_format; and open a fresh log file.
        
        For example, with a base file name of "app.log", and other arguments 
        as default, a rolled-over logfile might have a name of
        "20090610_234620.app.log".
        
        The file being written to is always "app.log".
        
        timestring_format is a format string, as described for time.strftime().
        '''
        BaseRotatingHandler.__init__(self, filename, mode, encoding)
        self.timestring_format = timestring_format
        self.prefix = prefix
Esempio n. 6
0
File: log4.py Progetto: mahabara/RTP
    def __init__(self, filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=0):
        """
        Open the specified file and use it as the stream for logging.

        By default, the file grows indefinitely. You can specify particular
        values of maxBytes and backupCount to allow the file to rollover at
        a predetermined size.

        Rollover occurs whenever the current log file is nearly maxBytes in
        length. If backupCount is >= 1, the system will successively create
        new files with the same pathname as the base file, but with extensions
        ".1", ".2" etc. appended to it. For example, with a backupCount of 5
        and a base file name of "app.log", you would get "app.log",
        "app.log.1", "app.log.2", ... through to "app.log.5". The file being
        written to is always "app.log" - when it gets filled up, it is closed
        and renamed to "app.log.1", and if files "app.log.1", "app.log.2" etc.
        exist, then they are renamed to "app.log.2", "app.log.3" etc.
        respectively.

        If maxBytes is zero, rollover never occurs.
        """
        # If rotation/rollover is wanted, it doesn't make sense to use another
        # mode. If for example 'w' were specified, then if there were multiple
        # runs of the calling application, the logs from previous runs would be
        # lost if the 'w' is respected, because the log file would be truncated
        # on each run.
        if maxBytes > 0:
            mode = 'a'
        BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
        self.maxBytes = maxBytes
        self.backupCount = backupCount
        self.baseFilename = self.baseFilename.split('+')[0]+'+'+datetime.datetime.now().strftime('%Y-%m-%d__%H-%M-%S-%f')+'.txt'
Esempio n. 7
0
File: log.py Progetto: wxhzk/mylib
 def __init__(self, filename, maxbytes=2*1024, mode='a', encoding=None, delay=0):
     self.mode = mode
     self.index = 0
     self.maxBytes = maxbytes
     self.createdate = datetime.datetime.now().strftime("%Y%m%d")
     if DEBUG: self.createdate = DATESTR
     BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
Esempio n. 8
0
    def __init__(self, filename, mode='a', maxBytes=0, backupCount=0,
                 encoding=None, debug=True, supress_abs_warn=False):
        try:
            BaseRotatingHandler.__init__(self, filename, mode, encoding)
        except TypeError: # Due to a different logging release without encoding support  (Python 2.4.1 and earlier?)
            BaseRotatingHandler.__init__(self, filename, mode)
            self.encoding = encoding

        self._rotateFailed = False
        self.maxBytes = maxBytes
        self.backupCount = backupCount
        # Prevent multiple extensions on the lock file (Only handles the normal "*.log" case.)
        lock_dir = os.path.join(os.path.dirname(filename), '.lock')
        if not os.path.isdir(lock_dir):
            create_dir(lock_dir)
        log_filename = os.path.basename(filename)
        if log_filename.endswith(".log"):
            lock_file = os.path.join(lock_dir, log_filename[:-4])
        else:
            lock_file = os.path.join(lock_dir, log_filename)
        self.stream_lock = open(lock_file + ".lock", "w")

        # For debug mode, swap out the "_degrade()" method with a more a verbose one.
        if debug:
            self._degrade = self._degrade_debug
Esempio n. 9
0
 def __init__(
     self, filename, mode="a", max_bytes=0, backup_count=0, encoding=None, delay=0
 ):
     BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
     self.max_bytes = max_bytes
     self.backup_count = backup_count
     self.placeholder = str(len(str(backup_count)))
Esempio n. 10
0
 def __init__(self, filename, max_bytes=0, backup_count=0, when='midnight', interval=1,
              encoding=None, delay=False, utc=False):
     BaseRotatingHandler.__init__(self, filename, 'a', encoding, delay)
     self.mid_night = 24 * 60 * 60
     self.max_bytes = max_bytes
     self.when = when.upper()
     self.backupCount = backup_count
     self.utc = utc
     if self.when == 'S':
         self.interval = 1
     elif self.when == 'M':
         self.interval = 60
     elif self.when == 'H':
         self.interval = 60 * 60
     elif self.when == 'D' or self.when == 'MIDNIGHT':
         self.interval = 60 * 60 * 24
     elif self.when.startswith('W'):
         self.interval = 60 * 60 * 24 * 7
         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])
     else:
         raise ValueError("Invalid rollover interval specified: %s" % self.when)
     self.suffix = "%Y%m%d%H%M%S%f"
     self.extMatch = re.compile(r"^\d{4}\d{2}\d{2}\d{2}\d{2}\d{2}\d{6}$")
     self.interval *= interval
     if os.path.exists(filename):
         t = os.stat(filename)[ST_MTIME]
     else:
         t = int(time.time())
     self.rolloverAt = self.computeRollover(t)
Esempio n. 11
0
    def __init__(self, filename, when, encoding=None, delay=0):
        self.when = string.upper(when)
        self.fname_pat = filename
        self.mock_dt = None

        self.computeNextRollover()

        BaseRotatingHandler.__init__(self, self.filename, 'a', encoding, delay)
    def __init__(self,
                 filename,
                 mode='a',
                 maxBytes=0,
                 backupCount=0,
                 encoding=None,
                 debug=True,
                 delay=0):
        """
        Open the specified file and use it as the stream for logging.

        By default, the file grows indefinitely. You can specify particular
        values of maxBytes and backupCount to allow the file to rollover at
        a predetermined size.

        Rollover occurs whenever the current log file is nearly maxBytes in
        length. If backupCount is >= 1, the system will successively create
        new files with the same pathname as the base file, but with extensions
        ".1", ".2" etc. appended to it. For example, with a backupCount of 5
        and a base file name of "app.log", you would get "app.log",
        "app.log.1", "app.log.2", ... through to "app.log.5". The file being
        written to is always "app.log" - when it gets filled up, it is closed
        and renamed to "app.log.1", and if files "app.log.1", "app.log.2" etc.
        exist, then they are renamed to "app.log.2", "app.log.3" etc.
        respectively.

        If maxBytes is zero, rollover never occurs.

        On Windows, it is not possible to rename a file that is currently opened
        by another process.  This means that it is not possible to rotate the
        log files if multiple processes is using the same log file.  In this
        case, the current log file will continue to grow until the rotation can
        be completed successfully.  In order for rotation to be possible, all of
        the other processes need to close the file first.  A mechanism, called
        "degraded" mode, has been created for this scenario.  In degraded mode,
        the log file is closed after each log message is written.  So once all
        processes have entered degraded mode, the net rotation attempt should
        be successful and then normal logging can be resumed.  Using the 'delay'
        parameter may help reduce contention in some usage patterns.

        This log handler assumes that all concurrent processes logging to a
        single file will are using only this class, and that the exact same
        parameters are provided to each instance of this class.  If, for
        example, two different processes are using this class, but with
        different values for 'maxBytes' or 'backupCount', then odd behavior is
        expected. The same is true if this class is used by one application, but
        the RotatingFileHandler is used by another.
        """
        # Absolute file name handling done by FileHandler since Python 2.5
        BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
        self.delay = delay
        self._rotateFailed = False
        self.maxBytes = maxBytes
        self.backupCount = backupCount
        self._open_lockfile()
        # For debug mode, swap out the "_degrade()" method with a more a verbose one.
        if debug:
            self._degrade = self._degrade_debug
Esempio n. 13
0
    def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False, atTime=None):
        self.when = when.upper()
        self.backupCount = backupCount
        self.utc = utc
        self.atTime = atTime
        # Calculate the real rollover interval, which is just the number of
        # seconds between rollovers.  Also set the filename suffix used when
        # a rollover occurs.  Current 'when' events supported:
        # S - Seconds
        # M - Minutes
        # H - Hours
        # D - Days
        # midnight - roll over at midnight
        # W{0-6} - roll over on a certain day; 0 - Monday
        #
        # Case of the 'when' specifier is not important; lower or upper case
        # will work.
        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}(\.\w+)?$"
        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}(\.\w+)?$"
        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}(\.\w+)?$"
        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}(\.\w+)?$"
        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}(\.\w+)?$"
        else:
            raise ValueError("Invalid rollover interval specified: %s" % self.when)

        self.extMatch = re.compile(self.extMatch, re.ASCII)
        self.interval = self.interval * interval  # multiply by units requested

        BaseRotatingHandler.__init__(self, filename, 'a', encoding, delay)
        filename = self.baseFilename
        if os.path.exists(filename):
            t = os.stat(filename)[ST_MTIME]
        else:
            t = int(time.time())
        self.rolloverAt = self.computeRollover(t)

        ConcurrentLock.__init__(self, filename, 'a', encoding, delay)
Esempio n. 14
0
 def __init__(self, pathformat="logs/%Y/%m/%Y-%m-%d.log", utc=False, encoding=None, delay=False):
     self.path_format = pathformat
     self.utc = utc
     current_time = self._get_time()
     current_file = self._format_time(current_time)
     self._current_day = self._get_days_since_epoch(current_time)
     self._create_dirs(resource_filename('logs', current_file))
     BaseRotatingHandler.__init__(self, filename=resource_filename('logs', current_file), mode="a",
                                  encoding=encoding, delay=delay)
Esempio n. 15
0
    def __init__(self, filename, rotatefilename, when='h', interval=1,
                 backupCount=0, encoding=None, delay=False, utc=False,
                 atTime=None, errors=None):

        # create directory
        rotatedirName, rotatebaseName = os.path.split(rotatefilename)
        os.makedirs(rotatedirName, exist_ok=True)
        dirName, baseName = os.path.split(filename)
        os.makedirs(dirName, exist_ok=True)

        BaseRotatingHandler.__init__(self, filename, 'a', encoding=encoding,
                                     delay=delay, errors=errors)
        self.rotatefilename = os.fspath(rotatefilename)
        self.when = when.upper()
        self.backupCount = backupCount
        self.utc = utc
        self.atTime = atTime
        # Calculate the real rollover interval, which is just the number of
        # seconds between rollovers.  Also set the filename suffix used when
        # a rollover occurs.  Current 'when' events supported:
        # S - Seconds
        # M - Minutes
        # H - Hours
        # D - Days
        # midnight - roll over at midnight
        # W{0-6} - roll over on a certain day; 0 - Monday
        #
        # Case of the 'when' specifier is not important; lower or upper case
        # will work.
        if self.when == 'S':
            self.interval = 1  # one second
        elif self.when == 'M':
            self.interval = 60  # one minute
        elif self.when == 'H':
            self.interval = 60 * 60  # one hour
        elif self.when == 'D' or self.when == 'MIDNIGHT':
            self.interval = 60 * 60 * 24  # one day
        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])
        else:
            raise ValueError("Invalid rollover interval specified: %s" % self.when)

        self.rotatedmatch = timeformat_to_regex(rotatebaseName)
        self.interval = self.interval * interval  # multiply by units requested
        # The following line added because the filename passed in could be a
        # path object (see Issue #27493), but self.baseFilename will be a string
        # filename = self.baseFilename
        if os.path.exists(filename):
            t = os.stat(filename)[ST_MTIME]
        else:
            t = int(time.time())
        self.rolloverAt = self.computeRollover(t)
Esempio n. 16
0
 def __init__(self, filename, mode='a', maxBytes=0, backupCount=0,
              encoding=None, delay=0):
     BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
     self.delay = delay
     self._exception = False
     self._open_lockfile()
     self._chmod(filename, mode=const.log_file_mode)
     self.maxBytes = maxBytes
     self.backupCount = backupCount
Esempio n. 17
0
 def __init__(self,
              filename,
              mode='a',
              maxBytes=1024 * 1024 * 50,
              encoding=None,
              delay=0):
     self.curCount = 0
     self.created = getNowDate()
     BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
     self.maxBytes = maxBytes
Esempio n. 18
0
 def __init__(self, filename, mode='a', max_bytes=256, backup_count=5, encoding=None, cmp_type='gzip', cmp_level=9, suffixes='gz'):
     if max_bytes > 0:
         mode = 'a'
     BaseRotatingHandler.__init__(self, filename, mode, encoding)
     self.filename = filename
     self.backup_count = backup_count
     self.max_bytes = max_bytes
     self.cmp_type = cmp_type
     self.cmp_level = cmp_level
     self.suffixes = suffixes
Esempio n. 19
0
    def __init__(self, filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=None):

        self.maxBytes = maxBytes
        self.backupCount = backupCount

        # Construct the handler with the given arguments in "delayed" mode
        # because we will handle opening the file as needed. File name
        # handling is done by FileHandler since Python 2.5.
        BaseRotatingHandler.__init__(self, filename, mode, encoding=encoding, delay=True)
        ConcurrentLock.__init__(self, filename, mode, encoding=encoding, delay=True)
Esempio n. 20
0
    def __init__(self,
                 filename,
                 maxDays=1,
                 minDays=0,
                 maxMBytes=10,
                 backupCount=5,
                 compression=None):
        """
        Initialize the Handler.  We assume the following:

            1. Interval is in days
            2. No special encoding
            3. No delays are set
            4. Timestamps are not in UTC

        @type filename: string
        @param filename: The full path of the log file
        @type interval: int
        @param interval: Number of days before file rotation
        @type maxMBytes: int
        @param maxMBytes: Maximum size of the logfile in MB before file rotation
        @type backupCount: int
        @param backupCount: Number of backups to keep

        """
        # Make dirs if logging directory does not exist
        if not os.path.exists(os.path.dirname(filename)):
            os.makedirs(os.path.dirname(filename))

        self.compression = ''
        try:
            if compression.lower() in COMPRESSION_SUPPORTED:
                self.compression = compression.lower()
        except AttributeError:
            pass
        # bz2 compression can be implementes with encoding='bz2-codec' in BaseRotatingHandler
        mode = 'a'
        BaseRotatingHandler.__init__(self, filename, mode, encoding=None)
        self.backupCount = backupCount
        self.maxBytes = maxMBytes * 1024.0 * 1024.0  # Convert the MB to bytes as needed by the base class
        self.min_lifetime = minDays * 24 * 60 * 60  # Convert min days to seconds
        self.interval = maxDays * 24 * 60 * 60  # Convert max days (interval) to seconds

        # We are enforcing a date/time format for rotated log files to include
        # year, month, day, hours, and minutes.  The hours and minutes are to
        # preserve multiple rotations in a single day.
        self.suffix = "%Y-%m-%d_%H-%M"
        self.extMatch = re.compile(
            r"^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}(\.gz|\.zip)?$")

        if os.path.exists(filename):
            begin_interval_time = os.stat(filename)[stat.ST_MTIME]
        else:
            begin_interval_time = int(time.time())
        self.rolloverAt = begin_interval_time + self.interval
Esempio n. 21
0
 def __init__(self,
              filename,
              mode='a',
              maxBytes=0,
              backupCount=0,
              encoding=None):
     BaseRotatingHandler.__init__(self, filename, mode, encoding)
     self.maxBytes = maxBytes
     self.backupCount = backupCount
     head, tail = os.path.split(filename)
     self.stream_lock = open("{}/.{}.lock".format(head, tail), "w")
 def __init__(self,
              filename,
              encoding=None,
              delay=False,
              utc=False,
              **kwargs):
     self.utc = utc
     self.suffix = "%Y-%m-%d_%H-%M.log"
     self.baseFilename = filename
     self.currentFileName = self._compute_fn()
     BaseRotatingHandler.__init__(self, filename, 'a', encoding, delay)
Esempio n. 23
0
 def __init__(self,
              filename,
              encoding=None,
              delay=False,
              utc=False,
              **kwargs):
     self.utc = utc
     self.suffix = "%Y%m%d"
     self.baseFilename = os.path.abspath(filename)
     self.currentFileName = self._compute_fn()
     BaseRotatingHandler.__init__(self, filename, 'a', encoding, delay)
 def __init__(self, prefix_name, backup_count=0, encoding=None, delay=False, utc=False):
     self.suffix_name_ = "%Y%m%d.log"
     self.prefix_name_= prefix_name
     self.utc_ = utc
     cur_file_name = self._current_log_filename()
     self.backup_count_ = backup_count
     BaseRotatingHandler.__init__(self, cur_file_name, 'a', encoding, delay)
     
     self.interval_ = 60 * 60 * 24 # one day
     self.ext_match_ = re.compile(r"^\d{4}\d{2}\d{2}$")
     
     self.rollover_at_ = self.compute_rollover()
Esempio n. 25
0
    def __init__(self, filename, mode='a', maxBytes=0, backupCount=0,
                 encoding=None, debug=True, delay=0):
        """
        Open the specified file and use it as the stream for logging.

        By default, the file grows indefinitely. You can specify particular
        values of maxBytes and backupCount to allow the file to rollover at
        a predetermined size.

        Rollover occurs whenever the current log file is nearly maxBytes in
        length. If backupCount is >= 1, the system will successively create
        new files with the same pathname as the base file, but with extensions
        ".1", ".2" etc. appended to it. For example, with a backupCount of 5
        and a base file name of "app.log", you would get "app.log",
        "app.log.1", "app.log.2", ... through to "app.log.5". The file being
        written to is always "app.log" - when it gets filled up, it is closed
        and renamed to "app.log.1", and if files "app.log.1", "app.log.2" etc.
        exist, then they are renamed to "app.log.2", "app.log.3" etc.
        respectively.

        If maxBytes is zero, rollover never occurs.

        On Windows, it is not possible to rename a file that is currently opened
        by another process.  This means that it is not possible to rotate the
        log files if multiple processes is using the same log file.  In this
        case, the current log file will continue to grow until the rotation can
        be completed successfully.  In order for rotation to be possible, all of
        the other processes need to close the file first.  A mechanism, called
        "degraded" mode, has been created for this scenario.  In degraded mode,
        the log file is closed after each log message is written.  So once all
        processes have entered degraded mode, the net rotation attempt should
        be successful and then normal logging can be resumed.  Using the 'delay'
        parameter may help reduce contention in some usage patterns.

        This log handler assumes that all concurrent processes logging to a
        single file will are using only this class, and that the exact same
        parameters are provided to each instance of this class.  If, for
        example, two different processes are using this class, but with
        different values for 'maxBytes' or 'backupCount', then odd behavior is
        expected. The same is true if this class is used by one application, but
        the RotatingFileHandler is used by another.
        """
        # Absolute file name handling done by FileHandler since Python 2.5  
        BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
        self.delay = delay
        self._rotateFailed = False
        self.maxBytes = maxBytes
        self.backupCount = backupCount
        self._open_lockfile()
        # For debug mode, swap out the "_degrade()" method with a more a verbose one.
        if debug:
            self._degrade = self._degrade_debug
Esempio n. 26
0
    def __init__(self, path_template, scope_id):
        """
        Create the logger using the scope_id to create a default file path.

        :param path_template: the template with {scope} to generate file names
        :param scope_id: the id of the scope that will be used to create the default file path
        """
        self.path_template = path_template
        self.scope_id = scope_id
        self.default_path = path_template.format(scope=scope_id)
        BaseRotatingHandler.__init__(self, self.default_path, 'w', delay=True)
        self.exiting_scope = None
        self.delete_next_file_after_rollover = False
Esempio n. 27
0
    def __init__(self, filename, encoding=None, delay=False, utc=False, mode='a', maxBytes=0, backupCount=0,
                 logPath=None, *args, **kwargs):
        self.utc = utc
        self.suffix = '%Y%m%d'
        self.filename = filename
        self.log_path = logPath
        self.baseFilename = self._handler_date_path(self.filename)
        self.currentFileName = self._compute_fn()

        if maxBytes > 0:
            mode = 'a'
        BaseRotatingHandler.__init__(self, self.baseFilename, mode, encoding, delay)
        self.maxBytes = maxBytes
        self.backupCount = backupCount
Esempio n. 28
0
	def __init__(self, base_filename, suffix_filename='', when='H',
				encoding=None, log_name_format=None, log_time_format={}):

		#
		# Control log file name
		#
		if log_time_format == {}:
			self.log_time_format = {
				"S": "%Y%m%d_%H%M%S",
				"M": "%Y%m%d_%H%M",
				"H": "%Y%m%d_%H",
				"D": "%Y%m%d"
			}
		else:
			self.log_time_format = log_time_format
		self.log_time_format.update(log_time_format)

		if log_name_format == None:
			self.log_name_format = "%(name)s.%(time)s.%(suffix)s"
		else:
			self.log_name_format = log_name_format

		self.when = when.upper()

		ts = time.time()
		currentTime = int(ts)
		offset = datetime.fromtimestamp(ts) - datetime.utcfromtimestamp(ts)
		self.offset_secs = offset.seconds

		if self.when.startswith('S'):
			self.interval = 1
		elif self.when.startswith('M'):
			self.interval = 60
		elif self.when.startswith('H'):
			self.interval = 60 * 60
		elif self.when.startswith('D'):
			self.interval = 60 * 60 * 24
		else:
			raise ValueError("Invalid rollover interval specified: %s" % self.when)

		lastRoll = currentTime - (currentTime % self.interval)
		self.rolloverAt = lastRoll + self.interval - self.offset_secs

		tt = time.localtime(lastRoll)

		filename = self.get_filename(base_filename, tt, suffix_filename)

		BaseRotatingHandler.__init__(self, filename, 'a', encoding)
		self.baseFilename = os.path.abspath(base_filename)
		self.suffix_filename = suffix_filename
Esempio n. 29
0
    def __init__(self,
                 filename,
                 mode='a',
                 maxBytes=0,
                 backupCount=0,
                 encoding=None,
                 delay=0):
        dateTime = datetime.datetime.now().strftime('%d-%m-%Y-%H-%M-%S')

        filename = filename + '/log-' + dateTime + '.log'
        if maxBytes > 0:
            mode = 'a'
        BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
        self.maxBytes = maxBytes
        self.backupCount = backupCount
Esempio n. 30
0
    def __init__(self, filename, maxDays=1, minDays=0, maxMBytes=10, backupCount=5, compression=None):
        """
        Initialize the Handler.  We assume the following:

            1. Interval is in days
            2. No special encoding
            3. No delays are set
            4. Timestamps are not in UTC

        @type filename: string
        @param filename: The full path of the log file
        @type interval: int
        @param interval: Number of days before file rotation
        @type maxMBytes: int
        @param maxMBytes: Maximum size of the logfile in MB before file rotation
        @type backupCount: int
        @param backupCount: Number of backups to keep

        """
        # Make dirs if logging directory does not exist
        if not os.path.exists(os.path.dirname(filename)):
            os.makedirs(os.path.dirname(filename))

        self.compression = ''
        try:
            if compression.lower() in COMPRESSION_SUPPORTED:
                self.compression = compression.lower()
        except AttributeError:
            pass
        # bz2 compression can be implementes with encoding='bz2-codec' in BaseRotatingHandler
        mode = 'a'
        BaseRotatingHandler.__init__(self, filename, mode, encoding=None)
        self.backupCount = backupCount
        self.maxBytes = maxMBytes * 1024.0 * 1024.0 # Convert the MB to bytes as needed by the base class
        self.min_lifetime = minDays * 24 * 60 * 60 # Convert min days to seconds
        self.interval = maxDays * 24 * 60 * 60 # Convert max days (interval) to seconds

        # We are enforcing a date/time format for rotated log files to include
        # year, month, day, hours, and minutes.  The hours and minutes are to
        # preserve multiple rotations in a single day.
        self.suffix = "%Y-%m-%d_%H-%M"
        self.extMatch = re.compile(r"^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}(\.gz|\.zip)?$")

        if os.path.exists(filename):
            begin_interval_time = os.stat(filename)[stat.ST_MTIME]
        else:
            begin_interval_time = int(time.time())
        self.rolloverAt = begin_interval_time + self.interval
Esempio n. 31
0
    def __init__(self, filename, delay=0, symlink=True, prologue=None, epilogue=None, rollover=None):
        self.prefix = filename + "-"
        self.suffix = "%Y-%m-%d"
        self.prologue = prologue
        self.epilogue = epilogue
        self.rollover = rollover
        self.symlink = symlink
        self.rolloverAt = self.computeRollover(int(time.time()))

        if symlink:
            self._symlinkPointToToday(filename)

        BaseRotatingHandler.__init__(self, filename, "a", None, delay)
        # print "Will rollover at %d, %d seconds from now" % (self.rolloverAt, self.rolloverAt - currentTime)
        if self.prologue:
            self.stream.write(self.prologue % self._getParameter())
Esempio n. 32
0
    def __init__(self, filename, delay=0, symlink=True, prologue=None, epilogue=None, rollover=None):
        self.prefix = filename + "-"
        self.suffix = "%Y-%m-%d"
        self.prologue = prologue
        self.epilogue = epilogue
        self.rollover = rollover
        self.symlink = symlink
        self.rolloverAt = self.computeRollover(int(time.time()))

        if symlink:
            self._symlinkPointToToday(filename)

        BaseRotatingHandler.__init__(self, filename, 'a', None, delay)
        # print "Will rollover at %d, %d seconds from now" % (self.rolloverAt, self.rolloverAt - currentTime)
        if self.prologue:
            self.stream.write(self.prologue % self._getParameter())
Esempio n. 33
0
    def __init__(self,
                 filename,
                 when='h',
                 interval=1,
                 backupCount=0,
                 encoding=None,
                 delay=False,
                 utc=False,
                 atTime=None):
        BaseRotatingHandler.__init__(self, filename, 'a', encoding, delay)
        self.when = when.upper()
        self.backupCount = backupCount
        self.utc = utc
        self.atTime = atTime

        if self.when == 'S':
            self.interval = 1  # one second
        elif self.when == 'M':
            self.interval = 60  # one minute
        elif self.when == 'H':
            self.interval = 60 * 60  # one hour
        elif self.when == 'D' or self.when == 'MIDNIGHT':
            self.interval = 60 * 60 * 24  # one day
        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])
        else:
            raise ValueError("Invalid rollover interval specified: %s" %
                             self.when)

        self.interval = self.interval * interval  # multiply by units requested
        # The following line added because the filename passed in could be a
        # path object (see Issue #27493), but self.baseFilename will be a string
        filename = self.baseFilename
        if os.path.exists(filename):
            t = os.stat(filename)[ST_MTIME]
        else:
            t = int(time.time())
        self.rolloverAt = self.computeRollover(t)
    def __init__(self, filename, hour=8, minute=0, backupCount=0, encoding=None, delay=False, utc=False):
        BaseRotatingShitHandler.__init__(self, filename, 'a', encoding, delay)

        self.delay = delay
        self.backupCount = backupCount
        self.utc = utc

        self.interval = 60 * 60 * 24
        self.suffix = "%Y-%m-%d"
        self.extMatch = r"^\d{4}-\d{2}-\d{2}$"
        self.extMatch = re.compile(self.extMatch)

        t = int(time.mktime(datetime.datetime.now(pytz.timezone("Europe/Berlin")).replace(hour=hour, minute=minute, second=0).timetuple()))
        if datetime.datetime.now(pytz.timezone("Europe/Berlin")).hour < hour or (datetime.datetime.now(pytz.timezone("Europe/Berlin")).hour==hour and datetime.datetime.now(pytz.timezone("Europe/Berlin")).minute < minute):
            self.rolloverAt = self.computeRollover(t-self.interval)
        else:
            self.rolloverAt = self.computeRollover(t)
Esempio n. 35
0
    def __init__(self,
                 prefix_name,
                 backup_count=0,
                 encoding=None,
                 delay=False,
                 utc=False):
        self.suffix_name_ = "%Y%m%d.log"
        self.prefix_name_ = prefix_name
        self.utc_ = utc
        cur_file_name = self._current_log_filename()
        self.backup_count_ = backup_count
        BaseRotatingHandler.__init__(self, cur_file_name, 'a', encoding, delay)

        self.interval_ = 60 * 60 * 24  # one day
        self.ext_match_ = re.compile(r"^\d{4}\d{2}\d{2}$")

        self.rollover_at_ = self.compute_rollover()
Esempio n. 36
0
 def __init__(self,
              pathformat="logs/%Y/%m/%Y-%m-%d.log",
              utc=False,
              encoding=None,
              delay=False):
     self.path_format = pathformat
     self.utc = utc
     current_time = self._get_time()
     current_file = self._format_time(current_time)
     self._current_day = self._get_days_since_epoch(current_time)
     self._create_dirs(resource_filename('logs', current_file))
     BaseRotatingHandler.__init__(self,
                                  filename=resource_filename(
                                      'logs', current_file),
                                  mode="a",
                                  encoding=encoding,
                                  delay=delay)
Esempio n. 37
0
    def __init__(self, level, file_prefix, when, backup_count, max_bytes, mode='a', encoding=None, delay=False
                 ,interval=1, ST_MTIME = 8, utc=False, atTime=None):
        BaseRotatingHandler.__init__(self, file_prefix, mode, encoding, delay)
        # 处理按大小划分
        self.maxBytes = max_bytes
        # 处理按时间划分
        self.when = when.upper()
        self.backupCount = backup_count
        self.utc = utc
        self.atTime = atTime
        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}(\.\w+)?$"
        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}(\.\w+)?$"
        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}(\.\w+)?$"
        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}(\.\w+)?$"
        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}(\.\w+)?$"
        else:
            raise ValueError("Invalid rollover interval specified: %s" % self.when)

        self.extMatch = re.compile(self.extMatch, re.ASCII)
        self.interval = self.interval * interval  # multiply by units requested
        if os.path.exists(file_prefix):
            t = os.stat(file_prefix)[ST_MTIME]
        else:
            t = int(time.time())
        self.rolloverAt = self.computeRollover(t)
Esempio n. 38
0
    def __init__(self,
                 log_dir="logs",
                 mode="a",
                 max_bytes=0,
                 encoding=None,
                 delay=False):

        if max_bytes > 0:
            mode = "a"
        self._log_dir = log_dir
        self._suffix = ".log"
        self._year_month = datetime.datetime.now().strftime("%Y-%m")
        self.store_dir = os.path.join(self._log_dir, self._year_month)
        self._create_new_stream_if_not_exists(self.store_dir,
                                              open_stream=False)
        self.filename = datetime.datetime.now().strftime("%Y-%m-%d")
        filename = os.path.join(self.store_dir, self.filename) + self._suffix
        BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
        self.max_bytes = max_bytes
Esempio n. 39
0
 def __init__(self,
              log_path,
              filename,
              keep_days=7,
              emit_tg=True,
              environment=""):
     self.emit_tg = emit_tg
     self.environment = environment
     self.suffix_format = DateTimeFormatEnum.DAY_FORMAT
     self.keep_days = keep_days
     self.log_path = log_path
     self.filename = filename
     self.base_file = os.path.join(log_path, filename)
     self.current_file = self.get_current_file()
     BaseRotatingHandler.__init__(self,
                                  self.base_file,
                                  'a',
                                  'utf-8',
                                  delay=False)
Esempio n. 40
0
    def __init__(self,
                 pathformat="%Y/%m/%Y-%m-%d.log", utc=False,
                 encoding=None, delay=False):

        self.pathformat = pathformat
        self.utc = utc

        # Placeholder function for the info function of the
        # logging module.
        self.logging_writeinfo = lambda *args: None

        current_time = self._get_time()
        current_file = self._format_time(current_time)


        self._current_day = self._get_days_since_epoch(current_time)
        self._create_dirs(os.path.abspath(current_file))

        BaseRotatingHandler.__init__(self,
                                     filename=current_file, mode="a",
                                     encoding=encoding, delay=delay)
Esempio n. 41
0
    def __init__(self,
                 filename,
                 mode='a',
                 maxBytes=0,
                 backupCount=0,
                 encoding=None,
                 delay=0):
        """
        Open the specified file and use it as the stream for logging.

        By default, the file grows indefinitely. You can specify particular
        values of maxBytes and backupCount to allow the file to rollover at
        a predetermined size.

        Rollover occurs whenever the current log file is nearly maxBytes in
        length. If backupCount is >= 1, the system will successively create
        new files with the same pathname as the base file, but with extensions
        ".1", ".2" etc. appended to it. For example, with a backupCount of 5
        and a base file name of "app.log", you would get "app.log",
        "app.log.1", "app.log.2", ... through to "app.log.5". The file being
        written to is always "app.log" - when it gets filled up, it is closed
        and renamed to "app.log.1", and if files "app.log.1", "app.log.2" etc.
        exist, then they are renamed to "app.log.2", "app.log.3" etc.
        respectively.

        If maxBytes is zero, rollover never occurs.
        """
        # If rotation/rollover is wanted, it doesn't make sense to use another
        # mode. If for example 'w' were specified, then if there were multiple
        # runs of the calling application, the logs from previous runs would be
        # lost if the 'w' is respected, because the log file would be truncated
        # on each run.
        if maxBytes > 0:
            mode = 'a'
        BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
        self.maxBytes = maxBytes
        self.backupCount = backupCount
        self.baseFilename = self.baseFilename.split(
            '+')[0] + '+' + datetime.datetime.now().strftime(
                '%Y-%m-%d__%H-%M-%S-%f') + '.txt'
Esempio n. 42
0
    def __init__(self, filename, mode='a', 
                timestring_format="%Y%m%d_%H%M%S", prefix=True, encoding=None):
        '''Open the specified file and use it as the stream for logging.

        File grows indefinitely, until rollover is called.

        A rollover will close the stream; rename the file to a new name, 
        with a prefix or suffix (prefix if prameter prefix=True)
        to the filename of the current date and time, in the format specified 
        by timestring_format; and open a fresh log file.
        
        For example, with a base file name of "app.log", and other arguments 
        as default, a rolled-over logfile might have a name of
        "20090610_234620.app.log".
        
        The file being written to is always "app.log".
        
        timestring_format is a format string, as described for time.strftime().
        '''
        BaseRotatingHandler.__init__(self, filename, mode, encoding)
        self.timestring_format = timestring_format
        self.prefix = prefix
Esempio n. 43
0
    def __init__(self, filename, mode='a', maxBytes=0, encoding=None, 
            debug=True, delay=0):
        """
        Open the specified file and use it as the stream for logging.

        By default, the file grows indefinitely. You can specify particular
        values of maxBytes to allow the file to rollover at a predetermined
        size.

        Rollover occurs whenever the current log file is nearly maxBytes in
        length.  Old files are renamed based on timestamps.

        If maxBytes is zero, rollover never occurs.
        """
        # Absolute file name handling done by FileHandler since Python 2.5  
        BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
        self.delay = delay
        self._rotateFailed = False
        self.maxBytes = maxBytes
        self._open_lockfile()
        # For debug mode, swap out the "_degrade()" method with a more a verbose one.
        if debug:
            self._degrade = self._degrade_debug
    def __init__(self, filename, mode='a', maxBytes=0, backupCount=0,
                 encoding=None, debug=True, supress_abs_warn=False):
        """
        Open the specified file and use it as the stream for logging.

        By default, the file grows indefinitely. You can specify particular
        values of maxBytes and backupCount to allow the file to rollover at
        a predetermined size.

        Rollover occurs whenever the current log file is nearly maxBytes in
        length. If backupCount is >= 1, the system will successively create
        new files with the same pathname as the base file, but with extensions
        ".1", ".2" etc. appended to it. For example, with a backupCount of 5
        and a base file name of "app.log", you would get "app.log",
        "app.log.1", "app.log.2", ... through to "app.log.5". The file being
        written to is always "app.log" - when it gets filled up, it is closed
        and renamed to "app.log.1", and if files "app.log.1", "app.log.2" etc.
        exist, then they are renamed to "app.log.2", "app.log.3" etc.
        respectively.

        If maxBytes is zero, rollover never occurs.

        On Windows, it is not possible to rename a file that is currently opened
        by another process.  This means that it is not possible to rotate the
        log files if multiple processes is using the same log file.  In this
        case, the current log file will continue to grow until the rotation can
        be completed successfully.  In order for rotation to be possible, all of
        the other processes need to close the file first.  A mechanism, called
        "degraded" mode, has been created for this scenario.  In degraded mode,
        the log file is closed after each log message is written.  So once all
        processes have entered degraded mode, the next rotate log attempt should
        be successful and then normal logging can be resumed.

        This log handler assumes that all concurrent processes logging to a
        single file will are using only this class, and that the exact same
        parameters are provided to each instance of this class.  If, for
        example, two different processes are using this class, but with
        different values for 'maxBytes' or 'backupCount', then odd behavior is
        expected. The same is true if this class is used by one application, but
        the RotatingFileHandler is used by another.

        NOTE:  You should always provide 'filename' as an absolute path, since
        this class will need to re-open the file during rotation. If your
        application call os.chdir() then subsequent log files could be created
        in the wrong directory.
        """
        # The question of absolute paths: I'm not sure what the 'right thing' is
        # to do here. RotatingFileHander simply ignores this possibility. I was
        # going call os.path.abspath(), but that potentially limits uses. For
        # example, on Linux (any posix system?) you can rename a directory of a
        # running app, and the app wouldn't notice as long as it only opens new
        # files using relative paths. But since that's not a "normal" thing to
        # do, and having an app call os.chdir() is a much more likely scenario
        # that should be supported. For the moment, we are just going to warn
        # the user if they provide a relative path and do some other voodoo
        # logic that you'll just have to review for yourself.
        
        # if the given filename contains no path, we make an absolute path
        if not os.path.isabs(filename):
            if FORCE_ABSOLUTE_PATH or \
               not os.path.split(filename)[0]:
                filename = os.path.abspath(filename)
            elif not supress_abs_warn:
                from warnings import warn
                warn("The given 'filename' should be an absolute path.  If your "
                     "application calls os.chdir(), your logs may get messed up. "
                     "Use 'supress_abs_warn=True' to hide this message.")
        try:
            BaseRotatingHandler.__init__(self, filename, mode, encoding)
        except TypeError: # Due to a different logging release without encoding support  (Python 2.4.1 and earlier?)
            BaseRotatingHandler.__init__(self, filename, mode)
            self.encoding = encoding
        
        self._rotateFailed = False
        self.maxBytes = maxBytes
        self.backupCount = backupCount
        # Prevent multiple extensions on the lock file (Only handles the normal "*.log" case.)
        if filename.endswith(".log"):
            lock_file = filename[:-4]
        else:
            lock_file = filename
        self.stream_lock = open(lock_file + ".lock", "w")
        
        # For debug mode, swap out the "_degrade()" method with a more a verbose one.
        if debug:
            self._degrade = self._degrade_debug
 def __init__(self, log_directory, logfile_prefix):
     BaseRotatingHandler.__init__(self, "", 'a', encoding=None, delay=True)
     self._log_directory = log_directory
     self._logfile_prefix = logfile_prefix
     self.doRollover()
Esempio n. 46
0
 def __init__ (self, file_pattern, encoding=None):
     self.file_pattern = file_pattern
     BaseRotatingHandler.__init__(self, self.get_baseFilename(),
         "a", encoding)
Esempio n. 47
0
    def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False, debug=False):
        """ 
            * interval, backupCount is not working!!! *

        Just Copied from logging.handlers.TimedRotatingFileHandler

        # a rollover occurs.  Current 'when' events supported:
        # S - Seconds
        # M - Minutes
        # H - Hours
        # D - Days
        # midnight - roll over at midnight
        # W{0-6} - roll over on a certain day; 0 - Monday
        #
        # Case of the 'when' specifier is not important; lower or upper case
        # will work.

        """
        BaseRotatingHandler.__init__(self, filename, 'a', encoding, delay)
        self.when = when.upper()
        self.backupCount = backupCount
        self.utc = utc
        self.debug = debug
        self.mylogfile  = "%s.%08d" % ('/tmp/mptfhanldler', randint(0,99999999))

        self.interval = 1 # datetime timedelta only have, days, seconds, microseconds
        
        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}$"
            self.when = 'D' # MIDNIGHT is day, use day only
        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.extMatch = re.compile(self.extMatch)
        #self.interval = self.interval * interval # multiply by units requested
        self.interval = self.interval * 1 # interval arg is not working

        # lock file, contain next rollover timestamp
        self.stream_lock = None
        self.lock_file  = self._getLockFile()

        # read from conf first for inherit the first process
        # if it is the first process, please remove the lock file by hand first
        self.nextRolloverTime = self.getNextRolloverTime()
        if not self.nextRolloverTime:
            self.nextRolloverTime = self.computerNextRolloverTime()
            self.saveNextRolloverTime()
Esempio n. 48
0
 def __init__(self, filename, encoding=None, delay=False, utc=False, **kwargs):
     self.utc = utc
     self.suffix = "%Y-%m-%d"
     self.baseFilename = filename
     self.currentFileName = self._compute_fn()
     BaseRotatingHandler.__init__(self, filename, 'a', encoding, delay)
Esempio n. 49
0
 def __init__(self, base_filename):
     self.base_filename = base_filename
     self.date_val = self.date_value
     BaseRotatingHandler.__init__(self, self.date_filename, 'a', None, 0)
Esempio n. 50
0
    def __init__(self, filename, mode='a', when='h', backupCount=0, utc=False,
                 encoding=None, debug=True, delay=0, **kwargs):
        """
        Open the specified file and use it as the stream for logging.

        By default, the file grows indefinitely. You can specify particular
        values of maxBytes and backupCount to allow the file to rollover at
        a predetermined size.

        Rollover occurs whenever the current log file is nearly maxBytes in
        length. If backupCount is >= 1, the system will successively create
        new files with the same pathname as the base file, but with extensions
        ".1", ".2" etc. appended to it. For example, with a backupCount of 5
        and a base file name of "app.log", you would get "app.log",
        "app.log.1", "app.log.2", ... through to "app.log.5". The file being
        written to is always "app.log" - when it gets filled up, it is closed
        and renamed to "app.log.1", and if files "app.log.1", "app.log.2" etc.
        exist, then they are renamed to "app.log.2", "app.log.3" etc.
        respectively.

        If maxBytes is zero, rollover never occurs.

        On Windows, it is not possible to rename a file that is currently opened
        by another process.  This means that it is not possible to rotate the
        log files if multiple processes is using the same log file.  In this
        case, the current log file will continue to grow until the rotation can
        be completed successfully.  In order for rotation to be possible, all of
        the other processes need to close the file first.  A mechanism, called
        "degraded" mode, has been created for this scenario.  In degraded mode,
        the log file is closed after each log message is written.  So once all
        processes have entered degraded mode, the net rotation attempt should
        be successful and then normal logging can be resumed.  Using the 'delay'
        parameter may help reduce contention in some usage patterns.

        This log handler assumes that all concurrent processes logging to a
        single file will are using only this class, and that the exact same
        parameters are provided to each instance of this class.  If, for
        example, two different processes are using this class, but with
        different values for 'maxBytes' or 'backupCount', then odd behavior is
        expected. The same is true if this class is used by one application, but
        the RotatingFileHandler is used by another.
        """
        # Absolute file name handling done by FileHandler since Python 2.5
        BaseRotatingHandler.__init__(self, filename, mode, encoding, delay)
        self.delay = delay
        self._rotateFailed = False
        self.when = when.upper()
        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.extMatch = re.compile(self.extMatch)
        self.backupCount = backupCount
        self.utc = utc
        if utc:
            self._get_time = lambda: time.strftime(self.suffix, time.gmtime())
        else:
            self._get_time = lambda: time.strftime(self.suffix, time.localtime())
        self.time = self._get_time()
        self._open_lockfile()
        # For debug mode, swap out the "_degrade()" method with a more a verbose one.
        if debug:
            self._degrade = self._degrade_debug
        self.callback = kwargs.get('callback', None)