package pkg import ( "regexp" "strings" ) var ( LtmLog = regexp.MustCompile(`([^\s]+[\s]+[\d]+)[\s]((?:[\d]{2}:){2}[\d]{2})[\s]([^\s]+)[\s]([^\s]+)[\s]([^\s]+):[\s](.*)`) ) // LtmLogLine type is a parsed representation of a log line from the ltm logs. // // Example log line: "Feb 2 04:02:03 10000 info audit_forwarder: audit_forwarder started." // Example struct: {Date: "Feb 2:, Time: "04:02:03", MessageCode: "10000", Level: "info", Service: "audit_forwarder:", Message: "audit_forwarder started."} type LtmLogLine struct { Date string `json:"date"` Time string `json:"time"` Level string `json:"level"` Service string `json:"service"` MessageCode string `json:"message_code"` Message string `json:"message"` } // ParseLtmLog method returns a slice of parsed log lines from both the current ltm and zipped ltm logs. // // Should the log message not fall into the expected log pattern the entire log line will appear un-parsed in the message field of the LtmLogLine struct // all other fields will be nil. func ParseLtmLog(log string) (ltmLog []LtmLogLine) { for _, i := range strings.Split(log, "\n") { match := LtmLog.FindStringSubmatch(i) if len(match) == 0 { ltmLog = append(ltmLog, LtmLogLine{Message: i}) } else { ltmLog = append(ltmLog, LtmLogLine{Date: match[1], Time: match[2], MessageCode: match[3], Level: match[4], Service: match[5], Message: match[6]}) } } return }