主にプログラミングの経験を積むために、自分のサイト用に PHP を使用してカスタム ログ クラスを作成していますが、解決できない奇妙な問題が発生しています。クラスを次のように呼び出します。
$objLogger = new logger(DIRECTORY_LOG, logger::DEBUG);
$objLogger->logDebug("Debug message 1", "Extra Notes Here");
$objLogger->logDebug("Debug message 2", "Extra Notes Here");
ページをロードした後のログファイルには、次のように書かれています(1-2になり、1-2が繰り返されることに注意してください)。
2013-03-07 16:03:41 - DEBUG --> Debug message 1 ('Extra Notes Here')
2013-03-07 16:03:41 - DEBUG --> Debug message 2 ('Extra Notes Here')
2013-03-07 16:03:41 - DEBUG --> Debug message 1 ('Extra Notes Here')
2013-03-07 16:03:41 - DEBUG --> Debug message 2 ('Extra Notes Here')
コードを 1 回だけ呼び出すと、2 行が書き込まれます。以下のクラスのコードを含めています。
// Logging class to record data from the website into the daily log files
class logger {
// Define and set the class constants
const EMAIL = 0; // Errors with sending/dealing with emails
const DATABASE = 1; // Errors with any sort of database work
const SERVER = 2; // 404 type server errors
const INFO = 3; // Basic information messages
const SCRIPT = 4; // Errors encountered by running scripts
const DEBUG = 5; // Debug messages
const ARGUMENTS = 'none';
// Define the static variables that may be used
private static $permissions = 0777; // File writing permissions
private static $timestamp = 'Y-m-d G:i:s'; // Format for the log timestamp
private static $date = 'Y-m-d'; // Format for the file name
// Define the variables that may be used
private $pathway = null;
private $status = false;
private $file = null;
// Construct the class object when it is called initially
public function __construct($_directory, $_severity = self::SCRIPT) {
// Create the full pathway to the log file to record to
$this->pathway = $_directory . 'log_' . date(self::$date) . '.log';
// Set the threshold for logging (match the severity level to record)
$this->threshold = $_severity;
// Check for and create the directory for the logs if not created already
if (!file_exists($_directory)) {
mkdir($_directory, self::$permissions, true);
}
// Check if the log file exists and can be written to
if (file_exists($this->pathway) && !is_writable($this->pathway)) {
// Set the flag so we don't attempt to write later
$this->status = false;
}
// Open the log file for writting
if (($this->file = fopen($this->pathway, 'a'))) {
// Set the flag so we can write later
$this->status = true;
} else {
// Set the flag so we don't attempt to write later
$this->status = false;
}
}
// Log: Debugging
public function logDebug($_line, $_args = self::ARGUMENTS) {
$this->writeLog($_line, self::DEBUG, $_args);
}
// Write the constructed string into the log file
public function writeLog($_line, $_severity, $_args = self::ARGUMENTS) {
// Check if we can write to the log file first
if (!$this->status) {
// There is some reason we cannot write to the log file
return false;
}
// Check to make sure the severity is not higher then the threshold set earlier
if ($_severity > $this->threshold) {
// A message above the threshold is trying to log so ignore it
return false;
}
// Build the string for the log
$line = $this->buildString($_severity, $_line);
// Check for and add any additional arguments if passed
if ($_args !== self::ARGUMENTS) {
$line .= ' (' . var_export($_args, true) . ')';
}
// Add the proper 'new line' character to the end of the line
$line .= "\r\n";
// Write to the log file
if (fwrite($this->file, $line) === false) {
// Writting to the log failed for some reason
return false;
}
}
// Return the string with in its constructed format
private function buildString($_level, $_line) {
// Build the timestamp
$time = date(self::$timestamp);
// Build the string based on the passed level
switch ($_level) {
case self::EMAIL:
return "$time - EMAIL --> $_line";
case self::DATABASE:
return "$time - DATABASE --> $_line";
case self::SERVER:
return "$time - SERVER --> $_line";
case self::INFO:
return "$time - INFO --> $_line";
case self::SCRIPT:
return "$time - SCRIPT --> $_line";
case self::DEBUG:
return "$time - DEBUG --> $_line";
default:
return "$time - LOG --> $_line";
}
}
}
私は実際にいくつかの助けを借りることができます.2回電話していないので、問題がどこにあるのかわかりません. 私はオンラインで例を見て、例の一部を使用してこれを構築しましたが、正しく実行されているようです。5つのブラウザすべてで試しましたが、同じことをします。