0

我已经找了几天,阅读了多个线程,但我没有找到任何答案。

这是我的问题:我正在重新制作 CodeIgniter 3.XX 上的日志管理。

我在这样的数组中收集每个日志消息的数据:

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

等等等等。从每个日志消息中收集数据并将它们放在一个数组中是没有问题的。

问题是我想对数据进行 json 编码并在另一个控制器中对其进行解码以将它们显示在仪表板上。

所以我用 $log_message = json_encode($log_message, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);

这是结果:

{
    "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"
}{
    "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"
}

但是当我想对我的文件进行 json_decode 时,它​​不被视为 json 数组,所以我在每个消息之间添加了一个“,”来分隔它们,但我需要找到一种方法在开头写一个“[”和一个“]”最后,知道文件由新的日志消息增加。你知道这样做的方法吗?

感谢您的建议,如果我的问题不清楚或可读性不好,请告诉我

4

1 回答 1

0

试试这个,这样你就创建了一个数组数组

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

$log_message[] = $line;

加载完数据后,一个简单的

$messages = json_encode($log_message);

应该足够了,然后当你解码它应该很好地解码为一个对象数组。

更好的示例代码

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

结果:

JSON 字符串 - 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"
    }

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
        )

)
于 2019-10-21T15:11:03.583 回答