Use regex to parse a log file; Append and modify values in a dictionary; Write to a file in CSV
The code generates 2 different reports from this internal ticketing system log file i.e., syslog.log:
- The ranking of errors generated by the system: A list of all the error messages logged and how many times each error was found, sorted by the most common error to the least common error. This report doesn't take into account the users involved.
- The user usage statistics for the service: A list of all users that have used the system, including how many info messages and how many error messages they've generated. This report is sorted by username.
The logs' pattern is as follows: Jan 31 17:51:52 ubuntu.local ticky: INFO Closed ticket [#8604] (mcintosh)
For each log entry, you'll have to first check if it matches the INFO or ERROR message formats. You should use regular expressions for this. When you get a successful match, add one to the corresponding value in the per_user dictionary. If you get an ERROR message, add one to the corresponding entry in the error dictionary by using proper data structure.
After you've processed the log entries from the syslog.log file, you need to sort both the per_user and error dictionary before creating CSV report files.
Keep in mind that:
The error dictionary should be sorted by the number of errors from most common to least common. The user dictionary should be sorted by username. Insert column names as ("Error", "Count") at the zero index position of the sorted error dictionary. And insert column names as ("Username", "INFO", "ERROR") at the zero index position of the sorted per_user dictionary.
After sorting these dictionaries, store them in two different files: error_message.csv and user_statistics.csv.