|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
首先我定义一个类:
- import logging
- class Logger():
- def __init__(self, logfile='output.log'):
- self.logfile = logfile
- self.logger = logging.getLogger(__name__)
- logging.basicConfig(
- format='[%(asctime)s] - %(message)s',
- datefmt='%Y_%m_%d %H:%M:%S',
- level=logging.INFO,
- filename=self.logfile
- )
- def info(self, msg, *args):
- msg = str(msg)
- if args:
- print(msg % args)
- self.logger.info(msg, *args)
- else:
- print(msg)
- self.logger.info(msg)
复制代码
然后我在下面的代码中使用了上面这个类:
- import Logger
- for foo in range(0, 10):
- file_name = "./out/" + str(foo) + ".log"
- logger = Logger(file_name)
- logger.info(str(foo))
复制代码
我期望的是得到名字为0~9共10个.log文件,并且每个文件中的内容就是名字本身,比如:
“5.log”文件中是“[2022_05_23 22:42:36] - 5”
“6.log”文件中是“[2022_05_23 22:42:36] - 6”
然而上述代码运行后却得到只有一个“0.log”文件,其中内容为:
[2022_05_23 22:42:36] - 0
[2022_05_23 22:42:36] - 1
[2022_05_23 22:42:36] - 2
[2022_05_23 22:42:36] - 3
[2022_05_23 22:42:36] - 4
[2022_05_23 22:42:36] - 5
[2022_05_23 22:42:36] - 6
[2022_05_23 22:42:36] - 7
[2022_05_23 22:42:36] - 8
[2022_05_23 22:42:36] - 9
请问我要如何解决该问题?
|
|