<redis in action> 5.1.2 Common logs

Source: Internet
Author: User

Article Source: https://redislabs.com/ebook/redis-in-action/part-2-core-concepts-2/ Chapter-5-using-redis-for-application-support/5-1-logging-to-redis/5-1-2-common-logs

If you have already run log_recent (), you may find that although it is very useful in getting what is going on in the system, it does not include telling you if you missed some important information, and by recording the frequency of certain information, you can decide which is important.

To find out the frequency of a message, a simple but effective way is to store this information as a member in Zset, the score (score) is the frequency of its occurrence, in order to ensure that we only see the most recent common information, we will adjust (rotate rollover) once per hour record, So we won't miss any news, we will keep the last hour valuable news. Here is the code implementation

#代码来源: https://github.com/huangz1990/riacn-code/blob/master/ch05_listing_source.py#L54
defLog_common (conn, name, message, Severity=logging.info, timeout=5): #sets the level of the log. Severity =Str (severity.get (SEVERITY, SEVERITY)). Lower ()#the key responsible for storing the latest log. Destination ='common:%s:%s'%(name, severity)#because the program needs to rotate the log every hour, it uses a key to record the number of hours currently in place. Start_key = destination +': Start'Pipe=conn.pipeline () End= Time.time () +Timeout whileTime.time () <End:Try: #monitor the keys that record the current hours to ensure that the rotation is performed correctly. Pipe.watch (Start_key)#gets the current time. now =Datetime.utcnow (). Timetuple ()#gets the number of hours that are currently in place. Hour_start = DateTime (*now[:4]). Isoformat () existing=pipe.get (Start_key)#creates a transaction. Pipe.multi ()#if the current list of common logs is one hours ... ifExisting andExisting <Hour_start: #此处在python3里需要改成existing. Decode () < Hour_start #... Then archive the old common log information. Pipe.rename (destination, Destination +': Last') Pipe.rename (start_key, Destination+':p Start') #update the number of hours currently in place. Pipe.set (Start_key, Hour_start)#performs a self-increment operation on the counter that logs the number of occurrences. Pipe.zincrby (destination, message)#The log_recent () function is responsible for logging and invoking the Execute () function. log_recent (pipe, name, message, severity, pipe)return exceptRedis.exceptions.WatchError:#If the program has a monitoring error because another client is performing an archive operation, retry. Continue

<redis in action> 5.1.2 Common logs

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.