2

I have parsed logs from a log file using php and I have pushed these lines in an array.

[2020-01-21 18:01:23] local.INFO: Backup success
[2020-01-21 18:11:03] local.DEBUG: aid=1 bac=2343
[2020-01-21 18:21:29] production.CRITICAL:send failed
[2020-01-21 18:51:01] production.WARNING:limit 7/9 reached

from each line how I can get my expected output more efficiently?

$final = [];
foreach($lines as $line){
    //best way to parse data to $date, $env, $type, $message from $line variable?

    $arr = [
        'date'=> $date,
        'env'=> $env,
        'type'=> $type,
        'message'=> $message
    ];
    array_push($final, $arr);
}

Expectation

[
    {
        "date":"2020-01-21 18:01:23",
        "env":"local",
        "type":"INFO",
        "message":"Backup success"
    },
    {
        "date":"2020-01-21 18:21:29",
        "env":"production",
        "type":"CRITICAL",
        "message":"send failed"
    },
    ...
    ...
]
3
  • hi and welcome to SO! have you tried any regex? if so, what regex have you tried? Commented Jan 22, 2020 at 5:17
  • Some hints to avoid using REGEXP: Explode by spaces. Remove brackets from first element to get date and time, explode second element by dot to get env and type, implode by spaces last elements to get the message. Maybe explode by : to get message first, and then other elements. Commented Jan 22, 2020 at 5:20
  • this one has space local.INFO: Backup success but this one has no space production.CRITICAL:send failed. Commented Jan 22, 2020 at 5:27

3 Answers 3

3

Try this version.

$re = '/^\[(?<date>.*)\]\s(?<env>\w+)\.(?<type>\w+):(?<message>.*)/m';

$str = '[2020-01-21 18:01:23] local.INFO: Backup success
[2020-01-21 18:11:03] local.DEBUG: aid=1 bac=2343
[2020-01-21 18:21:29] production.CRITICAL:send failed
[2020-01-21 18:51:01] production.WARNING:limit 7/9 reached';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
var_dump($matches);

// Print json string
echo json_encode($matches, JSON_PRETTY_PRINT);

Sign up to request clarification or add additional context in comments.

Comments

0

Hope this resolves your answer to the question :)

$lines = '[2020-01-21 18:01:23] local.INFO: Backup success';
$arr = explode('] ', $lines);
$arr1 = explode(': ', $arr[1]);
$arr2 = explode('.', $arr1[0]);
$arr[0] = str_replace("[","",$arr[0]);
echo 'date = ' . $arr[0];
echo '<br>';
echo 'env = ' .$arr2[0];
echo '<br>';
echo 'type = ' .$arr2[1];
echo '<br>';
echo 'message = ' .$arr1[1];
echo '<br>';

1 Comment

Look at line 3,4 in logs. no space between log type and log info. already tried. $info = explode(': ', $line); $temp = explode('] ', $info[0]); $temp2 = explode('.', $temp[1]); please see the logs lines carefully. I am looking for error risk free solution with regex
0

Mix of explodes, substrings, trim and array destructuring:

$final = [];
foreach ($lines as $line) {
    [$envType, $message] = explode(':', substr($line, 22));
    [$env, $type] = explode('.', $envType);

    $final[] = [
        'date'    => substr($line, 1, 19),
        'env'     => $env,
        'type'    => $type,
        'message' => trim($message)
    ];
}

$jsonFormat = json_encode($final, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.