Basic Info:Log is in text lines. I am combining all lines and breaking it up to separate tags/lines. The Log files are copies and not being updated currently for development.

Trying to figure out the best way to get my time from lines in log files. Parsing the line for date time error and error message to output to csv. This process is working on some of my log files but one in particular has 2 different time/date formats. For this reason the code is not picking up the lines not formatted the specific way.

This line is ok format:
2017-02-20T09:30:53.177000 20[] 0000000000000000 Error Description

Second (problem)line
Mon Feb 20 09:31:25 2017 INFO AGENTEXEC Error Description

Since I now have 2 formats in 1 log I now have installed DateTime::Format::Flexible. Hoping this will fix my issue and convert one format to match the other.

#Original Working code prior to 2nd date format #!/usr/bin/perl use IO::File; my $file = 'file.location.log'; open(MYLOG, "<$file") or die "Can't open $file: " . $!; my @mylog = <MYLOG>; close(MYLOG); my $myfixedlog = join("ENDOFLINE", @mylog); # Date 1 ,T 2, Tim +e 3 , 4, 5 , 6 , 7 , + 8 , 9 while ($myfixedlog =~ /([0-9]{4}-[0-9][0-9]-[0-9][0-9])(T)([0-9][0-9]: +[0-9][0-9]:[0-9][0-9])(.\d+\s\d+.\d+.\s)(.{16}\s)(\[\w+\])(\w+\S\s+)( +.+?)(ENDOFLINE)/smg) { $date = $1; $severity = $7; $timestamp = $3; $errormsg = $6; chomp($errormsg); print "New Error Found...\n"; print "0 $0\n"; print "1 $1\n"; print "2 $2\n"; print "3 $3\n"; print "4 $4\n"; print "5 $5\n"; print "6 $6\n"; print "7 $7\n"; print "8 $8\n"; print "9 $9\n"; }

That was to get the positions for the output.
Then to get the desired output using this code.

use 5.18.0; use warnings; use IO::File; use Time::Piece; my $servername = "Server1"; # This is the Location of the Original Log File my $OLF = '\\\server\logs\server.log'; # This is the location of the file parsed my $file = 'server.log'; open(MYLOG, "<$file") or die "Can't open $file: " . $!; my @mylog = <MYLOG>; close(MYLOG); # This is the Location\Name of the file Being Created my $CSVLOG = 'output.csv'; open(OUTLOG, ">>$CSVLOG") ; my @outlog = <OUTLOG>; my $runtimestamp = localtime(time); my $myfixedlog = join("ENDOFLINE", @mylog); print OUTLOG "$OLF\n"; print OUTLOG "Server, Date, TimeStamp, Severity, ErrorMsg, \n"; # Date 1 ,T 2, Tim +e 3 , 4, 5 , 6 , 7 , 8 + , 9 while ($myfixedlog =~ /([0-9]{4}-[0-9][0-9]-[0-9][0-9])(T)([0-9][0-9]: +[0-9][0-9]:[0-9][0-9])(.\d+\s\d+.\d+.\s)(.{16}\s)(\[\w+\])(\w+\S)(.+? +)(ENDOFLINE)/smg) { my $date = $1; my $timestamp = $3; my $severity = $7; my $errormsg = join "",$6, $7, $8; chomp($errormsg); if ($errormsg =~ /DfException/ || $severity eq "error +:" || $errormsg =~ /started in/){ foreach ($errormsg =~ /DfException/ || $seve +rity eq "error:" || $errormsg =~ /started in/) {print OUTLOG "$servername, $date, $timestam +p, $severity, $errormsg \n"}}; # print OUTLOG "$servername, $da +te, $timestamp, $severity, $errormsg, \n" } print OUTLOG @outlog; close(OUTLOG); print "CSV - File Created Log File: $CSVLOG";

So now trying to add and use new code but cant figure out how to set the desired format or how to have it collect and identify properly as before from original code. This is the added code I have found so far to the code to find locations for $myfixedlog

DateTime::Format::Flexible; my $dt = DateTime::Format::Flexible->parse_datetime( $date, lang => [' +en'], );

Ok so I guess the questions are:
How to declare $date if its already being used (in code i am not yet familiar with)?
If this code pulls out the date would it have a position or just be placed by $dt per line?
If the date is pulled and formatted correctly would I still break up my line the same way?

Looking at the preview it looks like i might have grabbed code for 2 different log files but the basics are the same.
Any Assistance would be appreciated. And no I did not write the original code but I have modified these listed to work. I am Still new to Perl but it is getting easier the more I do.
Hopefully this makes sense to someone.


In reply to DateTime::Format::Flexible; for Log Parse with multiple formatted lines by TCLion

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.