98

システムがシステム内で行うすべてのアクションを登録/ログに記録するために、システムのログファイルを作成したいと考えています。しかし、私はそれを行う方法がわかりません。

たとえば、ログイン機能を実行するこのphpコードがあります。

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

「今日の日付」というタイトルのログファイルを作成したいのですが、その関数がログインに使用されると、他の関数と同じように、ユーザーがログインしたことが書き込まれます。しかし、私は毎日 1 つのファイルだけが必要です。

コードの実行方法を教えてくれる親切な人はいますか?

4

10 に答える 10

10

こちらの資料でご確認ください。

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

例:

<?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,
               "operator@example.com");
}

// another way to call error_log():
error_log("You messed up!", 3, "/var/tmp/my-errors.log");
?>
于 2013-11-11T04:54:27.410 に答える
8

@jonの回答に同意します。パスを変更して、logディレクトリ内にディレクトリを作成するように追加しましたroot

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

追加したばかり$_SERVER['DOCUMENT_ROOT']

于 2018-05-12T07:19:05.380 に答える
6

このコードを確認してください。私にとっては問題なく動作します。

$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
于 2017-01-04T06:32:11.907 に答える
3

組み込み関数trigger_error()を使用して、ユーザー エラー/警告/通知をトリガーし、set_error_handler()それらを処理できます。エラー ハンドラー内で、すべてのレコードをファイルに使用error_log()またはfile_put_contents()保存することができます。毎日単一のファイルを作成するには、sprintf('%s.log', date('Y-m-d'))ファイル名のようなものを使用します。そして今、あなたはどこから始めるべきかを知っているはずです... :)

于 2013-11-11T04:32:05.323 に答える
0

以下の関数を使用

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

上記の関数は、適切な説明を含むログファイル php_error を作成し、電子メールが送信されます。

于 2014-04-16T13:09:35.060 に答える