PHP日志文件不写JSON

发布于 2025-02-09 11:21:53 字数 926 浏览 4 评论 0原文

我有问题。

我的class.log.php

<?php
 
date_default_timezone_set('Europe/Istanbul');
 
class Log
{
    public function add($uID, $pID, $text)
    {
        $date = date("d.m.Y") . " " . date("H:i:s");
        $data = array("Date" => $date, "UserID" => $uID, "ProductID" => $pID, "Text" => $text);
        
        $folder = "../log/";
        $logFileName = $folder . date("d-m-Y") . ".log";
 
        if (!file_exists($folder)) {
            mkdir($folder);
        }
 
        if (!file_exists($logFileName)) {
            file_put_contents($logFileName, "\xEF\xBB\xBF");
        }
 
        $ofile = fopen($logFileName, "a");
 
        fwrite($ofile, $data);
        fclose($ofile);
    }
}
?>

我在页面中使用它的功能。

require_once "class.log.php";
$log = new Log();
$log->add('41','16','Product Added');

为什么我不能在.log文件中以JSON格式打印输出。 我找不到代码中的错误在哪里

I have a problem.

my class.log.php

<?php
 
date_default_timezone_set('Europe/Istanbul');
 
class Log
{
    public function add($uID, $pID, $text)
    {
        $date = date("d.m.Y") . " " . date("H:i:s");
        $data = array("Date" => $date, "UserID" => $uID, "ProductID" => $pID, "Text" => $text);
        
        $folder = "../log/";
        $logFileName = $folder . date("d-m-Y") . ".log";
 
        if (!file_exists($folder)) {
            mkdir($folder);
        }
 
        if (!file_exists($logFileName)) {
            file_put_contents($logFileName, "\xEF\xBB\xBF");
        }
 
        $ofile = fopen($logFileName, "a");
 
        fwrite($ofile, $data);
        fclose($ofile);
    }
}
?>

I use it function like here in pages.

require_once "class.log.php";
$log = new Log();
$log->add('41','16','Product Added');

why can't I print output in json format to my .log file.
I can't find where is the error in the codes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如歌彻婉言 2025-02-16 11:21:53

写入文件时,您应该将数组转换为字符串

使用此代码

    fwrite($ofile, json_encode($data,JSON_UNESCAPED_UNICODE));

when write to file you should convert array to string

use this code

    fwrite($ofile, json_encode($data,JSON_UNESCAPED_UNICODE));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文