Hướng dẫn how to create custom log file in php - cách tạo tệp nhật ký tùy chỉnh trong php

107

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi muốn tạo một logfile cho hệ thống của mình để đăng ký/ghi nhật ký mọi hành động họ thực hiện bên trong hệ thống. Nhưng tôi không biết làm thế nào để làm điều đó.

Ví dụ: tôi có mã PHP này thực hiện chức năng đăng nhập.

public function hasAccess($username,$password){
    $form = array();
    $form['username'] = $username;
    $form['password'] = $password;


    $securityDAO = $this->getDAO('SecurityDAO');
    $result = $securityDAO->hasAccess($form);
    //var_dump($form);
    //var_dump($result);
    if($result[0]['success']=='1'){
        $this->Session->add('user_id', $result[0]['id']);
        //$this->Session->add('username', $result[0]['username']);
        //$this->Session->add('roleid', $result[0]['roleid']);
        return $this->status(0,true,'auth.success',$result);
    }else{
        return $this->status(0,false,'auth.failed',$result);
    }
}

Bây giờ tôi muốn tạo một logfile có tên 'Ngày hôm nay', sau đó khi các chức năng đó được sử dụng để đăng nhập, nó sẽ viết rằng người dùng đã đăng nhập, giống nhau với các chức năng khác. Nhưng tôi chỉ muốn một tệp duy nhất cho mỗi ngày.

Bất cứ ai có thể đủ tử tế để hướng dẫn và dạy tôi làm thế nào tôi nên làm mã của mình?

Hướng dẫn how to create custom log file in php - cách tạo tệp nhật ký tùy chỉnh trong php

Hỏi ngày 11 tháng 11 năm 2013 lúc 4:13Nov 11, 2013 at 4:13

0

Để ghi vào tệp nhật ký và tạo một tệp mới mỗi ngày, bạn có thể sử dụng

//Something to write to txt log
$log  = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
        "Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
        "User: ".$username.PHP_EOL.
        "-------------------------".PHP_EOL;
