欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python多進(jìn)程下實(shí)現(xiàn)日志記錄按時(shí)間分割

 更新時(shí)間:2019年07月22日 11:46:49   作者:Just_Walking  
這篇文章主要為大家詳細(xì)介紹了python多進(jìn)程下實(shí)現(xiàn)日志記錄按時(shí)間分割,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

python多進(jìn)程下實(shí)現(xiàn)日志記錄按時(shí)間分割,供大家參考,具體內(nèi)容如下

原理:自定義日志handler繼承TimedRotatingFileHandler,并重寫computeRollover與doRollover函數(shù)。其中重寫computeRollover是為了能按整分鐘/小時(shí)/天來分割日志,如按天分割,2018-04-10 00:00:00~2018-04-11 00:00:00,是一個半閉半開區(qū)間,且不是原意的:從日志創(chuàng)建時(shí)間或當(dāng)前時(shí)間開始,到明天的這個時(shí)候。

代碼如下:

#!/usr/bin/env python
# encoding: utf-8

"""自定義日志處理類"""


import os
import time
from logging.handlers import TimedRotatingFileHandler


class MyLoggingHandler(TimedRotatingFileHandler):

  def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False, atTime=None):
    TimedRotatingFileHandler.__init__(self, filename, when=when, interval=interval, backupCount=backupCount, encoding=encoding, delay=delay, utc=utc, atTime=atTime)

  def computeRollover(self, currentTime):
    # 將時(shí)間取整
    t_str = time.strftime(self.suffix, time.localtime(currentTime))
    t = time.mktime(time.strptime(t_str, self.suffix))
    return TimedRotatingFileHandler.computeRollover(self, t)

  def doRollover(self):
    """
    do a rollover; in this case, a date/time stamp is appended to the filename
    when the rollover happens. However, you want the file to be named for the
    start of the interval, not the current time. If there is a backup count,
    then we have to get a list of matching filenames, sort them and remove
    the one with the oldest suffix.
    """
    if self.stream:
      self.stream.close()
      self.stream = None
    # get the time that this sequence started at and make it a TimeTuple
    currentTime = int(time.time())
    dstNow = time.localtime(currentTime)[-1]
    t = self.rolloverAt - self.interval
    if self.utc:
      timeTuple = time.gmtime(t)
    else:
      timeTuple = time.localtime(t)
      dstThen = timeTuple[-1]
      if dstNow != dstThen:
        if dstNow:
          addend = 3600
        else:
          addend = -3600
        timeTuple = time.localtime(t + addend)
    dfn = self.rotation_filename(self.baseFilename + "." +
                   time.strftime(self.suffix, timeTuple))
    # 修改內(nèi)容--開始
    # 在多進(jìn)程下,若發(fā)現(xiàn)dfn已經(jīng)存在,則表示已經(jīng)有其他進(jìn)程將日志文件按時(shí)間切割了,只需重新打開新的日志文件,寫入當(dāng)前日志;
    # 若dfn不存在,則將當(dāng)前日志文件重命名,并打開新的日志文件
    if not os.path.exists(dfn):
      try:
        self.rotate(self.baseFilename, dfn)
      except FileNotFoundError:
        # 這里會出異常:未找到日志文件,原因是其他進(jìn)程對該日志文件重命名了,忽略即可,當(dāng)前日志不會丟失
        pass
    # 修改內(nèi)容--結(jié)束
    # 原內(nèi)容如下:
    """
    if os.path.exists(dfn):
      os.remove(dfn)
    self.rotate(self.baseFilename, dfn)
    """

    if self.backupCount > 0:
      for s in self.getFilesToDelete():
        os.remove(s)
    if not self.delay:
      self.stream = self._open()
    newRolloverAt = self.computeRollover(currentTime)
    while newRolloverAt <= currentTime:
      newRolloverAt = newRolloverAt + self.interval
    # If DST changes and midnight or weekly rollover, adjust for this.
    if (self.when == 'MIDNIGHT' or self.when.startswith('W')) and not self.utc:
      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
    self.rolloverAt = newRolloverAt

說明

