如何使用Elasticsearch,Logstash和Kibana實時可視化Python中的日志

by Ritvik Khanna

Ritvik Khanna著

如何使用Elasticsearch,Logstash和Kibana實時可視化Python中的日志 (How to use Elasticsearch, Logstash and Kibana to visualise logs in Python in realtime)

什么是日志記錄? (What is logging?)

Let’s say you are developing a software product. It works remotely, interacts with different devices, collects data from sensors and provides a service to the user. One day, something goes wrong and the system is not working as expected. It might not be identifying the devices or not receiving any data from the sensors, or might have just gotten a runtime error due to a bug in the code. How can you know for sure?

假設您正在開發軟件產品。 它可以遠程工作,與不同的設備進行交互,從傳感器收集數據并為用戶提供服務。 有一天,出了點問題,系統無法按預期運行。 它可能無法識別設備或未從傳感器接收任何數據,或者可能由于代碼中的錯誤而剛發生運行時錯誤。 您怎么能確定?

Now, imagine if there are checkpoints in the system code where, if the system returns an unexpected result, it simply flags it and notifies the developer. This is the concept of logging.

現在,想象一下系統代碼中是否存在檢查點,如果系統返回意外結果,則僅對其進行標記并通知開發人員。 這就是日志記錄的概念。

Logging enables the developers to understand what the code is actually doing and how the work-flow is. A large part of software developers’ lives is monitoring, troubleshooting and debugging. Logging makes this a much easier and smoother process.

通過日志記錄,開發人員可以了解代碼的實際作用以及工作流程。 軟件開發人員的大部分工作是監視,故障排除和調試。 日志記錄使此過程變得更加輕松和順暢。

日志可視化 (Visualisation of logs)

Now, if you are an expert developer who has been developing and creating software for quite a while, then you would think that logging is not a big deal and most of our code is included with a Debug.Log('____') statement. Well, that is great but there are some other aspects of logging we can make use of.

現在,如果您是開發和創建軟件已有相當一段時間的專家開發人員,那么您會認為日志記錄并不重要,并且我們的大多數代碼都包含在Debug.Log('____')語句中。 很好,但是我們可以利用日志記錄的其他一些方面。

Visualisation of specific logged data has the following benefits:

可視化特定記錄的數據具有以下好處:

  • Monitor the operations of the system remotely.

    遠程監視系統的操作。
  • Communicate information clearly and efficiently via statistical graphics, plots and information graphics.

    通過統計圖形,曲線圖和信息圖形清晰有效地傳達信息。
  • Extract knowledge from the data visualised in the form of different graphs.

    從以不同圖形形式可視化的數據中提取知識。
  • Take necessary actions to better the system.

    采取必要的措施來改善系統。

There are a number of ways we can visualise raw data. There are a number of libraries in the Python and R programming languages that can help in plotting graphs. You can learn more about it here. But in this post, I am not going to discuss about above mentioned methods. Have you ever heard about the ELK stack?

我們可以通過多種方式可視化原始數據。 Python和R編程語言中有許多庫可以幫助繪制圖形。 您可以在此處了解更多信息。 但是在這篇文章中,我將不討論上述方法。 您聽說過ELK堆棧嗎?

ELK堆棧 (ELK stack)

E — Elasticsearch, L — Logstash, K — Kibana

E- Elasticsearch ,L- Logstash K- Kibana

Let me give a brief introduction to it. The ELK stack is a collection of three open source softwares that helps in providing realtime insights about data that can be either structured or unstructured. One can search and analyse data using its tools with extreme ease and efficiently.

讓我對其進行簡要介紹。 ELK堆棧是三個開源軟件的集合,這些軟件有助于提供有關可結構化或非結構化數據的實時見解。 一個人可以使用其工具輕松高效地搜索和分析數據。

Elasticsearch is a distributed, RESTful search and analytics engine capable of solving a growing number of use cases. As the heart of the Elastic Stack, it centrally stores your data so you can discover the expected and uncover the unexpected. Elasticsearch lets you perform and combine many types of searches — structured, unstructured, geo, metric etc. It is built on Java programming language, which enables Elasticsearch to run on different platforms. It enables users to explore very large amount of data at very high speed.

Elasticsearch是一個分布式的RESTful搜索和分析引擎,能夠解決越來越多的用例。 作為Elastic Stack的核心,它集中存儲您的數據,以便您發現期望的數據并發現意外的數據。 Elasticsearch可讓您執行和組合多種類型的搜索-結構化,非結構化,地理,度量等。它基于Java編程語言構建,從而使Elasticsearch可以在不同平臺上運行。 它使用戶能夠以很高的速度瀏覽大量數據。