//Save string to log, use FILE_APPEND to append.
file_put_contents('./log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
3 như một phần của tên tệp.

//Something to write to txt log
$log  = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
        "Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
        "User: ".$username.PHP_EOL.
        "-------------------------".PHP_EOL;
//Save string to log, use FILE_APPEND to append.
file_put_contents('./log_'.date("j.n.Y").'.log', $log, FILE_APPEND);

Vì vậy, bạn sẽ đặt điều đó trong phương thức

//Something to write to txt log
$log  = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
        "Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
        "User: ".$username.PHP_EOL.
        "-------------------------".PHP_EOL;
//Save string to log, use FILE_APPEND to append.
file_put_contents('./log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
4 của bạn.

public function hasAccess($username,$password){
    $form = array();
    $form['username'] = $username;
    $form['password'] = $password;

    $securityDAO = $this->getDAO('SecurityDAO');
    $result = $securityDAO->hasAccess($form);

    //Write action to txt log
    $log  = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
            "Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
            "User: ".$username.PHP_EOL.
            "-------------------------".PHP_EOL;
    //-
    file_put_contents('./log_'.date("j.n.Y").'.txt', $log, FILE_APPEND);

    if($result[0]['success']=='1'){
        $this->Session->add('user_id', $result[0]['id']);
        //$this->Session->add('username', $result[0]['username']);
        //$this->Session->add('roleid', $result[0]['roleid']);
        return $this->status(0,true,'auth.success',$result);
    }else{
        return $this->status(0,false,'auth.failed',$result);
    }
}

Đã trả lời ngày 11 tháng 11 năm 2013 lúc 4:45Nov 11, 2013 at 4:45

Hướng dẫn how to create custom log file in php - cách tạo tệp nhật ký tùy chỉnh trong php

Lawrence Cheronelawrence CheroneLawrence Cherone

45,2K7 Huy hiệu vàng58 Huy hiệu bạc102 Huy hiệu đồng7 gold badges58 silver badges102 bronze badges

6

Tạo một logfile trong PHP, để thực hiện nó, bạn cần truyền dữ liệu trên chức năng và nó sẽ tạo tệp nhật ký cho bạn., to do it you need to pass data on function and it will create log file for you.

function wh_log($log_msg)
{
    $log_filename = "log";
    if (!file_exists($log_filename)) 
    {
        // create directory/folder uploads.
        mkdir($log_filename, 0777, true);
    }
    $log_file_data = $log_filename.'/log_' . date('d-M-Y') . '.log';
    // if you don't add `FILE_APPEND`, the file will be erased each time you add a log
    file_put_contents($log_file_data, $log_msg . "\n", FILE_APPEND);
} 
// call to function
wh_log("this is my log message");

Đã trả lời ngày 11 tháng 11 năm 2017 lúc 8:36Nov 11, 2017 at 8:36

Hướng dẫn how to create custom log file in php - cách tạo tệp nhật ký tùy chỉnh trong php

JonjonJON

9092 Huy hiệu vàng10 Huy hiệu bạc28 Huy hiệu đồng2 gold badges10 silver badges28 bronze badges

Đồng ý với câu trả lời @jon. Chỉ cần thêm sửa đổi đường dẫn để tạo thư mục

//Something to write to txt log
$log  = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
        "Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
        "User: ".$username.PHP_EOL.
        "-------------------------".PHP_EOL;
//Save string to log, use FILE_APPEND to append.
file_put_contents('./log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
5 bên trong
//Something to write to txt log
$log  = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
        "Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
        "User: ".$username.PHP_EOL.
        "-------------------------".PHP_EOL;
//Save string to log, use FILE_APPEND to append.
file_put_contents('./log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
6

 function wh_log($log_msg) {
    $log_filename = $_SERVER['DOCUMENT_ROOT']."/log";
    if (!file_exists($log_filename))
    {
        // create directory/folder uploads.
        mkdir($log_filename, 0777, true);
    }
    $log_file_data = $log_filename.'/log_' . date('d-M-Y') . '.log';
    file_put_contents($log_file_data, $log_msg . "\n", FILE_APPEND);
}

wh_log('log to file');

Chỉ cần thêm

//Something to write to txt log
$log  = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
        "Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
        "User: ".$username.PHP_EOL.
        "-------------------------".PHP_EOL;
//Save string to log, use FILE_APPEND to append.
file_put_contents('./log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
7

Hướng dẫn how to create custom log file in php - cách tạo tệp nhật ký tùy chỉnh trong php

Đã trả lời ngày 12 tháng 5 năm 2018 lúc 7:19May 12, 2018 at 7:19

Hướng dẫn how to create custom log file in php - cách tạo tệp nhật ký tùy chỉnh trong php

ManishmanishManish

5395 Huy hiệu bạc17 Huy hiệu đồng5 silver badges17 bronze badges

Vui lòng kiểm tra với tài liệu này.

http://php.net/manual/en/function.error-log.php

Example:

<?php
// Send notification through the server log if we can not
// connect to the database.
if (!Ora_Logon($username, $password)) {
    error_log("Oracle database not available!", 0);
}

// Notify administrator by email if we run out of FOO
if (!($foo = allocate_new_foo())) {
    error_log("Big trouble, we're all out of FOOs!", 1,
               "");
}

// another way to call error_log():
error_log("You messed up!", 3, "/var/tmp/my-errors.log");
?>

Đã trả lời ngày 11 tháng 11 năm 2013 lúc 4:54Nov 11, 2013 at 4:54

Surabhil Sergysurabhil SergySurabhil Sergy

1.8861 Huy hiệu vàng24 Huy hiệu bạc39 Huy hiệu đồng1 gold badge24 silver badges39 bronze badges

Vui lòng kiểm tra mã này, nó hoạt động tốt cho tôi.

$data = array('shopid'=>3,'version'=> 1,'value=>1');  //here $data is dummy varaible

error_log(print_r($data,true), 3, $_SERVER['DOCUMENT_ROOT']."/your-file-name.log");

//In $data we can mention the error messege and create the log

Đã trả lời ngày 4 tháng 1 năm 2017 lúc 6:32Jan 4, 2017 at 6:32

Hướng dẫn how to create custom log file in php - cách tạo tệp nhật ký tùy chỉnh trong php

1

Bạn có thể sử dụng chức năng tích hợp

//Something to write to txt log
$log  = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
        "Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
        "User: ".$username.PHP_EOL.
        "-------------------------".PHP_EOL;
//Save string to log, use FILE_APPEND to append.
file_put_contents('./log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
8 để kích hoạt lỗi/cảnh báo/thông báo của người dùng và
//Something to write to txt log
$log  = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
        "Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
        "User: ".$username.PHP_EOL.
        "-------------------------".PHP_EOL;
//Save string to log, use FILE_APPEND to append.
file_put_contents('./log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
9 để xử lý chúng. Bên trong trình xử lý lỗi của bạn, bạn có thể muốn sử dụng
public function hasAccess($username,$password){
    $form = array();
    $form['username'] = $username;
    $form['password'] = $password;

    $securityDAO = $this->getDAO('SecurityDAO');
    $result = $securityDAO->hasAccess($form);

    //Write action to txt log
    $log  = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
            "Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
            "User: ".$username.PHP_EOL.
            "-------------------------".PHP_EOL;
    //-
    file_put_contents('./log_'.date("j.n.Y").'.txt', $log, FILE_APPEND);

    if($result[0]['success']=='1'){
        $this->Session->add('user_id', $result[0]['id']);
        //$this->Session->add('username', $result[0]['username']);
        //$this->Session->add('roleid', $result[0]['roleid']);
        return $this->status(0,true,'auth.success',$result);
    }else{
        return $this->status(0,false,'auth.failed',$result);
    }
}
0 hoặc
public function hasAccess($username,$password){
    $form = array();
    $form['username'] = $username;
    $form['password'] = $password;

    $securityDAO = $this->getDAO('SecurityDAO');
    $result = $securityDAO->hasAccess($form);

    //Write action to txt log
    $log  = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
            "Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
            "User: ".$username.PHP_EOL.
            "-------------------------".PHP_EOL;
    //-
    file_put_contents('./log_'.date("j.n.Y").'.txt', $log, FILE_APPEND);

    if($result[0]['success']=='1'){
        $this->Session->add('user_id', $result[0]['id']);
        //$this->Session->add('username', $result[0]['username']);
        //$this->Session->add('roleid', $result[0]['roleid']);
        return $this->status(0,true,'auth.success',$result);
    }else{
        return $this->status(0,false,'auth.failed',$result);
    }
}
1 để lưu trữ tất cả các bản ghi trên các tệp. Để có một tệp duy nhất cho mỗi ngày, chỉ cần sử dụng một cái gì đó như
public function hasAccess($username,$password){
    $form = array();
    $form['username'] = $username;
    $form['password'] = $password;

    $securityDAO = $this->getDAO('SecurityDAO');
    $result = $securityDAO->hasAccess($form);

    //Write action to txt log
    $log  = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
            "Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
            "User: ".$username.PHP_EOL.
            "-------------------------".PHP_EOL;
    //-
    file_put_contents('./log_'.date("j.n.Y").'.txt', $log, FILE_APPEND);

    if($result[0]['success']=='1'){
        $this->Session->add('user_id', $result[0]['id']);
        //$this->Session->add('username', $result[0]['username']);
        //$this->Session->add('roleid', $result[0]['roleid']);
        return $this->status(0,true,'auth.success',$result);
    }else{
        return $this->status(0,false,'auth.failed',$result);
    }
}
2 như tên tệp. Và bây giờ bạn nên biết bắt đầu từ đâu ... :)

Đã trả lời ngày 11 tháng 11 năm 2013 lúc 4:32Nov 11, 2013 at 4:32

user222758user222758user222758

12.7K13 Huy hiệu vàng72 Huy hiệu bạc95 Huy hiệu Đồng13 gold badges72 silver badges95 bronze badges

1

Đây là mã làm việc của tôi. Cảm ơn Paulo cho các liên kết. Bạn tạo một trình xử lý lỗi tùy chỉnh và gọi hàm

public function hasAccess($username,$password){
    $form = array();
    $form['username'] = $username;
    $form['password'] = $password;

    $securityDAO = $this->getDAO('SecurityDAO');
    $result = $securityDAO->hasAccess($form);

    //Write action to txt log
    $log  = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
            "Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
            "User: ".$username.PHP_EOL.
            "-------------------------".PHP_EOL;
    //-
    file_put_contents('./log_'.date("j.n.Y").'.txt', $log, FILE_APPEND);

    if($result[0]['success']=='1'){
        $this->Session->add('user_id', $result[0]['id']);
        //$this->Session->add('username', $result[0]['username']);
        //$this->Session->add('roleid', $result[0]['roleid']);
        return $this->status(0,true,'auth.success',$result);
    }else{
        return $this->status(0,false,'auth.failed',$result);
    }
}
3 với ngoại lệ
public function hasAccess($username,$password){
    $form = array();
    $form['username'] = $username;
    $form['password'] = $password;

    $securityDAO = $this->getDAO('SecurityDAO');
    $result = $securityDAO->hasAccess($form);

    //Write action to txt log
    $log  = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
            "Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
            "User: ".$username.PHP_EOL.
            "-------------------------".PHP_EOL;
    //-
    file_put_contents('./log_'.date("j.n.Y").'.txt', $log, FILE_APPEND);

    if($result[0]['success']=='1'){
        $this->Session->add('user_id', $result[0]['id']);
        //$this->Session->add('username', $result[0]['username']);
        //$this->Session->add('roleid', $result[0]['roleid']);
        return $this->status(0,true,'auth.success',$result);
    }else{
        return $this->status(0,false,'auth.failed',$result);
    }
}
4 chính xác, ngay cả khi đó không phải là lỗi. Đảm bảo bạn có thể viết vào thư mục tệp nhật ký mà không cần truy cập quản trị viên.

<?php
    $logfile_dir = "C:\workspace\logs\\";   // or "/var/log/" for Linux
    $logfile = $logfile_dir . "php_" . date("y-m-d") . ".log";
    $logfile_delete_days = 30;

    function error_handler($errno, $errstr, $errfile, $errline)
    {
        global $logfile_dir, $logfile, $logfile_delete_days;

        if (!(error_reporting() & $errno)) {
            // This error code is not included in error_reporting, so let it fall
            // through to the standard PHP error handler
            return false;
        }

        $filename = basename($errfile);

        switch ($errno) {
            case E_USER_ERROR:
                file_put_contents($logfile, date("y-m-d H:i:s.").gettimeofday()["usec"] . " $filename ($errline): " . "ERROR >> message = [$errno] $errstr\n", FILE_APPEND | LOCK_EX);
                exit(1);
                break;

            case E_USER_WARNING:
                file_put_contents($logfile, date("y-m-d H:i:s.").gettimeofday()["usec"] . " $filename ($errline): " . "WARNING >> message = $errstr\n", FILE_APPEND | LOCK_EX);
                break;

            case E_USER_NOTICE:
                file_put_contents($logfile, date("y-m-d H:i:s.").gettimeofday()["usec"] . " $filename ($errline): " . "NOTICE >> message = $errstr\n", FILE_APPEND | LOCK_EX);
                break;

            default:
                file_put_contents($logfile, date("y-m-d H:i:s.").gettimeofday()["usec"] . " $filename ($errline): " . "UNKNOWN >> message = $errstr\n", FILE_APPEND | LOCK_EX);
                break;
        }

        // delete any files older than 30 days
        $files = glob($logfile_dir . "*");
        $now   = time();

        foreach ($files as $file)
            if (is_file($file))
                if ($now - filemtime($file) >= 60 * 60 * 24 * $logfile_delete_days)
                    unlink($file);

        return true;    // Don't execute PHP internal error handler
    }

    set_error_handler("error_handler");

    trigger_error("testing 1,2,3", E_USER_NOTICE);
?>

Đã trả lời ngày 15 tháng 2 năm 2017 lúc 22:39Feb 15, 2017 at 22:39

Hướng dẫn how to create custom log file in php - cách tạo tệp nhật ký tùy chỉnh trong php

Xinthosexinthosexinthose

2.7032 huy hiệu vàng38 Huy hiệu bạc57 Huy hiệu đồng2 gold badges38 silver badges57 bronze badges

2

Sử dụng chức năng dưới đây

// Enable error reporting
ini_set('display_errors', 1);
//Report runtime errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
//error_reporting(E_ALL & ~E_NOTICE);
// Tell php where your custom php error log is
ini_set('error_log', 'php_error.log');

$dateTime=date("Y-m-d H:i:s");
$ip= $_SERVER['REMOTE_ADDR'];
$errorString="Error occured on time $dateTime by ip $ip";
$php_error_msg.=$errorString;
// Append the error message to the php-error log
//error_log($php_error_msg);
error_log("A custom error has been triggered",1,"email_address","From: email_address");

Chức năng trên sẽ tạo một nhật ký trong tệp php_error với mô tả và email thích hợp sẽ được gửi.

Đã trả lời ngày 16 tháng 4 năm 2014 lúc 13:09Apr 16, 2014 at 13:09

Để in nhật ký, hãy sử dụng chức năng này, điều này sẽ tạo tệp nhật ký trong thư mục

//Something to write to txt log
$log  = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
        "Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
        "User: ".$username.PHP_EOL.
        "-------------------------".PHP_EOL;
//Save string to log, use FILE_APPEND to append.
file_put_contents('./log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
5. Tạo thư mục nhật ký nếu nó không tồn tại.

logger("Your msg in log ", "Filename you want ", "Data to be log string or array or object");


function logger($logMsg="logger", $filename="logger", $logData=""){     
            $log  = date("j.n.Y h:i:s")." || $logMsg : ".print_r($logData,1).PHP_EOL .                  
            "-------------------------".PHP_EOL;
            file_put_contents('./log/'.$filename.date("j.n.Y").'.log', $log, FILE_APPEND);                      
    }

Đã trả lời ngày 22 tháng 10 năm 2019 lúc 9:39Oct 22, 2019 at 9:39

Hướng dẫn how to create custom log file in php - cách tạo tệp nhật ký tùy chỉnh trong php

Ashwani Panwarashwani PanwarAshwani Panwar

2.8633 huy hiệu vàng40 Huy hiệu bạc57 Huy hiệu đồng3 gold badges40 silver badges57 bronze badges

Chức năng nhật ký tùy chỉnh (cho máy chủ PHP):

Features:

Nhật ký sẽ được ẩn (vô hình) ra công khai (mà không cần cài đặt trên máy chủ)

Dễ dàng đọc dữ liệu nhật ký

//Something to write to txt log
$log  = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
        "Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
        "User: ".$username.PHP_EOL.
        "-------------------------".PHP_EOL;
//Save string to log, use FILE_APPEND to append.
file_put_contents('./log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
0

thông báo nếu nó có chỗ để cải thiện

Đã trả lời ngày 30 tháng 11 năm 2021 lúc 7:09Nov 30, 2021 at 7:09

Hướng dẫn how to create custom log file in php - cách tạo tệp nhật ký tùy chỉnh trong php

Proseosocprosocproseosoc

89211 Huy hiệu bạc23 Huy hiệu Đồng11 silver badges23 bronze badges

Đây là chức năng nhật ký PHP của tôi:

Nếu bạn muốn chỉnh sửa các hàng nhật ký

public function hasAccess($username,$password){
    $form = array();
    $form['username'] = $username;
    $form['password'] = $password;

    $securityDAO = $this->getDAO('SecurityDAO');
    $result = $securityDAO->hasAccess($form);

    //Write action to txt log
    $log  = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
            "Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
            "User: ".$username.PHP_EOL.
            "-------------------------".PHP_EOL;
    //-
    file_put_contents('./log_'.date("j.n.Y").'.txt', $log, FILE_APPEND);

    if($result[0]['success']=='1'){
        $this->Session->add('user_id', $result[0]['id']);
        //$this->Session->add('username', $result[0]['username']);
        //$this->Session->add('roleid', $result[0]['roleid']);
        return $this->status(0,true,'auth.success',$result);
    }else{
        return $this->status(0,false,'auth.failed',$result);
    }
}
6 hoặc thứ tự để ghi nhật ký của bạn
public function hasAccess($username,$password){
    $form = array();
    $form['username'] = $username;
    $form['password'] = $password;

    $securityDAO = $this->getDAO('SecurityDAO');
    $result = $securityDAO->hasAccess($form);

    //Write action to txt log
    $log  = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
            "Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
            "User: ".$username.PHP_EOL.
            "-------------------------".PHP_EOL;
    //-
    file_put_contents('./log_'.date("j.n.Y").'.txt', $log, FILE_APPEND);

    if($result[0]['success']=='1'){
        $this->Session->add('user_id', $result[0]['id']);
        //$this->Session->add('username', $result[0]['username']);
        //$this->Session->add('roleid', $result[0]['roleid']);
        return $this->status(0,true,'auth.success',$result);
    }else{
        return $this->status(0,false,'auth.failed',$result);
    }
}
7

//Something to write to txt log
$log  = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
        "Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
        "User: ".$username.PHP_EOL.
        "-------------------------".PHP_EOL;
//Save string to log, use FILE_APPEND to append.
file_put_contents('./log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
1

Đầu ra trông giống như:

//Something to write to txt log
$log  = "User: ".$_SERVER['REMOTE_ADDR'].' - '.date("F j, Y, g:i a").PHP_EOL.
        "Attempt: ".($result[0]['success']=='1'?'Success':'Failed').PHP_EOL.
        "User: ".$username.PHP_EOL.
        "-------------------------".PHP_EOL;
//Save string to log, use FILE_APPEND to append.
file_put_contents('./log_'.date("j.n.Y").'.log', $log, FILE_APPEND);
2

Đã trả lời ngày 4 tháng 8 lúc 14:03Aug 4 at 14:03

Hướng dẫn how to create custom log file in php - cách tạo tệp nhật ký tùy chỉnh trong php

DazzafactDazzafactdazzafact

2.0843 huy hiệu vàng27 Huy hiệu bạc44 Huy hiệu đồng3 gold badges27 silver badges44 bronze badges

2

Làm cách nào để tạo nhật ký lỗi PHP?

Có thể thêm lệnh ini_set (log log_errors, đúng) vào tập lệnh PHP để cho phép đăng nhập lỗi trong PHP.Có thể thêm lệnh ini_set ('error_log', $ log_file) vào tập lệnh PHP để đặt tệp ghi nhật ký lỗi.Có thể sử dụng chức năng ERROR_LOG ($ ERROR_MESSAGE) để đăng nhập thông báo lỗi vào tệp đã cho.. The ini_set('error_log', $log_file) command can be added to the php script to set the error logging file. Further error_log($error_message) function call can be used to log error message to the given file.

Làm cách nào để tạo một tệp nhật ký cho trang web của tôi?

Trong khung ghi nhật ký, chọn định dạng tệp nhật ký trong hộp định dạng, sau đó nhập đường dẫn đến thư mục nơi bạn lưu trữ tệp nhật ký trong hộp thư mục hoặc nhấp vào Duyệt ... để chọn thư mục để lưu trữ tệp nhật ký.

Error_log viết thư cho ở đâu?

Vị trí của tệp nhật ký lỗi có thể được đặt thủ công trong PHP.Tệp INI.Trên máy chủ Windows, trong IIS, nó có thể là một cái gì đó giống như "'error_log = c: \ log_files \ php_errors.log'" Trong Linux, nó có thể là giá trị của "'/var/log/php_errors.C:\log_files\php_errors. log'" in Linux it may be a value of "'/var/log/php_errors.