PHP log file parse by regex

php log-parser
php apache log-parser
php logging
php apache log viewer
kassner/log-parser
php parse nginx access log
php log reader
php access log

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"
    },
    ...
    ...
]

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);

Parsing a Web Server Log File (PHP Cookbook), Open the file and parse each line with a regular expression that matches the log file format. This regular expression matches the NCSA Combined Log Format:. PHP RegEx PHP Forms PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail PHP Form Complete PHP Advanced PHP Date and Time PHP Include PHP File Handling PHP File Open/Read PHP File Create/Write PHP File Upload PHP Cookies PHP Sessions PHP Filters PHP Filters Advanced PHP Callback Functions PHP JSON PHP Exceptions PHP OOP

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>';

Parsing Apache access log files using PHP, This is a bit dated, but I still come back to it. A small script (using regex) that parses apache log files. The data breakdown required:. For Software and Web Development. Parsing Apache access log files using PHP. This is a bit dated, but I still come back to it. A small script (using regex) that parses apache log files. The data breakdown required: IP ADDRESS - -. Server Date / Time [SPACE] "GET /path/to/page. HTTP/Type Request".

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);

21 Tips for Parsing Log Files, The location and name of the Apache log file will vary by *nix platform. in an input stream using regular expressions and its own programming syntax. sed 37.54.90.68 – – [02/Jul/2015:21:45:33 +0000] “GET /wp-login.php� I will use the regular expression to parse a log text file. This example I am using to have a better understanding of the regular expressions. Moreover I will state the most commonly used patterns. What is a Regular Expression ? Regular expression are used to identify the character patterns.

kassner/log-parser: PHP Web Server Log Parser Library, $parser = new \Kassner\LogParser\LogParser(); $lines = file('/var/log/apache2/ access.log', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); foreach� Parsing delimited log files with regex. Post a different question. 1 response. stephen. Hi I'm using nxlog v2.9.1716. I've created the following input: <Input in

mvar/log-parser: Universal log parser in plain PHP, Contribute to mvar/log-parser development by creating an account on GitHub. to iterate over the file is to initialize SimpleParser with your regular expression� Herefore we use a log file and within this example it will be the logfile ‘cbs.log’. Here is an exemplaric line of it: 2015-12-09 18:53:04, Info CBS Session: 30487210_1944050851 initialized by client WindowsUpdateAgent, external staging directory: (null) As you can see – this log line could be separated manually easily into diferent sections:

Using regular expressions to parse text files, We're going to use some IIS log files as examples, because they're exactly the kind of pure-text file that a regex is designed to deal with. It'd be nice if we could� PHP Regular Expression Functions. PHP has many useful functions to work with regular expressions. Here is a quick cheat sheet of the main PHP regex functions. Remember that all of them are case sensitive. For more information about the native functions for PHP regular expressions, have a look at the manual.

Comments
  • hi and welcome to SO! have you tried any regex? if so, what regex have you tried?
  • 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.
  • this one has space local.INFO: Backup success but this one has no space production.CRITICAL:send failed.
  • 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