Logstash is an open source, server-side data processing pipeline that ingests data from a multitude of sources simultaneously, transforms it, and then sends it to your favourite “stash” (like Elasticsearch). Data is often scattered or siloed across many systems in many formats. Logstash supports a variety of inputs that pull in events from a multitude of common sources, all at the same time. Easily ingest from your logs, metrics, web applications, data stores, and various AWS services, all in continuous, streaming fashion. Logstash has a pluggable framework featuring over 200 plugins. Mix, match, and orchestrate different inputs, filters, and outputs to work in pipeline harmony.

Logstash是一個開放源代碼的服務器端數據處理管道,它同時從多個源中提取數據,進行轉換,然后將其發送到您喜歡的“存儲”(例如Elasticsearch)。 數據通常以多種格式分散或分散在許多系統中。 Logstash支持各種輸入,這些輸入可同時從多個常見來源獲取事件。 輕松以連續,流式方式從日志,指標,Web應用程序,數據存儲和各種AWS服務中提取數據。 Logstash有一個可插入框架,其中包含200多個插件。 混合,匹配和編排不同的輸入,過濾器和輸出,以協調管道。

Kibana is an open source analytics and visualisation platform designed to work with Elasticsearch. You use Kibana to search, view, and interact with data stored in Elasticsearch indices. You can easily perform advanced data analysis and visualise your data in a variety of charts, tables, and maps. Kibana makes it easy to understand large volumes of data. Its simple, browser-based interface enables you to quickly create and share dynamic dashboards that display changes to Elasticsearch queries in real time.

Kibana是一個旨在與Elasticsearch一起使用的開源分析和可視化平臺。 您可以使用Kibana搜索,查看和與Elasticsearch索引中存儲的數據進行交互。 您可以輕松地執行高級數據分析,并在各種圖表,表格和地圖中可視化數據。 使用Kibana可以輕松理解大量數據。 其簡單的基于瀏覽器的界面使您能夠快速創建和共享動態儀表板,以實時顯示對Elasticsearch查詢的更改。

To get a better picture of the workflow of how the three softwares interact with each other, refer to the following diagram:

為了更好地了解這三種軟件如何交互的工作流程,請參考下圖:

實作 (Implementation)

登錄Python (Logging in Python)

Here, I chose to explain the implementation of logging in Python because it is the most used language for projects involving communication between multiple machines and internet of things. It’ll help give you an overall idea of how it works.

在這里,我選擇解釋使用Python進行日志記錄的實現,因為它是涉及多臺機器與物聯網之間通信的項目的最常用語言。 它會幫助您全面了解其工作原理。

Python provides a logging system as a part of its standard library, so you can quickly add logging to your application.

Python提供了一個日志記錄系統作為其標準庫的一部分,因此您可以快速將日志記錄添加到應用程序中。

import logging

In Python, logging can be done at 5 different levels that each respectively indicate the type of event. There are as follows:

在Python中,可以在5個不同的級別上進行日志記錄,每個級別分別指示事件的類型。 內容如下:

  • Info — Designates informational messages that highlight the progress of the application at coarse-grained level.

    信息 —指定參考消息,以粗粒度級別突出顯示應用程序的進度。

  • Debug — Designates fine-grained informational events that are most useful to debug an application.

    調試 -指定對調試應用程序最有用的細粒度信息事件。

  • Warning — Designates potentially harmful situations.

    警告 —表示潛在的有害情況。

  • Error — Designates error events that might still allow the application to continue running.

    錯誤 —指定可能仍允許應用程序繼續運行的錯誤事件。

  • Critical — Designates very severe error events that will presumably lead the application to abort.

    嚴重 -指定非常嚴重的錯誤事件,可能會導致應用程序中止。

Therefore depending on the problem that needs to be logged, we use the defined level accordingly.

因此,根據需要記錄的問題,我們相應地使用定義的級別。

Note: Info and Debug do not get logged by default as logs of only level Warning and above are logged.

注意 :默認情況下,不會記錄信息和調試信息,因為僅記錄警告和更高級別的日志。

Now to give an example and create a set of log statements to visualise, I have created a Python script that logs statements of specific format and a message.

現在給出一個示例并創建一組可視化的日志語句,我創建了一個Python腳本,用于記錄特定格式的語句和一條消息。

Here, the log statements will append to a file named logFile.txt in the specified format. I ran the script for three days at different time intervals creating a file containing logs at random like below:

在這里,日志語句將以指定格式追加到名為logFile.txt的文件中。 我以不同的時間間隔運行了三天的腳本,創建了一個包含日志的文件,如下所示:

