python打印日志文件_Python-在文件中打印日志

python打印日志文件

If you want to print python logs in a file rather than on the console then we can do so using the basicConfig() method by providing filename and filemode as parameter.

如果要在文件中而不是在控制台上打印python日志,则可以使用basicConfig()方法通过提供filenamefilename filemode作为参数来进行filemode

The format of the message can be specified by using format parameter in basicConfig() method.

可以通过使用basicConfig()方法中的format参数来指定消息的 format

Let us take a basic example to print logs to a file rather than on the console. The code snippet is given below:

让我们以一个基本示例为例,将日志打印到文件而不是在控制台上。 该代码段如下所示:

import logging    # first of all import the module

logging.basicConfig(filename='std.log', filemode='w', format='%(name)s - %(levelname)s - %(message)s')
logging.warning('This message will get logged on to a file')

root - ERROR - This message will get logged on to a file

根-错误-此消息将登录到文件

The above output shows how the message will look like but keep in mind it will be written to a file named std.log instead of the console.

上面的输出显示了消息的外观,但请记住,它将被写入名为std.log文件,而不是控制台。

In the above code, the filemode is set to w, which means the log file is opened in “write mode” each time basicConfig() is called, and after each run of the program, it will rewrite the file.

在上面的代码中, filemode设置为w ,这意味着每次调用basicConfig()basicConfig()以“ 写入模式 ”打开日志文件,并且在每次运行该程序后,它将重写该文件。

The default configuration for filemode is a, that is append, which means that logs will be appended to the log file and adding logs to the existing logs.

对于FILEMODE默认配置为a ,即追加 ,这意味着日志将被追加到日志文件并添加日志到现有日志。

Python日志记录-将日志存储在文件中 (Python Logging - Store Logs in a File)

There are some basic steps and these are given below:

有一些基本步骤,这些步骤如下:

  1. First of all, simply import the logging module just by writing import logging.

    首先,只需编写import logging即可导入日志记录模块。

  2. The second step is to create and configure the logger. To configure logger to store logs in a file, it is mandatory to pass the name of the file in which you want to record the events.

    第二步是创建和配置记录器。 要配置记录器以将日志存储在文件中,必须传递要在其中记录事件的文件名称

  3. In the third step, the format of the logger can also be set. Note that by default, the file works in append mode but we can change that to write mode if required.

    在第三步中,还可以设置记录器的格式。 请注意,默认情况下,该文件在附加模式下工作,但是如果需要,我们可以将其更改为写入模式。

  4. You can also set the level of the logger.

    您还可以设置记录器的级别。

So let's move on to the code now:

现在让我们继续执行代码:

#importing the module 
import logging 

#now we will Create and configure logger 
logging.basicConfig(filename="std.log", 
					format='%(asctime)s %(message)s', 
					filemode='w') 

#Let us Create an object 
logger=logging.getLogger() 

#Now we are going to Set the threshold of logger to DEBUG 
logger.setLevel(logging.DEBUG) 

#some messages to test
logger.debug("This is just a harmless debug message") 
logger.info("This is just an information for you") 
logger.warning("OOPS!!!Its a Warning") 
logger.error("Have you try to divide a number by zero") 
logger.critical("The Internet is not working....")

The above code will write some messages to file named std.log. If we will open the file then the messages will be written as follows:

上面的代码会将一些消息写入名为std.log的文件。 如果我们打开文件,则消息将编写如下:

2020-06-19 12:48:00,449 - This is just harmless debug message 2020-06-19 12:48:00,449 - This is just an information for you 2020-06-19 12:48:00,449 - OOPS!!!Its a Warning 2020-06-19 12:48:00,449 - Have you try to divide a number by zero 2020-06-19 12:48:00,449 - The Internet is not working...

2020-06-19 12:48:00,449-这只是无害的调试消息2020-06-19 12:48:00,449-这只是给您的信息2020-06-19 12:48:00,449-糟糕!它是一个警告2020-06-19 12:48:00,449-您是否尝试将数字除以零2020-06-19 12:48:00,449-互联网无法正常工作...

You can change the format of logs, log level or any other attribute of the LogRecord along with setting the filename to store logs in a file along with the mode.

您可以更改日志的格式,日志级别或LogRecord任何其他属性,以及设置文件名以将日志和模式一起存储在文件中。

翻译自: https://www.studytonight.com/python/python-logging-in-file

python打印日志文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值