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