找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 2449|回复: 3

[已解决] 请教一个python logging问题

1

主题

3

帖子

3

积分

贫民

积分
3
天天向上 发表于 2017-11-20 08:28:02 | 显示全部楼层 |阅读模式
做了一个logger, 在mac上面正常输出和转变logger:
        fileHandler = logging.handlers.TimedRotatingFileHandler(log_file_path, when='s', interval=10, backupCount=10)


同样的程序在windows 10上面出错:
WindowsError: [Error 32] The process cannot access the file because it is being used by another process

正常情况应该是到10秒回产生另外一个log文件。mac上面没有问题,大家给看看是什么问题
回复

使用道具 举报

1

主题

3

帖子

3

积分

贫民

积分
3
天天向上  楼主| 发表于 2017-11-20 08:42:00 | 显示全部楼层
import time
import logging
import logging.handlers
log_file_name = 'TimedRotatingFileHandler.log'
logging_level = logging.DEBUG
try:
    # set TimedRotatingFileHandler for root
    formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s')
    # use very short interval for this example, typical 'when' would be 'midnight' and no explicit interval
    handler = logging.handlers.TimedRotatingFileHandler(log_file_name, when="S", interval=30, backupCount=10)
    handler.setFormatter(formatter)
    logger = logging.getLogger() # or pass string to give it a name
    logger.addHandler(handler)
    logger.setLevel(logging_level)
    # generate lots of example messages
    for i in range(10000):
        time.sleep(0.1)
        logger.debug('i=%d' % i)
        logger.info('i=%d' % i)
        logger.warn('i=%d' % i)
        logger.error('i=%d' % i)
        logger.critical('i=%d' % i)
except KeyboardInterrupt:
    # handle Ctrl-C
    logging.warn("Cancelled by user")
except Exception as ex:
    # handle unexpected script errors
    logging.exception("Unhandled error\n{}".format(ex))
    raise
finally:
    # perform an orderly shutdown by flushing and closing all handlers; called at application exit and no further use of the logging system should be made after this call.
    logging.shutdown()
回复 支持 1 反对 0

使用道具 举报

1

主题

3

帖子

3

积分

贫民

积分
3
天天向上  楼主| 发表于 2017-11-20 09:51:48 | 显示全部楼层
不用管这个问题了,是别的问题。证明了无数次。问题解决不了就是自己懒的问题,以后勤快点。辛苦看这个问题的朋友了,谢谢
回复 支持 反对

使用道具 举报

50

主题

1057

帖子

1108

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1108

活跃会员热心会员最佳导师默默耕耘突出贡献优秀版主荣誉管理论坛元老

blueelwang 发表于 2017-11-20 17:38:07 | 显示全部楼层
天天向上 发表于 2017-11-20 09:51
不用管这个问题了,是别的问题。证明了无数次。问题解决不了就是自己懒的问题,以后勤快点。辛苦看这个问题 ...

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表