eventlet模块中select出现ValueError错误
eventlet.monkey_patch是否影响threading

python自定义windowsr日志支持文件分割

妙音 posted @ 2021年1月20日 13:48 in python , 1281 阅读

描述

 
python自带的RotatingFileHandler,在windows中运行, 分割文件rename时会出问题。因为windows不支持rename正在使用的文件
 

解决办法

 
自定义handler, 按大小和日期切割文件
 
原理: handler写文件时调用emit, 其中shouldRollover判断是否要分割, doRollover进行分割. 重写这个两个函数就可以
 
class BaseRotatingHandler(logging.FileHandler):

    def emit(self, record):
        """
        Emit a record.

        Output the record to the file, catering for rollover as described
        in doRollover().
        """
        try:
            if self.shouldRollover(record):
                self.doRollover()
            logging.FileHandler.emit(self, record)
        except Exception:
            self.handleError(record)
 

示例

 
import os
import time
import datetime
import logging
from logging.handlers import RotatingFileHandler
from pathlib import Path


class AutumnRotatingFileHandler(RotatingFileHandler):
    """
    日志+大小+支持windows
    """

    def __init__(self, filename, mode='a', maxBytes=0, backupCount=99, encoding=None, delay=False, backupDayCount=30):
        filename = str(filename)
        self.date_format = "%Y%m%d"
        self.create_date = self._now_date()
        self.backupCount = backupCount

        # 保存原始文件名
        self.filename = str(Path(filename).absolute())
        self.backupDayCount = backupDayCount

        RotatingFileHandler.__init__(self, self.last_file_name(filename), mode=mode, maxBytes=maxBytes, backupCount=backupCount, encoding=encoding, delay=delay)

    def _now_date(self):
        return time.strftime(self.date_format, time.localtime())

    def doRollover(self):
        """
        Do a rollover, as described in __init__().
        """
        # 关闭当前文件
        if self.stream:
            self.stream.close()
            self.stream = None

        # 生成最新文件名
        suffix = "."+ self._now_date()
        self.baseFilename = str(self.filename) + suffix
        if self.backupCount > 0:
            for i in range(0, self.backupCount):
                number_suffix = "{:0>2d}".format(i)
                sfn = ".".join([self.baseFilename, number_suffix])
                if os.path.exists(sfn):
                    continue
                else:
                    break
            number_suffix = "{:0>2d}".format(i)
            self.baseFilename = ".".join([self.baseFilename, number_suffix])

        # 删除过期文件
        self.deleteExpiredFiles()

        # 打开
        self.stream = self._open()
        self.create_date = self._now_date()

    def shouldRollover(self, record):
        """
        Determine if rollover should occur.

        Basically, see if the supplied record would cause the file to exceed
        the size limit we have.
        """
        # 文件分割条件1: 日期变化
        if self._now_date() != self.create_date:
            return 1

        if self.stream is None:                 # delay was set...
            self.stream = self._open()

        # 文件分割条件1: 文件大小超过限制
        if self.maxBytes > 0:                   # are we rolling over?
            msg = "%s\n" % self.format(record)
            self.stream.seek(0, 2)  #due to non-posix-compliant Windows feature
            if self.stream.tell() + len(msg) >= self.maxBytes:
                return 1
        return 0

    def deleteExpiredFiles(self):
        """
        删除过期文件
        """
        dead_datetime = datetime.datetime.now() - datetime.timedelta(days=self.backupDayCount)

        filenames = []
        for i in range(1, 3):
            date = dead_datetime - datetime.timedelta(days=1)
            date_str = date.strftime(self.date_format)

            base_name = "%s.%s" % (self.filename, date_str)
            filenames.append(base_name)

            for j in range(1, self.backupCount):
                sfn = "%s.%d" % (base_name, j)
                filenames.append(sfn)

        # delete file
        for filename in filenames:
            if os.path.exists(filename):
                try:
                    os.remove(sfn)
                except:
                    pass

    def last_file_name(self, filename):
        """
        文件名
        """
        suffix = "."+self._now_date()
        base_filename = str(filename) + suffix
        if self.backupCount > 0:
            for i in reversed(range(0, self.backupCount)):
                number_suffix = "{:0>2d}".format(i)
                sfn = ".".join([base_filename, number_suffix])
                # 最近存在的文件
                if os.path.exists(sfn):
                    break
                else:
                    continue
            number_suffix = "{:0>2d}".format(i)
            base_filename = ".".join([base_filename, number_suffix])
        return base_filename
 
Avatar_small
Darren 说:
2024年1月17日 15:34

https://m.goaloo88.com/
https://www.goaloo88.com/
Goaloo covers 2000+ soccer/football and basketball leagues, cups and tournaments (English Premier League/EPL, and UEFA Champions League, NBA etc.).

Avatar_small
Darren 说:
2024年1月17日 15:34

https://m.goaloo88.com/
https://www.goaloo88.com/
Goaloo covers 2000+ soccer/football and basketball leagues, cups and tournaments (English Premier League/EPL, and UEFA Champions League, NBA etc.).

Avatar_small
Simplethoughts - エキサ 说:
2024年11月09日 13:05

Don’t think. Thinking is the enemy of creativity. It’s self-conscious, and anything self-conscious is lousy.
You can’t try to do things. You simply must do things.” – Ray Bradbury

Avatar_small
Naikpharmacy 说:
2025年1月08日 12:03

<a href="https://naikpharmacy.in/">NaikPharmacy</a> is a great online platform for all your healthcare needs! The variety of products, fast delivery, and excellent customer service make it my go-to for medications and wellness items. Highly recommend!

Avatar_small
Naikpharmacy 说:
2025年1月08日 12:04

