Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
Make IconPeriodAndBytesFileHandler to inherit BaseRotatingHandler.#2
Browse files Browse the repository at this point in the history
  • Loading branch information
inwonKim committed Oct 17, 2018
1 parent 6104269 commit 4053c71
Showing 1 changed file with 46 additions and 10 deletions.
56 changes: 46 additions & 10 deletions iconcommons/logger/icon_period_and_bytes_file_handler.py
Expand Up @@ -11,27 +11,63 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os

This comment has been minimized.

Copy link
@cow-hs

cow-hs Oct 18, 2018

Contributor

try ctrl + option + O

import time
import types
from logging.handlers import BaseRotatingHandler
from stat import ST_MTIME

from .icon_bytes_file_handler import IconBytesFileHandler
from .icon_period_file_handler import IconPeriodFileHandler
from iconcommons.logger.logger_utils import suffix, extMatch


class IconPeriodAndBytesFileHandler(IconPeriodFileHandler, IconBytesFileHandler):
class IconPeriodAndBytesFileHandler(BaseRotatingHandler):
suffix = suffix
extMatch = extMatch

def __init__(self, filename, mode='a', maxBytes=0, backupCount=0,
encoding=None, delay=0, when='h', interval=1, utc=False):
IconPeriodFileHandler.__init__(
self, filename=filename, when=when, interval=interval,
backupCount=backupCount, encoding=encoding, delay=delay, utc=utc)
super().__init__(filename, mode, encoding, delay)
self.maxBytes = maxBytes
self.backupCount = backupCount
self.when = when.upper()
self.interval = interval
self.utc = utc

self.computeRollover = types.MethodType(IconPeriodFileHandler.computeRollover, self)
self.doRollover_bytes = types.MethodType(IconBytesFileHandler.doRollover, self)
self.getFilesToDelete = types.MethodType(IconBytesFileHandler.getFilesToDelete, self)
self.shouldRollover_period = types.MethodType(IconPeriodFileHandler.shouldRollover, self)
self.shouldRollover_bytes = types.MethodType(IconBytesFileHandler.shouldRollover, self)

IconBytesFileHandler.__init__(self, filename=filename, mode=mode, maxBytes=maxBytes,
backupCount=backupCount, encoding=encoding, delay=delay)
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)

def computeRollover(self, current_time):
return IconPeriodFileHandler.computeRollover(self, current_time)
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 doRollover(self):
# get from logging.handlers.TimedRotatingFileHandler.doRollover()
Expand All @@ -53,7 +89,7 @@ def doRollover(self):
new_rollover_at += addend
self.rolloverAt = new_rollover_at

return IconBytesFileHandler.doRollover(self)
return self.doRollover_bytes()

def shouldRollover(self, record):
return IconPeriodFileHandler.shouldRollover(self, record) or IconBytesFileHandler.shouldRollover(self, record)
return self.shouldRollover_period(record) or self.shouldRollover_bytes(record)

0 comments on commit 4053c71

Please sign in to comment.