第一次修改,如有不妥之處,還請指出,不勝感激。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python中不同類之間調(diào)用方法的四種方式小結(jié)

    Python中不同類之間調(diào)用方法的四種方式小結(jié)

    類是一種面向?qū)ο蟮木幊谭妒?它允許我們將數(shù)據(jù)和功能封裝在一個實(shí)體中,本文主要介紹了Python中不同類之間調(diào)用方法的四種方式小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02
  • tesserocr與pytesseract模塊的使用方法解析

    tesserocr與pytesseract模塊的使用方法解析

    這篇文章主要介紹了tesserocr與pytesseract模塊的使用方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • python調(diào)用kubernetesAPI簡單使用方法

    python調(diào)用kubernetesAPI簡單使用方法

    這篇文章主要介紹了python調(diào)用kubernetesAPI簡單使用方法,K8s也提供API接口,提供這個接口的是管理節(jié)點(diǎn)的apiserver組件,下文更多相關(guān)內(nèi)容,需要的小伙伴可以參考一下
    2022-05-05
  • Django數(shù)據(jù)庫類庫MySQLdb使用詳解

    Django數(shù)據(jù)庫類庫MySQLdb使用詳解

    Django項(xiàng)目要操作數(shù)據(jù)庫,首先要和數(shù)據(jù)庫建立連接,才能讓程序中的數(shù)據(jù)和數(shù)據(jù)庫關(guān)聯(lián)起來進(jìn)行數(shù)據(jù)的增刪改查操作。這篇文章主要介紹了Django數(shù)據(jù)庫類庫MySQLdb使用詳解,感興趣的小伙伴們可以參考一下
    2019-04-04
  • Python?HMAC模塊維護(hù)數(shù)據(jù)安全技術(shù)實(shí)例探索

    Python?HMAC模塊維護(hù)數(shù)據(jù)安全技術(shù)實(shí)例探索

    本篇博客將帶領(lǐng)讀者深入探索Python中HMAC模塊的高級應(yīng)用,通過豐富的示例代碼和詳細(xì)的解釋,揭示HMAC在實(shí)際應(yīng)用場景中的多面光芒,從基礎(chǔ)概念到密碼存儲、文件完整性驗(yàn)證、API安全,再到與加密算法的巧妙結(jié)合
    2024-01-01
  • Python中chinesecalendar簡介、安裝、使用方法詳細(xì)講解

    Python中chinesecalendar簡介、安裝、使用方法詳細(xì)講解

    這篇文章主要介紹了Python中chinesecalendar簡介、安裝、使用方法詳細(xì)講解,該庫是判斷某年某月某一天是不是工作日/節(jié)假日。 支持 2004年 至 2023年,包括 2020年 的春節(jié)延長,需要的朋友可以參考下
    2023-03-03
  • Python實(shí)現(xiàn)Canny及Hough算法代碼實(shí)例解析

    Python實(shí)現(xiàn)Canny及Hough算法代碼實(shí)例解析

    這篇文章主要介紹了Python實(shí)現(xiàn)Canny與Hough算法代碼實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • 基于Python實(shí)現(xiàn)簡單的學(xué)生點(diǎn)名系統(tǒng)

    基于Python實(shí)現(xiàn)簡單的學(xué)生點(diǎn)名系統(tǒng)

    現(xiàn)在的學(xué)生大部分都很積極,會主動舉手回答問題。但是,也會遇到一些不好的情況,比如年級越高主動舉手的人越少,所以本文做了一個隨機(jī)的學(xué)生點(diǎn)名系統(tǒng)可以幫老師解決這些問題
    2022-09-09
  • 簡單了解Python生成器是什么

    簡單了解Python生成器是什么

    這篇文章主要介紹了簡單了解Python生成器是什么,生成器就是一個在行為上和迭代器非常類似的對象,如果把迭代器比作 Android 系統(tǒng),那么生成器就是 iOS,二者功能上差不多,但是生成器更優(yōu)雅,需要的朋友可以參考下
    2019-07-07
  • 淺談numpy數(shù)組的幾種排序方式

    淺談numpy數(shù)組的幾種排序方式

    這篇文章主要介紹了淺談numpy數(shù)組的幾種排序方式,涉及對numpy的簡單介紹和創(chuàng)建數(shù)組的方式,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12

最新評論