設置Elasticsearch,Logstash和Kibana (Setting up Elasticsearch, Logstash and Kibana)

At first let’s download the three open source softwares from their respective links [elasticsearch],[logstash]and[kibana]. Unzip the files and put all three in the project folder.

首先,讓我們從下載他們相應的鏈接三個開源軟件[ elasticsearch ],[ logstash ]和[ kibana 。 解壓縮文件,然后將所有三個文件放入項目文件夾。

Let’s get started.

讓我們開始吧。

Step 1 — Set up Kibana and Elasticsearch on the local system. We run Kibana by the following command in the bin folder of Kibana.

步驟1 —在本地系統上設置Kibana和Elasticsearch。 我們通過以下命令在Kibana的bin文件夾中運行Kibana。

bin\kibana

Similarly, Elasticsearch is setup like this:

同樣,Elasticsearch的設置如下:

bin\elasticsearch

Now, in the two separate terminals we can see both of the modules running. In order to check that the services are running open localhost:5621 and localhost:9600.

現在,在兩個單獨的終端中,我們可以看到兩個模塊都在運行。 為了檢查服務是否正在運行,請打開localhost:5621localhost:9600

After both the services are successfully running we use Logstash and Python programs to parse the raw log data and pipeline it to Elasticsearch from which Kibana queries data.

在兩個服務都成功運行之后,我們使用Logstash和Python程序解析原始日志數據,并將其通過管道傳輸到Elasticsearch,Kibana將從中查詢數據。

Step 2— Now let’s get on with Logstash. Before starting Logstash, a Logstash configuration file is created in which the details of input file, output location, and filter methods are specified.

第2步 -現在讓我們繼續進行Logstash。 啟動Logstash之前,將創建一個Logstash配置文件,其中指定了輸入文件,輸出位置和過濾器方法的詳細信息。

This configuration file plays a major role in the ELK stack. Take a look at filter{grok{…}} line. This is a Grok filter plugin. Grok is a great way to parse unstructured log data into something structured and queryable. This tool is perfect for syslog logs, apache and other webserver logs, mysql logs, and in general, any log format that is generally written for humans and not computer consumption. This grok pattern mentioned in the code tells Logstash how to parse each line entry in our log file.

此配置文件在ELK堆棧中起主要作用。 看一下filter {grok {…}}行。 這是一個Grok過濾器插件。 Grok是將非結構化日志數據解析為結構化和可查詢內容的好方法。 該工具非常適合syslog日志,apache和其他Web服務器日志,mysql日志,以及通常用于人類而非計算機使用的任何日志格式。 代碼中提到的這種grok模式告訴Logstash如何解析日志文件中的每個行條目。

Now save the file in Logstash folder and start the Logstash service.

現在,將文件保存在Logstash文件夾中,然后啟動Logstash服務。

bin\logstash –f logstash-simple.conf

In order to learn more about configuring logstash, click [here].

為了了解更多關于配置logstash的信息,請單擊[ 此處 ]。

Step 3 — After this the parsed data from the log files will be available in Kibana management at localhost:5621 for creating different visuals and dashboards. To check if Kibana is receiving any data, in the management tab of Kibana run the following command:

步驟3 —之后,將從日志文件中解析的數據在Kibana管理中的localhost:5621可用,以創建不同的圖像和儀表板。 要檢查Kibana是否正在接收任何數據,請在Kibana的管理選項卡中運行以下命令:

localhost:9200/_cat/indices?v

This will display all the indexes. For every visualisation, a new Index pattern has to be selected from dev tools, after which various visualisation techniques are used to create a dashboard.

這將顯示所有索引。 對于每次可視化,都必須從開發工具中選擇新的索引模式,然后使用各種可視化技術來創建儀表板。

使用Kibana的儀表板 (Dashboard Using Kibana)

After setting up everything, now it’s time to create graphs in order to visualise the log data.

設置完所有內容之后,現在該創建圖表以可視化日志數據了。

After opening the Kibana management homepage, we will be asked to create a new index pattern. Enter index_name* in the Index pattern field and select @timestamp in the Time Filter field name dropdown menu.

打開Kibana管理主頁后,將要求我們創建一個新的索引模式。 在索引模式字段中輸入index_name*然后時間過濾器字段名稱下拉菜單中選擇@timestamp

Now to create graphs, we go to the Visualize tab.

現在創建圖表,我們轉到“ 可視化”選項卡。

Select a new visualisation, choose a type of graph and index name, and depending on your axis requirements, create a graph. We can create a histogram with y-axis as the count and x-axis with the log-level keyword or the timestamp.

選擇一個新的可視化效果,選擇一種圖形和索引名稱,然后根據您的軸要求創建一個圖形。 我們可以使用log-level關鍵字時間戳創建以y軸計數x軸的直方圖

After creating a few graphs, we can add all the required visualisations and create a Dashboard, like below:

創建一些圖形后,我們可以添加所有必需的可視化效果并創建一個Dashboard ,如下所示:

Note — Whenever the logs in the log file get updated or appended to the previous logs, as long as the three services are running the data in elasticsearch and graphs in kibana will automatically update according to the new data.
注—只要日志文件中的日志被更新或追加到以前的日志中,只要這三個服務都在運行,elasticsearch中的數據和kibana中的圖形將根據新數據自動更新。

結語 (Wrapping up)

Logging can be an aid in fighting errors and debugging programs instead of using a print statement. The logging module divides the messages according to different levels. This results in better understanding of the code and how the call flow goes without interrupting the program.

日志記錄可以幫助您解決錯誤和調試程序,而不是使用print語句。 日志記錄模塊根據不同的級別劃分消息。 這樣可以更好地理解代碼以及調用流程如何進行而不會中斷程序。

The visualisation of data is a necessary step in situations where a huge amount of data is generated every single moment. Data-Visualization tools and techniques offer executives and other knowledge workers new approaches to dramatically improve their ability to grasp information hiding in their data. Rapid identification of error logs, easy comprehension of data and highly customisable data visuals are some of the advantages. It is one of the most constructive way of organising raw data.

在每時每刻都會生成大量數據的情況下,數據可視化是必不可少的步驟。 數據可視化工具和技術為高管和其他知識工作者提供了新的方法,可以大大提高他們掌握隱藏在數據中的信息的能力。 快速識別錯誤日志,輕松理解數據和高度可定制的數據外觀是其中的一些優勢。 它是組織原始數據的最有建設性的方法之一。

For further reference you can refer to the official ELK documentation from here — https://www.elastic.co/learn and on logging in python — https://docs.python.org/2/library/logging.html

如需進一步參考,你可以參考官方文檔ELK從這里- https://www.elastic.co/learn并在Python記錄- https://docs.python.org/2/library/logging.html

翻譯自: https://www.freecodecamp.org/news/how-to-use-elasticsearch-logstash-and-kibana-to-visualise-logs-in-python-in-realtime-acaab281c9de/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392340.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392340.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392340.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

感想篇:4)越來越精簡的機械設計

