3
responses

You can get microseconds of an DateTime object:

integer microsecond(datetime datetime)
    Return the microsecond part of the time value.

Since this a fraction of a second want it to log:

 second($EventTime) + "." + microsecond(($EventTime)

However this is wrong, when the microseconds fraction is lower then 100000. I need to add leading zeroes.

How do i do this?

Note: I cannot use sprintf("%06d", val) in perl module, since i am on windows.

AskedJuly 31, 2020 - 6:16pm

Answers (2)

I really think, this is the only way to do this with the limited functions of nxlog:

  $microsecond_str = string(microsecond($EventTime));
  # Add leading zeroes
  if ($microsecond_str =~ /\d{6}/) ;
  else if ($microsecond_str =~ /\d{5}/) $microsecond_str = "0" + $microsecond_str;
  else if ($microsecond_str =~ /\d{4}/) $microsecond_str = "00" + $microsecond_str;
  else if ($microsecond_str =~ /\d{3}/) $microsecond_str = "000" + $microsecond_str;
  else if ($microsecond_str =~ /\d{2}/) $microsecond_str = "0000" + $microsecond_str;
  else if ($microsecond_str =~ /\d{1}/) $microsecond_str = "00000" + $microsecond_str;
  $raw_event = $EventTime + "." + $microsecond_str + ": " + $Message;