Naikpharmacy is a great online platform for all your healthcare needs! The variety of products, fast delivery, and excellent customer service make it my go-to for medications and wellness items. Highly recommend!

https://naikpharmacy.in/

Avatar_small
/awaisinternational 说:
2025年1月26日 22:25

Awais International offers a fantastic range of IT products, including high-quality desktop computers, gaming laptops, and gaming PCs. They provide some of the best gaming laptops on the market, ensuring top-notch performance and reliability for gamers and professionals alike. A one-stop shop for all your tech needs!

https://awaisinternational.com/

Avatar_small
techworldnews 说:
2025年7月26日 23:31

<a href=" https://tech-world-news.com/">Stay ahead with Tech World News, your go-to source for the latest tech news, tech world updates, and new gadgets. Explore cutting-edge AI technology news, smartphone releases, and future tech trends. Get insights on cybersecurity news, blockchain updates, and in-depth tech industry analysis. Whether you're a tech enthusiast or professional, we bring you the best tech blogs with fresh, reliable, and easy-to-understand content.</a>

Avatar_small
peachyhealthy 说:
2025年7月27日 00:56

<a href=" https://www.peachyhealthy.com/">Expert tips on walking for weight loss, functional nutrition, and the best prebiotic and probiotic combination for gut health. Explore simple ways to boost mental health, improve skincare, and stay fit. Peachy Healthy offers user-friendly, research-backed wellness content designed to help you live your healthiest, happiest life naturally.</a>

Avatar_small
Golf Daily Roundup 说:
2025年7月27日 01:09

<a href=" https://golf-daily-roundup.com//"> Best golf rangefinders, including GPS and Garmin golf rangefinder options. Improve your short game with easy-to-follow golf chipping and pitching techniques. Discover fun golf chipping games and tools to enhance your skills. Perfect your shots and upgrade your game today!</a>

Avatar_small
getsetgamez 说:
2025年7月27日 01:34

<a https://getsetgamez.com/">GetSetGamez is your daily stop for Wordle hints today, the latest Wordle clue today, and the correct today. Enjoy solving fun online puzzles and games designed to boost your brainpower. Love classic gaming? Don’t miss updates on the Nintendo Switch Donkey Kong and the original Nintendo Donkey Kong game. Whether you're a puzzle lover or a retro gamer, our content is simple, user-friendly, and crafted to match your gaming interests and daily play needs.</a>

Avatar_small
academyforleadership 说:
2025年7月27日 01:58

<a href=" https://www.academyforleadershipandtraining.com//">Academy for Leadership and Training empowers individuals and organizations through expert-led leadership programs, skills development, and personalized training solutions. Our courses are designed to build confidence, inspire growth, and create strong leaders across various industries. With a focus on practical learning and real-world results, we offer a user-friendly, supportive environment for personal and professional success. Whether you're starting your journey or looking to advance, our academy helps you reach your leadership goals with clarity and purpose.</a>

Avatar_small
cryptopilotnews. 说:
2025年7月27日 02:09

<a https://cryptopilotnews.com/">Get the latest Bitcoin news, Ethereum price predictions, and crypto market updates. Discover the best altcoins to buy now, Solana price analysis, and expert insights on crypto crashes. Stay ahead with CryptoPilotNews!</a>

Avatar_small
khizra 说:
2025年8月02日 20:08

Men shoes are a must-have for every wardrobe! Whether you're dressing up for an event or going casual, the right pair can elevate your style. Comfort and quality should always come first when choosing men shoes for any occasion. Find your perfect fit today!

Avatar_small
pureganics 说:
2025年8月05日 18:17

Tried the Power Plus Tablet for male last month and it actually worked better than expected. Felt more active and confident. Make sure to stay hydrated and eat healthy alongside it for best results.

Avatar_small
Emaan Adeel 说:
2025年8月05日 18:59

You can’t talk about elegant ethnic wear without mentioning the best chiffon brands in Pakistan. Their fabric quality, color range, and designs are simply amazing. It’s no wonder they’re becoming more popular even internationally.

Avatar_small
Ndure 说:
2025年8月05日 19:19

Finding stylish men slippers that don’t look outdated is hard. Most options are either too flashy or too basic. This post covered some trendy designs I hadn’t seen before. Definitely going to try a few of these brands.

Avatar_small
The Entertainer 说:
2025年8月05日 19:38

Educational toys are more than just playthings—they’re powerful learning tools. Whether it’s math, science, or language skills, educational toys help kids grasp concepts faster. I’m amazed at how much my child has learned through hands-on play with them.

Avatar_small
Calza 说:
2025年8月05日 19:56

My mom loved this pair of chappal for women I got for her. She says they’re light, airy, and don't hurt her feet even after long hours. That says a lot about the comfort level!

Avatar_small
menshoes 说:
2025年8月09日 21:47

Men shoes here are worth every penny. The stitching is neat, the leather feels premium, and the fit is true to size. They give great support for daily wear, making them a must-have in every gentleman’s wardrobe.

Avatar_small
men shoes sale 说:
2025年8月09日 22:09

This men shoes sale is a fantastic opportunity to grab trendy and classic designs at discounted rates. From casual sneakers to formal dress shoes, there’s something for every style and occasion without breaking the bank.

Avatar_small
women shoes 说:
2025年8月09日 22:24

These women shoes are a game changer for my everyday outfits. The fit is comfortable, the material is durable, and they pair well with both casual and formal looks. I’ve already received so many compliments wearing them!

Avatar_small
women shoes sale 说:
2025年8月09日 22:40

If you love fashion and comfort, don’t miss the women shoes sale. There are so many designs to choose from—perfect for office, casual, or party wear. The discounts make it even better. Totally worth browsing today.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter