Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize real-time log monitoring by Python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to achieve real-time log monitoring in Python". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve real-time log monitoring in Python".

Introduction

Observer pattern: it is a kind of behavioral design pattern. The main concern is the responsibility of the object, which allows you to define a subscription mechanism that notifies multiple other objects that "observe" the object when an object event occurs. Used to deal with objects interacting with each other.

The observer pattern, also known as publish-subscribe pattern, defines one-to-many dependencies between objects. When an object changes its state, all dependents of that object are notified and updated in their own way.

The observer design pattern is one of the simplest behavior patterns. In the observer design pattern, the object maintains a list of dependencies (observers) so that the subject can notify all observers of its changes using any method defined by the observer.

Scenarios can be applied using observer mode

In the case of a broadcast or publish / subscribe system, you will see the use of the Observer Design pattern, which is mainly used in the following scenarios:

1. Implement event service in distributed system.

2. Broadcast or publish / read the system.

2. Used as the framework of the news machine.

3. Stock monitoring robot.

Observer pattern class diagram

Observer pattern class diagram

1. Publisher Publisher: sends interesting events to other objects. Events occur after the publisher's own state changes or performs a specific behavior. The publisher includes a subscription mechanism that allows new subscribers to join and current subscribers to leave the list.

2. Subscriber Subscriber: define notification API. In general, the interface contains only one update () update method. Method can have multiple parameters to enable the publisher to pass event details when updating.

3. Client-side Client: create the publisher and subscriber objects respectively, then register for the subscribers, and update the publishers.

Observer pattern example

If we monitor the running status of the application function and record the alarm when an exception occurs, we can subscribe to the information through the observer mode: 1, SMS 2, log 3, email

Code implementation-subscription_model.py

1. Create a subscriber class

Subscriber subscribers: all other objects that want to follow the changes in the state of the publisher.

There are three main subscriber (observer) interfaces that track events of the same publisher class. It mainly includes:

1. The _ _ init () method for each specific subscriber uses the attach () method to register with the publisher to get information updates.

2) the update () update message of the specific subscriber.

# Abstract subscriber from abc import ABCMeta,abstractmethodclass Subscriber (metaclass=ABCMeta): # method of sending messages to specific subscribers @ abstractmethod def update (self): pass# specific subscribers # 1. SMS subscribers class SMSSubscriber (Subscriber): def _ init__ (self,publisher): self.publisher = publisher self.publisher.attach (self) def update (self): print (type (self). _ _ name__ Self.publisher.getNews () # 2. Email subscriber class EmailSubscriber (Subscriber): def _ _ init__ (self, publisher): self.publisher = publisher self.publisher.attach (self) def update (self): print (type (self). _ _ name__ Self.publisher.getNews () info = self.publisher.getNews () # send email Sender_mail (info). Sender_mail () # 3, log subscription (file storage) class LoggerSubscriber (Subscriber): def _ _ init__ (self, publisher): log_dir = os.path.expanduser (r ".\ apps\ Mapview\ logs") log_file = os.path.join (log_dir "file_ {time} .log") logger.add (log_file, rotation= "100KB", retention=2) self.publisher = publisher self.publisher.attach (self) def update (self): print (type (self). _ _ name__,self.publisher.getNews () info=self.publisher.getNews () logger.info (f "{info}") 2, create publisher class

Publisher publishers: notify other objects of their own state changes, add a subscription mechanism for the publisher, and each object can subscribe to or unsubscribe from the subscriber event stream.

It mainly includes:

1) self.__subscribers = []: one is used to store the list of subscription objects

2) for subscribers to register NewsPublisher or delete subscribers.

3) several public methods for adding, deleting, or viewing subscribers in the list.

4) notifySubscribers (self): used to notify all subscribers of new information, and the sender traverses the subscription list and internally calls the update () method implemented by the specific subscriber.

5) create new messages and return the latest messages.

# create publisher class NewsPublisher: def _ _ init__ (self): self.__subscribers = [] self.__latestNews = None # add subscribers to the queue def attach (self Subscriber): self.__subscribers.append (subscriber) # remove def detach from the subscribed topic (self): return self.__subscribers.pop () # generate a list of observers def subscribers (self): return [type (x). _ _ name__ for x in self.__subscribers] # send a notification to the relevant topic subscriber def notifySubscribers (self) : for sub in self.__subscribers: # update () method sub.update () implemented by a specific viewer or subscriber # push update # create a new message def addNews (self News): self.__latestNews = news # returns the latest message And notify the observer def getNews (self): return "Got News:", self.__latestNews3, application client-Map_server_client.py

Subscribers usually need some context information to handle updates correctly. Therefore, publishers usually pass some context data as parameters of the notification method.

Here to supplement the tail left by the first article, the client instantiates the get_Map_model method to add a decorator with parameters, @ fail_data (msg=' map load failure') to add the API call failure handling mechanism, and appends log records. Here, you can further add more detailed parameters to the log, and the decorator passes parameters and declares the notification method and parameters in the interface, so that the publisher passes some context data when issuing the notification.

From apps.tools.subscription_model import NewsPublisher,LoggerSubscriber,EmailSubscriberimport functools# if the load fails Call subscriber def publisher (info): news_publisher = NewsPublisher () # for Subscribers in [EmailSubscriber, LoggerSubscriber]: for Subscribers in [LoggerSubscriber]: # eval (LoggerSubscriber) (news_publisher) Subscribers (news_publisher) print ("\ nSubscribers" News_publisher.subscribers () news_publisher.addNews (f "{info}") news_publisher.notifySubscribers () # decorator def fail_data (msg=' map load failed to load): def catch_exception (origin_func): @ functools.wraps (origin_func) def wrapper (* args, * * kwargs): try: U = origin_func (* args) * * kwargs) print ("this function executes normally:% s"% origin_func.__name__) return u except Exception as e: info = f "{msg}: {e.invalid docking _}" API call failure handling mechanism Append log "" print (info) publisher (info) # news_publisher = NewsPublisher () # LoggerSubscriber (news_publisher) # print ("\ nSubscribers" News_publisher.subscribers () # news_publisher.addNews (f "{info}") # news_publisher.notifySubscribers () return wrapper return catch_exception4, Test if _ _ name__ = ='_ _ main__': from loguru import logger from apps.tools.Sender_Email import Sender_mail news_publisher = NewsPublisher () for Subscribers in [LoggerSubscriber]: print (Subscribers) Subscribers (news_publisher) print ("\ nSubscribers" News_publisher.subscribers () news_publisher.addNews ("Map load failed!") News_publisher.notifySubscribers ()

Result

Class'_ _ main__.LoggerSubscriber'

Subscribers ['LoggerSubscriber']

LoggerSubscriber ('Got News:',' map load failed!)

2022-04-05 16 Got News:', 38 Got News:', 00.667 | INFO | _ _ map-('map' failed to load!')

Thank you for your reading, the above is the content of "how to achieve real-time log monitoring in Python". After the study of this article, I believe you have a deeper understanding of how to achieve real-time log monitoring in Python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report