本章目的:述說機械設計方向的發展。 kiss原則需要后期追加。 作者在寫電機選用章節時想到了機構的問題,機械發展的前半生對機構來說無疑有會輝煌的成就,各種各樣的機構能取得難以置信的成效,最終甚至可以說上升到了藝術的階段。如…

php api json,PHP API接口必備之輸出json格式數據實例詳解

這篇文章主要給大家介紹了關于PHP API接口必備之輸出json格式數據的相關資料文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。前言我們在日常的開發工作中免不了要寫接口,json格式文本的輸出是制…

leetcode 228. 匯總區間

給定一個無重復元素的有序整數數組 nums 。 返回 恰好覆蓋數組中所有數字 的 最小有序 區間范圍列表。也就是說,nums 的每個元素都恰好被某個區間范圍所覆蓋,并且不存在屬于某個范圍但不屬于 nums 的數字 x 。 列表中的每個區間范圍 [a,b] 應該按如下格…

接受拒絕算法_通過算法拒絕大學學位

接受拒絕算法數據科學 (Data Science) Nina was close to tears when she accused Nick Gibb of ruining her life. Nina is an 18 year old about to leave school and go on to higher education; Gibb is the UK government’s schools minister.妮娜(Nina)指責尼克吉布(Nic…

淺談傳統企業網絡運營那些事兒

網絡的變革、更新推動的速度很快,小到出門購物全方位在原基礎的微信/支付寶等第三方支付等,隨著微信公眾號/微信小程序等"輕"級傳播推廣渠道的發展,以及客觀的傳統企業在互聯網的沖擊下,同樣的價格比服務?比…

2019cvpr cv_如何編寫軟件工程簡歷(CV):權威指南(于2019年更新)

2019cvpr cvby the onset從發病開始 如何編寫軟件工程簡歷(CV):權威指南(于2019年更新) (How to write a Software Engineering resume (CV): the definitive guide (Updated for 2019)) While the debate still continues regarding the long term future of the …

leetcode 1202. 交換字符串中的元素(并查集)

給你一個字符串 s,以及該字符串中的一些「索引對」數組 pairs,其中 pairs[i] [a, b] 表示字符串中的兩個索引(編號從 0 開始)。 你可以 任意多次交換 在 pairs 中任意一對索引處的字符。 返回在經過若干次交換后,s …

vim 下web開發html css js插件

Vim下的Web開發之html,CSS,javascript插件HTML 下載HTML.zip 解壓HTML.zip,然后將里面的所有文件copy到C:\Program Files\Vim\vimfiles目錄下首先,你應該把“ filetype plugin on ”寫入你的vimrc。重啟vim。新建一個test.html文件。用gvim打開按 "…

為什么用scrum_為什么Scrum糟糕于數據科學

為什么用scrumScrum is a popular methodology for PM in software engineering and recently the trend has carried over to data science. While the utility of Scrum in standard software engineering may remain up for debate, here I will detail why it has unquesti…

Android_Event Bus 的基本用法

1 //事件總線分發2 public class MainActivity extends ActionBarActivity {3 Button button;4 TextView text;5 6 Override7 protected void onCreate(Bundle savedInstanceState) {8 super.onCreate(savedInstanceState);9 setContentView(R…

leetcode 1203. 項目管理(拓撲排序)

公司共有 n 個項目和 m 個小組,每個項目要不無人接手,要不就由 m 個小組之一負責。 group[i] 表示第 i 個項目所屬的小組,如果這個項目目前無人接手,那么 group[i] 就等于 -1。(項目和小組都是從零開始編號的&#xf…

谷歌cloud_通過使用Google Cloud ML大規模提供機器學習模型,我們學到了什么

谷歌cloudby Daitan通過大潭 通過使用Google Cloud ML大規模提供機器學習模型,我們學到了什么 (What we learned by serving machine learning models at scale using Google Cloud ML) By Bruno Schionato, Diego Domingos, Fernando Moraes, Gustavo Rozato, Isa…

php企業黃頁源碼,PHPCMS 企業黃頁模塊 v9 GBK 正式版

PHPCMS V9采用OOP(面向對象)方式進行基礎運行框架搭建。模塊化開發方式做為功能開發形式。框架易于功能擴展,代碼維護,優秀的二次開發能力,可滿足所有網站的應用需求。PHPCMS V9企業黃頁主要特色1、模型自定義,支持模型添加、修改…

跨域配置

SpringBoot跨域配置 我們的后端使用Spring Boot。Spring Boot跨域非常簡單,只需書寫以下代碼即可。 Configuration public class CustomCORSConfiguration {private CorsConfiguration buildConfig() {CorsConfiguration corsConfiguration new CorsConfiguration(…

fromEvent

fromEvent(selector,Event) 實際效果圖 這個功能和cad 3dmax里面的鼠標定位功能一致吧,是不是有點小成就? 轉載于:https://www.cnblogs.com/xiongwei2017/p/7074180.html

java開發第一天上班_從第一天開始,如何成為一名優秀的團隊合作伙伴,成為初級開發人員

java開發第一天上班One of the many things you might be asking yourself when starting your software development career is:在開始軟件開發職業時,您可能會問自己很多事情之一: “How do I REALLY contribute to my new team?”“我如何真正為我的…

java虛擬機編譯文件,理解Java虛擬機(1)之一個.java文件編譯成.class文件發生了什么...

理解Java虛擬機(1)之一個.java文件編譯成.class文件發生了什么最近在看《深入理解Java虛擬機》弄明白了很多java的底層知識,決定分幾部分總結下,從.java文件編譯,到類加載機制,內存分配垃圾回收機制,線程并發&#xff…

leetcode 684. 冗余連接()

在本問題中, 樹指的是一個連通且無環的無向圖。 輸入一個圖,該圖由一個有著N個節點 (節點值不重復1, 2, …, N) 的樹及一條附加的邊構成。附加的邊的兩個頂點包含在1到N中間,這條附加的邊不屬于樹中已存在的邊。 結果圖是一個以邊組成的二維數組。每一…

Go-如何讀取yaml,json,ini等配置文件

1. json使用 JSON 應該比較熟悉,它是一種輕量級的數據交換格式。層次結構簡潔清晰 ,易于閱讀和編寫,同時也易于機器解析和生成。 創建 conf.json:{"enabled": true,"path": "/usr/local" }新建conf…

SQL轉化為MapReduce的過程

轉載:http://www.cnblogs.com/yaojingang/p/5446310.html 在了解了MapReduce實現SQL基本操作之后,我們來看看Hive是如何將SQL轉化為MapReduce任務的,整個編譯過程分為六個階段: Antlr定義SQL的語法規則,完成SQL詞法&am…