antoinich
antoinich

Reputation: 21

Log management with multiple array in json_encode

I've looked for several days, read multiple threads and i didnt find any answer.

Here is my problem : i'm remaking the log management on CodeIgniter 3.XX.

I collect the datas for each log message in a array like this :

$log_message['date'] = $date
$log_message['severity'] = $severity
$log_message['message'] = $message

etc etc. There is no problem collecting data from each log message and put them in an array.

The problem is that i want to json encode the datas and decode them in an another controller to display them on a dashboard.

so i use $log_message = json_encode($log_message, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);

Here is the result :

{
    "message": "Severity: Compile Error --> Cannot redeclare Users::update_password_post() /application/controllers/api/Users.php 2055",
    "level": "ERROR",
    "date": "2019-10-21 16:53:24",
    "short_message": "Users.php 2055",
    "severity": "Compile",
    "ip": "",
    "trace": [
        "core/Common.php MY_Exceptions->log_exception() (line:618)"
    ],
    "short_trace": "core/Common.php MY_Exceptions->log_exception() (line:618)",
    "uri": "[POST] /api/users/user"
}


and after several message i got this :

{
    "message": "Severity: Compile Error --> Cannot redeclare Users::update_password_post() /application/controllers/api/Users.php 2055",
    "level": "ERROR",
    "date": "2019-10-21 16:53:24",
    "short_message": "Users.php 2055",
    "severity": "Compile",
    "ip": "",
    "trace": [
        "core/Common.php MY_Exceptions->log_exception() (line:618)"
    ],
    "short_trace": "core/Common.php MY_Exceptions->log_exception() (line:618)",
    "uri": "[POST] /api/users/user"
}{
    "message": "Severity: Compile Error --> Cannot redeclare Users::update_password_post() /application/controllers/api/Users.php 2055",
    "level": "ERROR",
    "date": "2019-10-21 16:53:24",
    "short_message": "Users.php 2055",
    "severity": "Compile",
    "ip": "",
    "trace": [
        "core/Common.php MY_Exceptions->log_exception() (line:618)"
    ],
    "short_trace": "core/Common.php MY_Exceptions->log_exception() (line:618)",
    "uri": "[POST] /api/users/user"
}

But when i want to json_decode my file it is not seen as json array, so i added a "," between each mesage to separate them but i need to find a way to write a "[" at the beginning and a "]" at the end, knowing that the file is incremented by new log messages. Do you know a way to do this?

Thanks for advices, if my probleme is not clear or well readable tell me

Upvotes: 0

Views: 260

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94682

Try this so you create an array of arrays

$line['date'] = $date;
$line['severity'] = $severity;
$line['message'] = $message;

$log_message[] = $line;

When you are finished loading data, a simple

$messages = json_encode($log_message);

should suffice and then when you decode that it should decode nicely as an array of objects.

Better Example Code

$line = [
        "message" => "Severity: Compile Error --> Cannot redeclare Users::update_password_post() /application/controllers/api/Users.php 2055",
        "level" => "ERROR",
        "date" => "2019-10-21 16:53:24",
        "short_message" => "Users.php 2055",
        "severity" => "Compile",
        "ip" => "",
        "trace" => ["core/Common.php MY_Exceptions->log_exception() (line:618)"],
        "short_trace" => "core/Common.php MY_Exceptions->log_exception() (line:618)",
        "uri" => "[POST] /api/users/user"
        ];

$log_message[] = $line;

$line = [
        "message" => "Severity: Compile Error --> Cannot redeclare Users::update_password_post() /application/controllers/api/Users.php 2055",
        "level" => "ERROR",
        "date" => "2019-10-21 16:53:24",
        "short_message" => "Users.php 2055",
        "severity" => "Compile",
        "ip" => "",
        "trace" => ["core/Common.php MY_Exceptions->log_exception() (line:618)"],
        "short_trace" => "core/Common.php MY_Exceptions->log_exception() (line:618)",
        "uri" => "[POST] /api/users/user"
        ];

$log_message[] = $line;

$messages = json_encode($log_message, JSON_PRETTY_PRINT );
print $messages;

$arr = json_decode($messages);
print_r($arr);

RESULTS:

JSON String - PrettyPrint

[
    {
        "message": "Severity: Compile Error --> Cannot redeclare Users::update_password_post() \/application\/controllers\/api\/Users.php 2055",
        "level": "ERROR",
        "date": "2019-10-21 16:53:24",
        "short_message": "Users.php 2055",
        "severity": "Compile",
        "ip": "",
        "trace": [
            "core\/Common.php MY_Exceptions->log_exception() (line:618)"
        ],
        "short_trace": "core\/Common.php MY_Exceptions->log_exception() (line:618)",
        "uri": "[POST] \/api\/users\/user"
    },
    {
        "message": "Severity: Compile Error --> Cannot redeclare Users::update_password_post() \/application\/controllers\/api\/Users.php 2055",
        "level": "ERROR",
        "date": "2019-10-21 16:53:24",
        "short_message": "Users.php 2055",
        "severity": "Compile",
        "ip": "",
        "trace": [
            "core\/Common.php MY_Exceptions->log_exception() (line:618)"
        ],
        "short_trace": "core\/Common.php MY_Exceptions->log_exception() (line:618)",
        "uri": "[POST] \/api\/users\/user"
    }

From Print_r($arr)

Array
(
    [0] => stdClass Object
        (
            [message] => Severity: Compile Error --> Cannot redeclare Users::update_password_post() /application/controllers/api/Users.php 2055
            [level] => ERROR
            [date] => 2019-10-21 16:53:24
            [short_message] => Users.php 2055
            [severity] => Compile
            [ip] => 
            [trace] => Array
                (
                    [0] => core/Common.php MY_Exceptions->log_exception() (line:618)
                )

            [short_trace] => core/Common.php MY_Exceptions->log_exception() (line:618)
            [uri] => [POST] /api/users/user
        )

    [1] => stdClass Object
        (
            [message] => Severity: Compile Error --> Cannot redeclare Users::update_password_post() /application/controllers/api/Users.php 2055
            [level] => ERROR
            [date] => 2019-10-21 16:53:24
            [short_message] => Users.php 2055
            [severity] => Compile
            [ip] => 
            [trace] => Array
                (
                    [0] => core/Common.php MY_Exceptions->log_exception() (line:618)
                )

            [short_trace] => core/Common.php MY_Exceptions->log_exception() (line:618)
            [uri] => [POST] /api/users/user
        )

)

Upvotes: 0

Related Questions