I have the following working function:
fn parse_loglines( logs_data: String ) -> Vec<LogLine> {
let mut loglines: Vec<LogLine> = Vec::new();
// This regex does not capture multi-line errors, such as:
// long_operation_tracker:
//W0214 11:22:36.980569 7803 long_operation_tracker.cc:114] UpdateReplica running for 1.000s in thread 7814:
//@ 0x7fa344eb611f (unknown)
//@ 0x7fa345833b39 __lll_lock_wait
//@ 0x7fa34582e6e2 __GI___pthread_mutex_lock
//@ 0x7fa34e609858 rocksdb::port::Mutex::Lock()
//@ 0x7fa34e6897fb rocksdb::InstrumentedMutex::Lock()
//@ 0x7fa34e564a4f rocksdb::DBImpl::WriteImpl()
//@ 0x7fa34e5666a3 rocksdb::DBImpl::Write()
//@ 0x7fa353ff6e40 yb::tablet::Tablet::WriteToRocksDB()
//@ 0x7fa354007114 yb::tablet::Tablet::ApplyKeyValueRowOperations()
//@ 0x7fa354007660 yb::tablet::Tablet::ApplyOperation()
//@ 0x7fa35400791a yb::tablet::Tablet::ApplyRowOperations()
//@ 0x7fa3540929d4 yb::tablet::WriteOperation::DoReplicated()
//@ 0x7fa354083efb yb::tablet::Operation::Replicated()
//@ 0x7fa354089720 yb::tablet::OperationDriver::ApplyTask()
//@ 0x7fa354089e8e yb::tablet::OperationDriver::ReplicationFinished()
//@ 0x7fa353cb3ae7 yb::consensus::ReplicaState::NotifyReplicationFinishedUnlocked()
// fs_manager:
//I0217 10:12:35.491056 26960 fs_manager.cc:278] Opened local filesystem: /mnt/d0
//uuid: "05b8d17620eb4cd79eddaddb2fbcbb42"
//format_stamp: "Formatted at 2022-02-13 16:26:17 on yb-1.local"
// tablet_server_main:
//I0217 10:12:35.521059 26960 tablet_server_main.cc:225] ulimit cur(max)...
//ulimit: core file size 0(unlimited) blks
//ulimit: data seg size unlimited(unlimited) kb
//ulimit: open files 1048576(1048576)
//ulimit: file size unlimited(unlimited) blks
//ulimit: pending signals 119934(119934)
//ulimit: file locks unlimited(unlimited)
//ulimit: max locked memory 64(64) kb
//ulimit: max memory size unlimited(unlimited) kb
//ulimit: stack size 8192(unlimited) kb
//ulimit: cpu time unlimited(unlimited) secs
//ulimit: max user processes 12000(12000)
// it does match the most common logline format:
// I0217 10:19:56.834905 31987 docdb_rocksdb_util.cc:416] FLAGS_rocksdb_base_background_compactions was not set, automatically configuring 1 base background compactions.
let regular_log_line = Regex::new( r"([IWFE])(\d{2})(\d{2}) (\d{2}):(\d{2}):(\d{2})\.(\d{6}) (\d{1,6}) ([a-z_A-Z.:0-9]*)] (.*)\n" ).unwrap();
// Just take the year, it's not in the loglines, however, when the year switches this will lead to error results
let year= Local::now().format("%Y").to_string();
let timezone = Local::now().format("%z").to_string();
for captures in regular_log_line.captures_iter(&logs_data) {
let mut timestamp_string: String = year.to_owned();
timestamp_string.push_str(":");
timestamp_string.push_str(&captures.get(2).unwrap().as_str()); // month
timestamp_string.push_str(":");
timestamp_string.push_str(&captures.get(3).unwrap().as_str()); // day
timestamp_string.push_str(":");
timestamp_string.push_str(&captures.get(4).unwrap().as_str()); // hour
timestamp_string.push_str(":");
timestamp_string.push_str(&captures.get(5).unwrap().as_str()); // minute
timestamp_string.push_str(":");
timestamp_string.push_str(&captures.get(6).unwrap().as_str()); // seconds
timestamp_string.push_str(":");
timestamp_string.push_str(&captures.get(7).unwrap().as_str()); // seconds
timestamp_string.push_str(":");
timestamp_string.push_str(&timezone.to_owned());
loglines.push(LogLine {
severity: captures.get(1).unwrap().as_str().to_string(),
timestamp: DateTime::parse_from_str(×tamp_string, "%Y:%m:%d:%H:%M:%S:%6f:%z").unwrap().with_timezone(&Local),
tid: captures.get(8).unwrap().as_str().to_string(),
sourcefile_nr: captures.get(9).unwrap().as_str().to_string(),
message: captures.get(10).unwrap().as_str().to_string()
});
}
loglines
}
- Can the building of the timestamp be done in a more elegant way?
- I essentially want to build a row, and it there are trailing rows add them to the message field of the LogLines struct. The common documentation shows multi match regex sets and captures, but not both. Can anyone show me how that's done?
- Is there anything else that I can do in a better way? I am still learning.
Thank you