專欄集錦,大佬們可以收藏以備不時(shí)之需
Spring Cloud實(shí)戰(zhàn)專欄:https://blog.csdn.net/superdangbo/category_9270827.html
Python 實(shí)戰(zhàn)專欄:https://blog.csdn.net/superdangbo/category_9271194.html
Logback 詳解專欄:https://blog.csdn.net/superdangbo/category_9271502.html
tensorflow專欄:https://blog.csdn.net/superdangbo/category_8691332.html
Redis專欄:https://blog.csdn.net/superdangbo/category_9950790.html
Spring Cloud實(shí)戰(zhàn):
Spring Cloud 實(shí)戰(zhàn) | 解密Feign底層原理,包含實(shí)戰(zhàn)源碼
Spring Cloud 實(shí)戰(zhàn) | 解密負(fù)載均衡Ribbon底層原理,包含實(shí)戰(zhàn)源碼
1024程序員節(jié)特輯文章:
1024程序員狂歡節(jié)特輯 | ELK+ 協(xié)同過(guò)濾算法構(gòu)建個(gè)性化推薦引擎,智能實(shí)現(xiàn)“千人千面”
1024程序員節(jié)特輯 | 解密Spring Cloud Hystrix熔斷提高系統(tǒng)的可用性和容錯(cuò)能力
1024程序員節(jié)特輯 | ELK+ 用戶畫像構(gòu)建個(gè)性化推薦引擎,智能實(shí)現(xiàn)“千人千面”
1024程序員節(jié)特輯 | OKR VS KPI誰(shuí)更合適?
1024程序員節(jié)特輯 | Spring Boot實(shí)戰(zhàn) 之 MongoDB分片或復(fù)制集操作
Spring實(shí)戰(zhàn)系列文章:
Spring實(shí)戰(zhàn) | Spring AOP核心秘笈之葵花寶典
Spring實(shí)戰(zhàn) | Spring IOC不能說(shuō)的秘密?
國(guó)慶中秋特輯系列文章:
國(guó)慶中秋特輯(八)Spring Boot項(xiàng)目如何使用JPA
國(guó)慶中秋特輯(七)Java軟件工程師常見20道編程面試題
國(guó)慶中秋特輯(六)大學(xué)生常見30道寶藏編程面試題
國(guó)慶中秋特輯(五)MySQL如何性能調(diào)優(yōu)?下篇
國(guó)慶中秋特輯(四)MySQL如何性能調(diào)優(yōu)?上篇
國(guó)慶中秋特輯(三)使用生成對(duì)抗網(wǎng)絡(luò)(GAN)生成具有節(jié)日氛圍的畫作,深度學(xué)習(xí)框架 TensorFlow 和 Keras 來(lái)實(shí)現(xiàn)
國(guó)慶中秋特輯(二)浪漫祝福方式 使用生成對(duì)抗網(wǎng)絡(luò)(GAN)生成具有節(jié)日氛圍的畫作
國(guó)慶中秋特輯(一)浪漫祝福方式 用循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)或長(zhǎng)短時(shí)記憶網(wǎng)絡(luò)(LSTM)生成祝福詩(shī)詞
1、Python中l(wèi)ogging日志庫(kù)
Python 的日志庫(kù)(logging)提供了一個(gè)靈活的日志記錄系統(tǒng)。以下是如何使用 Python 的 logging 庫(kù)進(jìn)行日志記錄的簡(jiǎn)單示例:
- 首先,導(dǎo)入 logging 庫(kù):
import logging
- 接下來(lái),配置日志記錄的基本設(shè)置,例如日志級(jí)別、日志格式和日志文件名:
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s',
filename='app.log',
filemode='w')
這里,我們將日志級(jí)別設(shè)置為 DEBUG,日志格式為時(shí)間 - 級(jí)別 - 消息
,并將日志記錄保存到名為 app.log 的文件中。
3. 然后,使用 logging 庫(kù)的不同級(jí)別和方法進(jìn)行日志記錄:
logging.debug('This is a debug log.')
logging.info('This is an info log.')
logging.warning('This is a warning log.')
logging.error('This is an error log.')
logging.critical('This is a critical log.')
這些日志記錄將分別對(duì)應(yīng)不同的日志級(jí)別,從低到高依次為:DEBUG、INFO、WARNING、ERROR、CRITICAL。
4. 運(yùn)行上述代碼后,您將在當(dāng)前目錄下看到一個(gè)名為 app.log 的日志文件,其中包含了您剛剛記錄的日志。文件內(nèi)容如下:
2021-01-01 12:34:56,789 - DEBUG - This is a debug log.
2021-01-01 12:34:56,789 - INFO - This is an info log.
2021-01-01 12:34:56,789 - WARNING - This is a warning log.
2021-01-01 12:34:56,789 - ERROR - This is an error log.
2021-01-01 12:34:56,789 - CRITICAL - This is a critical log.
以上示例展示了如何使用 Python 的 logging 庫(kù)進(jìn)行基本的日志記錄。您可以根據(jù)實(shí)際需求調(diào)整日志級(jí)別、格式和其他設(shè)置。如果您希望同時(shí)在控制臺(tái)和文件中輸出日志,可以不設(shè)置filemode
參數(shù),這樣日志將同時(shí)輸出到控制臺(tái)和文件。此外,您還可以使用handlers
參數(shù)配置日志處理器,以實(shí)現(xiàn)更復(fù)雜的日志記錄需求。
2、使用 Python 的日志庫(kù)(logging)和 pandas 庫(kù)對(duì)日志數(shù)據(jù)進(jìn)行分析
在 Python 中,實(shí)現(xiàn)日志收集和分析的方法有很多,這里我為您介紹一個(gè)簡(jiǎn)單的示例,使用 Python 的日志庫(kù)(logging)和 pandas 庫(kù)對(duì)日志數(shù)據(jù)進(jìn)行分析。
首先,請(qǐng)確保已安裝 pandas 庫(kù),如果尚未安裝,請(qǐng)使用以下命令進(jìn)行安裝:
pip install pandas
以下是一個(gè)簡(jiǎn)單的 Python 日志收集和分析示例:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-752475.html
- 導(dǎo)入所需的庫(kù):
import logging
import pandas as pd
- 設(shè)置日志格式:
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s')
- 模擬日志數(shù)據(jù):
log_data = [
{'timestamp': '2021-01-01 00:00:00', 'level': 'DEBUG', 'message': 'This is a debug log.'},
{'timestamp': '2021-01-01 00:01:00', 'level': 'INFO', 'message': 'This is an info log.'},
{'timestamp': '2021-01-01 00:02:00', 'level': 'WARNING', 'message': 'This is a warning log.'},
{'timestamp': '2021-01-01 00:03:00', 'level': 'ERROR', 'message': 'This is an error log.'},
]
- 將日志數(shù)據(jù)保存到 CSV 文件:
import os
if not os.path.exists('logs'):
os.makedirs('logs')
with open('logs/log_data.csv', 'w', newline='', encoding='utf-8') as csvfile:
fieldnames = ['timestamp', 'level', 'message']
writer = pd.writer(csvfile, fieldnames=fieldnames)
writer.writerow(fieldnames)
for log in log_data:
writer.writerow(log)
- 使用 pandas 讀取 CSV 文件,并對(duì)其進(jìn)行分析:
import pandas as pd
log_df = pd.read_csv('logs/log_data.csv')
# 按日志級(jí)別統(tǒng)計(jì)數(shù)量
level_counts = log_df['level'].value_counts()
print("日志級(jí)別統(tǒng)計(jì):")
print(level_counts)
# 按時(shí)間分析日志
hour_counts = log_df.groupby('timestamp').hour().value_counts()
print("\n按小時(shí)統(tǒng)計(jì):")
print(hour_counts)
# 按日志級(jí)別和時(shí)間進(jìn)行分組,統(tǒng)計(jì)日志數(shù)量
grouped_logs = log_df.groupby(['level', 'timestamp']).size().unstack(fill_value=0)
print("\n按級(jí)別和時(shí)間分組的日志數(shù)量:")
print(grouped_logs)
以上代碼將模擬的日志數(shù)據(jù)保存到 CSV 文件,并使用 pandas 對(duì)其進(jìn)行簡(jiǎn)單的統(tǒng)計(jì)和分析。實(shí)際應(yīng)用中,您可以根據(jù)需要修改日志收集和分析的邏輯。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-752475.html
到了這里,關(guān)于Python實(shí)戰(zhàn) | 使用 Python 的日志庫(kù)(logging)和 pandas 庫(kù)對(duì)日志數(shù)據(jù)進(jìn)行分析的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!