mirror of
https://github.com/ZeJMaN/LBCAlerte_ynh.git
synced 2025-07-06 11:50:48 +02:00
Initial commit
Functional, without SSO
This commit is contained in:
103
sources/lib/Log4php/appenders/LoggerAppenderConsole.php
Normal file
103
sources/lib/Log4php/appenders/LoggerAppenderConsole.php
Normal file
@ -0,0 +1,103 @@
|
||||
<?php
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* LoggerAppenderConsole appends log events either to the standard output
|
||||
* stream (php://stdout) or the standard error stream (php://stderr).
|
||||
*
|
||||
* **Note**: Use this Appender with command-line php scripts. On web scripts
|
||||
* this appender has no effects.
|
||||
*
|
||||
* This appender uses a layout.
|
||||
*
|
||||
* ## Configurable parameters: ##
|
||||
*
|
||||
* - **target** - the target stream: "stdout" or "stderr"
|
||||
*
|
||||
* @version $Revision: 1343601 $
|
||||
* @package log4php
|
||||
* @subpackage appenders
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
|
||||
* @link http://logging.apache.org/log4php/docs/appenders/console.html Appender documentation
|
||||
*/
|
||||
class LoggerAppenderConsole extends LoggerAppender {
|
||||
|
||||
/** The standard otuput stream. */
|
||||
const STDOUT = 'php://stdout';
|
||||
|
||||
/** The standard error stream.*/
|
||||
const STDERR = 'php://stderr';
|
||||
|
||||
/** The 'target' parameter. */
|
||||
protected $target = self::STDOUT;
|
||||
|
||||
/**
|
||||
* Stream resource for the target stream.
|
||||
* @var resource
|
||||
*/
|
||||
protected $fp = null;
|
||||
|
||||
public function activateOptions() {
|
||||
$this->fp = fopen($this->target, 'w');
|
||||
if(is_resource($this->fp) && $this->layout !== null) {
|
||||
fwrite($this->fp, $this->layout->getHeader());
|
||||
}
|
||||
$this->closed = (bool)is_resource($this->fp) === false;
|
||||
}
|
||||
|
||||
|
||||
public function close() {
|
||||
if($this->closed != true) {
|
||||
if (is_resource($this->fp) && $this->layout !== null) {
|
||||
fwrite($this->fp, $this->layout->getFooter());
|
||||
fclose($this->fp);
|
||||
}
|
||||
$this->closed = true;
|
||||
}
|
||||
}
|
||||
|
||||
public function append(LoggerLoggingEvent $event) {
|
||||
if (is_resource($this->fp) && $this->layout !== null) {
|
||||
fwrite($this->fp, $this->layout->format($event));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the 'target' parameter.
|
||||
* @param string $target
|
||||
*/
|
||||
public function setTarget($target) {
|
||||
$value = trim($target);
|
||||
if ($value == self::STDOUT || strtoupper($value) == 'STDOUT') {
|
||||
$this->target = self::STDOUT;
|
||||
} elseif ($value == self::STDERR || strtoupper($value) == 'STDERR') {
|
||||
$this->target = self::STDERR;
|
||||
} else {
|
||||
$target = var_export($target);
|
||||
$this->warn("Invalid value given for 'target' property: [$target]. Property not set.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of the 'target' parameter.
|
||||
* @return string
|
||||
*/
|
||||
public function getTarget() {
|
||||
return $this->target;
|
||||
}
|
||||
}
|
130
sources/lib/Log4php/appenders/LoggerAppenderDailyFile.php
Normal file
130
sources/lib/Log4php/appenders/LoggerAppenderDailyFile.php
Normal file
@ -0,0 +1,130 @@
|
||||
<?php
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* An Appender that automatically creates a new logfile each day.
|
||||
*
|
||||
* The file is rolled over once a day. That means, for each day a new file
|
||||
* is created. A formatted version of the date pattern is used as to create
|
||||
* the file name using the {@link PHP_MANUAL#sprintf} function.
|
||||
*
|
||||
* This appender uses a layout.
|
||||
*
|
||||
* ##Configurable parameters:##
|
||||
*
|
||||
* - **datePattern** - Format for the date in the file path, follows formatting
|
||||
* rules used by the PHP date() function. Default value: "Ymd".
|
||||
* - **file** - Path to the target file. Should contain a %s which gets
|
||||
* substituted by the date.
|
||||
* - **append** - If set to true, the appender will append to the file,
|
||||
* otherwise the file contents will be overwritten. Defaults to true.
|
||||
*
|
||||
* @version $Revision: 1382274 $
|
||||
* @package log4php
|
||||
* @subpackage appenders
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
|
||||
* @link http://logging.apache.org/log4php/docs/appenders/daily-file.html Appender documentation
|
||||
*/
|
||||
class LoggerAppenderDailyFile extends LoggerAppenderFile {
|
||||
|
||||
/**
|
||||
* The 'datePattern' parameter.
|
||||
* Determines how date will be formatted in file name.
|
||||
* @var string
|
||||
*/
|
||||
protected $datePattern = "Ymd";
|
||||
|
||||
/**
|
||||
* Current date which was used when opening a file.
|
||||
* Used to determine if a rollover is needed when the date changes.
|
||||
* @var string
|
||||
*/
|
||||
protected $currentDate;
|
||||
|
||||
/** Additional validation for the date pattern. */
|
||||
public function activateOptions() {
|
||||
parent::activateOptions();
|
||||
|
||||
if (empty($this->datePattern)) {
|
||||
$this->warn("Required parameter 'datePattern' not set. Closing appender.");
|
||||
$this->closed = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a logging event.
|
||||
*
|
||||
* If the target file changes because of passage of time (e.g. at midnight)
|
||||
* the current file is closed. A new file, with the new date, will be
|
||||
* opened by the write() method.
|
||||
*/
|
||||
public function append(LoggerLoggingEvent $event) {
|
||||
$eventDate = $this->getDate($event->getTimestamp());
|
||||
|
||||
// Initial setting of current date
|
||||
if (!isset($this->currentDate)) {
|
||||
$this->currentDate = $eventDate;
|
||||
}
|
||||
|
||||
// Check if rollover is needed
|
||||
else if ($this->currentDate !== $eventDate) {
|
||||
$this->currentDate = $eventDate;
|
||||
|
||||
// Close the file if it's open.
|
||||
// Note: $this->close() is not called here because it would set
|
||||
// $this->closed to true and the appender would not recieve
|
||||
// any more logging requests
|
||||
if (is_resource($this->fp)) {
|
||||
$this->write($this->layout->getFooter());
|
||||
fclose($this->fp);
|
||||
}
|
||||
$this->fp = null;
|
||||
}
|
||||
|
||||
parent::append($event);
|
||||
}
|
||||
|
||||
/** Renders the date using the configured <var>datePattern<var>. */
|
||||
protected function getDate($timestamp = null) {
|
||||
return date($this->datePattern, $timestamp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines target file. Replaces %s in file path with a date.
|
||||
*/
|
||||
protected function getTargetFile() {
|
||||
return str_replace('%s', $this->currentDate, $this->file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the 'datePattern' parameter.
|
||||
* @param string $datePattern
|
||||
*/
|
||||
public function setDatePattern($datePattern) {
|
||||
$this->setString('datePattern', $datePattern);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the 'datePattern' parameter.
|
||||
* @return string
|
||||
*/
|
||||
public function getDatePattern() {
|
||||
return $this->datePattern;
|
||||
}
|
||||
}
|
88
sources/lib/Log4php/appenders/LoggerAppenderEcho.php
Normal file
88
sources/lib/Log4php/appenders/LoggerAppenderEcho.php
Normal file
@ -0,0 +1,88 @@
|
||||
<?php
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* LoggerAppenderEcho uses the PHP echo() function to output events.
|
||||
*
|
||||
* This appender uses a layout.
|
||||
*
|
||||
* ## Configurable parameters: ##
|
||||
*
|
||||
* - **htmlLineBreaks** - If set to true, a <br /> element will be inserted
|
||||
* before each line break in the logged message. Default is false.
|
||||
*
|
||||
* @version $Revision: 1337820 $
|
||||
* @package log4php
|
||||
* @subpackage appenders
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
|
||||
* @link http://logging.apache.org/log4php/docs/appenders/echo.html Appender documentation
|
||||
*/
|
||||
class LoggerAppenderEcho extends LoggerAppender {
|
||||
/**
|
||||
* Used to mark first append. Set to false after first append.
|
||||
* @var boolean
|
||||
*/
|
||||
protected $firstAppend = true;
|
||||
|
||||
/**
|
||||
* If set to true, a <br /> element will be inserted before each line
|
||||
* break in the logged message. Default value is false. @var boolean
|
||||
*/
|
||||
protected $htmlLineBreaks = false;
|
||||
|
||||
public function close() {
|
||||
if($this->closed != true) {
|
||||
if(!$this->firstAppend) {
|
||||
echo $this->layout->getFooter();
|
||||
}
|
||||
}
|
||||
$this->closed = true;
|
||||
}
|
||||
|
||||
public function append(LoggerLoggingEvent $event) {
|
||||
if($this->layout !== null) {
|
||||
if($this->firstAppend) {
|
||||
echo $this->layout->getHeader();
|
||||
$this->firstAppend = false;
|
||||
}
|
||||
$text = $this->layout->format($event);
|
||||
|
||||
if ($this->htmlLineBreaks) {
|
||||
$text = nl2br($text);
|
||||
}
|
||||
echo $text;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the 'htmlLineBreaks' parameter.
|
||||
* @param boolean $value
|
||||
*/
|
||||
public function setHtmlLineBreaks($value) {
|
||||
$this->setBoolean('htmlLineBreaks', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the 'htmlLineBreaks' parameter.
|
||||
* @returns boolean
|
||||
*/
|
||||
public function getHtmlLineBreaks() {
|
||||
return $this->htmlLineBreaks;
|
||||
}
|
||||
}
|
||||
|
225
sources/lib/Log4php/appenders/LoggerAppenderFile.php
Normal file
225
sources/lib/Log4php/appenders/LoggerAppenderFile.php
Normal file
@ -0,0 +1,225 @@
|
||||
<?php
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* LoggerAppenderFile appends log events to a file.
|
||||
*
|
||||
* This appender uses a layout.
|
||||
*
|
||||
* ## Configurable parameters: ##
|
||||
*
|
||||
* - **file** - Path to the target file. Relative paths are resolved based on
|
||||
* the working directory.
|
||||
* - **append** - If set to true, the appender will append to the file,
|
||||
* otherwise the file contents will be overwritten.
|
||||
*
|
||||
* @version $Revision: 1382274 $
|
||||
* @package log4php
|
||||
* @subpackage appenders
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
|
||||
* @link http://logging.apache.org/log4php/docs/appenders/file.html Appender documentation
|
||||
*/
|
||||
class LoggerAppenderFile extends LoggerAppender {
|
||||
|
||||
/**
|
||||
* If set to true, the file is locked before appending. This allows
|
||||
* concurrent access. However, appending without locking is faster so
|
||||
* it should be used where appropriate.
|
||||
*
|
||||
* TODO: make this a configurable parameter
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $locking = true;
|
||||
|
||||
/**
|
||||
* If set to true, appends to file. Otherwise overwrites it.
|
||||
* @var boolean
|
||||
*/
|
||||
protected $append = true;
|
||||
|
||||
/**
|
||||
* Path to the target file.
|
||||
* @var string
|
||||
*/
|
||||
protected $file;
|
||||
|
||||
/**
|
||||
* The file resource.
|
||||
* @var resource
|
||||
*/
|
||||
protected $fp;
|
||||
|
||||
/**
|
||||
* Helper function which can be easily overriden by daily file appender.
|
||||
*/
|
||||
protected function getTargetFile() {
|
||||
return $this->file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Acquires the target file resource, creates the destination folder if
|
||||
* necessary. Writes layout header to file.
|
||||
*
|
||||
* @return boolean FALSE if opening failed
|
||||
*/
|
||||
protected function openFile() {
|
||||
$file = $this->getTargetFile();
|
||||
|
||||
// Create the target folder if needed
|
||||
if(!is_file($file)) {
|
||||
$dir = dirname($file);
|
||||
|
||||
if(!is_dir($dir)) {
|
||||
$success = mkdir($dir, 0777, true);
|
||||
if ($success === false) {
|
||||
$this->warn("Failed creating target directory [$dir]. Closing appender.");
|
||||
$this->closed = true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$mode = $this->append ? 'a' : 'w';
|
||||
$this->fp = fopen($file, $mode);
|
||||
if ($this->fp === false) {
|
||||
$this->warn("Failed opening target file. Closing appender.");
|
||||
$this->fp = null;
|
||||
$this->closed = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Required when appending with concurrent access
|
||||
if($this->append) {
|
||||
fseek($this->fp, 0, SEEK_END);
|
||||
}
|
||||
|
||||
// Write the header
|
||||
$this->write($this->layout->getHeader());
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a string to the target file. Opens file if not already open.
|
||||
* @param string $string Data to write.
|
||||
*/
|
||||
protected function write($string) {
|
||||
// Lazy file open
|
||||
if(!isset($this->fp)) {
|
||||
if ($this->openFile() === false) {
|
||||
return; // Do not write if file open failed.
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->locking) {
|
||||
$this->writeWithLocking($string);
|
||||
} else {
|
||||
$this->writeWithoutLocking($string);
|
||||
}
|
||||
}
|
||||
|
||||
protected function writeWithLocking($string) {
|
||||
if(flock($this->fp, LOCK_EX)) {
|
||||
if(fwrite($this->fp, $string) === false) {
|
||||
$this->warn("Failed writing to file. Closing appender.");
|
||||
$this->closed = true;
|
||||
}
|
||||
flock($this->fp, LOCK_UN);
|
||||
} else {
|
||||
$this->warn("Failed locking file for writing. Closing appender.");
|
||||
$this->closed = true;
|
||||
}
|
||||
}
|
||||
|
||||
protected function writeWithoutLocking($string) {
|
||||
if(fwrite($this->fp, $string) === false) {
|
||||
$this->warn("Failed writing to file. Closing appender.");
|
||||
$this->closed = true;
|
||||
}
|
||||
}
|
||||
|
||||
public function activateOptions() {
|
||||
if (empty($this->file)) {
|
||||
$this->warn("Required parameter 'file' not set. Closing appender.");
|
||||
$this->closed = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public function close() {
|
||||
if (is_resource($this->fp)) {
|
||||
$this->write($this->layout->getFooter());
|
||||
fclose($this->fp);
|
||||
}
|
||||
$this->fp = null;
|
||||
$this->closed = true;
|
||||
}
|
||||
|
||||
public function append(LoggerLoggingEvent $event) {
|
||||
$this->write($this->layout->format($event));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the 'file' parameter.
|
||||
* @param string $file
|
||||
*/
|
||||
public function setFile($file) {
|
||||
$this->setString('file', $file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the 'file' parameter.
|
||||
* @return string
|
||||
*/
|
||||
public function getFile() {
|
||||
return $this->file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the 'append' parameter.
|
||||
* @return boolean
|
||||
*/
|
||||
public function getAppend() {
|
||||
return $this->append;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the 'append' parameter.
|
||||
* @param boolean $append
|
||||
*/
|
||||
public function setAppend($append) {
|
||||
$this->setBoolean('append', $append);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the 'file' parmeter. Left for legacy reasons.
|
||||
* @param string $fileName
|
||||
* @deprecated Use setFile() instead.
|
||||
*/
|
||||
public function setFileName($fileName) {
|
||||
$this->setFile($fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the 'file' parmeter. Left for legacy reasons.
|
||||
* @return string
|
||||
* @deprecated Use getFile() instead.
|
||||
*/
|
||||
public function getFileName() {
|
||||
return $this->getFile();
|
||||
}
|
||||
}
|
100
sources/lib/Log4php/appenders/LoggerAppenderFirePHP.php
Normal file
100
sources/lib/Log4php/appenders/LoggerAppenderFirePHP.php
Normal file
@ -0,0 +1,100 @@
|
||||
<?php
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Logs messages as HTTP headers using the FirePHP Insight API.
|
||||
*
|
||||
* This appender requires the FirePHP server library version 1.0 or later.
|
||||
*
|
||||
* ## Configurable parameters: ##
|
||||
*
|
||||
* - **target** - (string) The target to which messages will be sent. Possible options are
|
||||
* 'page' (default), 'request', 'package' and 'controller'. For more details,
|
||||
* see FirePHP documentation.
|
||||
*
|
||||
* This class was originally contributed by Bruce Ingalls (Bruce.Ingalls-at-gmail-dot-com).
|
||||
*
|
||||
* @link https://github.com/firephp/firephp FirePHP homepage.
|
||||
* @link http://sourcemint.com/github.com/firephp/firephp/1:1.0.0b1rc6/-docs/Welcome FirePHP documentation.
|
||||
* @link http://sourcemint.com/github.com/firephp/firephp/1:1.0.0b1rc6/-docs/Configuration/Constants FirePHP constants documentation.
|
||||
* @link http://logging.apache.org/log4php/docs/appenders/firephp.html Appender documentation
|
||||
*
|
||||
* @version $Revision: 1343684 $
|
||||
* @package log4php
|
||||
* @subpackage appenders
|
||||
* @since 2.3
|
||||
*/
|
||||
class LoggerAppenderFirePHP extends LoggerAppender {
|
||||
|
||||
/**
|
||||
* Instance of the Insight console class.
|
||||
* @var Insight_Plugin_Console
|
||||
*/
|
||||
protected $console;
|
||||
|
||||
/**
|
||||
* The target for log messages. Possible values are: 'page' (default),
|
||||
* 'request', 'package' and 'contoller'.
|
||||
*/
|
||||
protected $target = 'page';
|
||||
|
||||
public function activateOptions() {
|
||||
if (method_exists('FirePHP', 'to')) {
|
||||
$this->console = FirePHP::to($this->target)->console();
|
||||
$this->closed = false;
|
||||
} else {
|
||||
$this->warn('FirePHP is not installed correctly. Closing appender.');
|
||||
}
|
||||
}
|
||||
|
||||
public function append(LoggerLoggingEvent $event) {
|
||||
$msg = $event->getMessage();
|
||||
|
||||
// Skip formatting for objects and arrays which are handled by FirePHP.
|
||||
if (!is_array($msg) && !is_object($msg)) {
|
||||
$msg = $this->getLayout()->format($event);
|
||||
}
|
||||
|
||||
switch ($event->getLevel()->toInt()) {
|
||||
case LoggerLevel::TRACE:
|
||||
case LoggerLevel::DEBUG:
|
||||
$this->console->log($msg);
|
||||
break;
|
||||
case LoggerLevel::INFO:
|
||||
$this->console->info($msg);
|
||||
break;
|
||||
case LoggerLevel::WARN:
|
||||
$this->console->warn($msg);
|
||||
break;
|
||||
case LoggerLevel::ERROR:
|
||||
case LoggerLevel::FATAL:
|
||||
$this->console->error($msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns the target. */
|
||||
public function getTarget() {
|
||||
return $this->target;
|
||||
}
|
||||
|
||||
/** Sets the target. */
|
||||
public function setTarget($target) {
|
||||
$this->setString('target', $target);
|
||||
}
|
||||
}
|
136
sources/lib/Log4php/appenders/LoggerAppenderMail.php
Normal file
136
sources/lib/Log4php/appenders/LoggerAppenderMail.php
Normal file
@ -0,0 +1,136 @@
|
||||
<?php
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* LoggerAppenderMail appends log events via email.
|
||||
*
|
||||
* This appender does not send individual emails for each logging requests but
|
||||
* will collect them in a buffer and send them all in a single email once the
|
||||
* appender is closed (i.e. when the script exists). Because of this, it may
|
||||
* not appropriate for long running scripts, in which case
|
||||
* LoggerAppenderMailEvent might be a better choice.
|
||||
*
|
||||
* This appender uses a layout.
|
||||
*
|
||||
* ## Configurable parameters: ##
|
||||
*
|
||||
* - **to** - Email address(es) to which the log will be sent. Multiple email
|
||||
* addresses may be specified by separating them with a comma.
|
||||
* - **from** - Email address which will be used in the From field.
|
||||
* - **subject** - Subject of the email message.
|
||||
*
|
||||
* @version $Revision: 1337820 $
|
||||
* @package log4php
|
||||
* @subpackage appenders
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
|
||||
* @link http://logging.apache.org/log4php/docs/appenders/mail.html Appender documentation
|
||||
*/
|
||||
class LoggerAppenderMail extends LoggerAppender {
|
||||
|
||||
/**
|
||||
* Email address to put in From field of the email.
|
||||
* @var string
|
||||
*/
|
||||
protected $from = null;
|
||||
|
||||
/**
|
||||
* The subject of the email.
|
||||
* @var string
|
||||
*/
|
||||
protected $subject = 'Log4php Report';
|
||||
|
||||
/**
|
||||
* One or more comma separated email addresses to which to send the email.
|
||||
* @var string
|
||||
*/
|
||||
protected $to = null;
|
||||
|
||||
/**
|
||||
* Indiciates whether this appender should run in dry mode.
|
||||
* @deprecated
|
||||
* @var boolean
|
||||
*/
|
||||
protected $dry = false;
|
||||
|
||||
/**
|
||||
* Buffer which holds the email contents before it is sent.
|
||||
* @var string
|
||||
*/
|
||||
protected $body = '';
|
||||
|
||||
public function append(LoggerLoggingEvent $event) {
|
||||
if($this->layout !== null) {
|
||||
$this->body .= $this->layout->format($event);
|
||||
}
|
||||
}
|
||||
|
||||
public function close() {
|
||||
if($this->closed != true) {
|
||||
$from = $this->from;
|
||||
$to = $this->to;
|
||||
|
||||
if(!empty($this->body) and $from !== null and $to !== null and $this->layout !== null) {
|
||||
$subject = $this->subject;
|
||||
if(!$this->dry) {
|
||||
mail(
|
||||
$to, $subject,
|
||||
$this->layout->getHeader() . $this->body . $this->layout->getFooter(),
|
||||
"From: {$from}\r\n");
|
||||
} else {
|
||||
echo "DRY MODE OF MAIL APP.: Send mail to: ".$to." with content: ".$this->body;
|
||||
}
|
||||
}
|
||||
$this->closed = true;
|
||||
}
|
||||
}
|
||||
|
||||
/** Sets the 'subject' parameter. */
|
||||
public function setSubject($subject) {
|
||||
$this->setString('subject', $subject);
|
||||
}
|
||||
|
||||
/** Returns the 'subject' parameter. */
|
||||
public function getSubject() {
|
||||
return $this->subject;
|
||||
}
|
||||
|
||||
/** Sets the 'to' parameter. */
|
||||
public function setTo($to) {
|
||||
$this->setString('to', $to);
|
||||
}
|
||||
|
||||
/** Returns the 'to' parameter. */
|
||||
public function getTo() {
|
||||
return $this->to;
|
||||
}
|
||||
|
||||
/** Sets the 'from' parameter. */
|
||||
public function setFrom($from) {
|
||||
$this->setString('from', $from);
|
||||
}
|
||||
|
||||
/** Returns the 'from' parameter. */
|
||||
public function getFrom() {
|
||||
return $this->from;
|
||||
}
|
||||
|
||||
/** Enables or disables dry mode. */
|
||||
public function setDry($dry) {
|
||||
$this->setBoolean('dry', $dry);
|
||||
}
|
||||
}
|
180
sources/lib/Log4php/appenders/LoggerAppenderMailEvent.php
Normal file
180
sources/lib/Log4php/appenders/LoggerAppenderMailEvent.php
Normal file
@ -0,0 +1,180 @@
|
||||
<?php
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* LoggerAppenderMailEvent appends individual log events via email.
|
||||
*
|
||||
* This appender is similar to LoggerAppenderMail, except that it sends each
|
||||
* each log event in an individual email message at the time when it occurs.
|
||||
*
|
||||
* This appender uses a layout.
|
||||
*
|
||||
* ## Configurable parameters: ##
|
||||
*
|
||||
* - **to** - Email address(es) to which the log will be sent. Multiple email
|
||||
* addresses may be specified by separating them with a comma.
|
||||
* - **from** - Email address which will be used in the From field.
|
||||
* - **subject** - Subject of the email message.
|
||||
* - **smtpHost** - Used to override the SMTP server. Only works on Windows.
|
||||
* - **port** - Used to override the default SMTP server port. Only works on
|
||||
* Windows.
|
||||
*
|
||||
* @version $Revision: 1343601 $
|
||||
* @package log4php
|
||||
* @subpackage appenders
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
|
||||
* @link http://logging.apache.org/log4php/docs/appenders/mail-event.html Appender documentation
|
||||
*/
|
||||
class LoggerAppenderMailEvent extends LoggerAppender {
|
||||
|
||||
/**
|
||||
* Email address to put in From field of the email.
|
||||
* @var string
|
||||
*/
|
||||
protected $from;
|
||||
|
||||
/**
|
||||
* Mail server port (widnows only).
|
||||
* @var integer
|
||||
*/
|
||||
protected $port = 25;
|
||||
|
||||
/**
|
||||
* Mail server hostname (windows only).
|
||||
* @var string
|
||||
*/
|
||||
protected $smtpHost;
|
||||
|
||||
/**
|
||||
* The subject of the email.
|
||||
* @var string
|
||||
*/
|
||||
protected $subject = 'Log4php Report';
|
||||
|
||||
/**
|
||||
* One or more comma separated email addresses to which to send the email.
|
||||
* @var string
|
||||
*/
|
||||
protected $to = null;
|
||||
|
||||
/**
|
||||
* Indiciates whether this appender should run in dry mode.
|
||||
* @deprecated
|
||||
* @var boolean
|
||||
*/
|
||||
protected $dry = false;
|
||||
|
||||
public function activateOptions() {
|
||||
if (empty($this->to)) {
|
||||
$this->warn("Required parameter 'to' not set. Closing appender.");
|
||||
$this->close = true;
|
||||
return;
|
||||
}
|
||||
|
||||
$sendmail_from = ini_get('sendmail_from');
|
||||
if (empty($this->from) and empty($sendmail_from)) {
|
||||
$this->warn("Required parameter 'from' not set. Closing appender.");
|
||||
$this->close = true;
|
||||
return;
|
||||
}
|
||||
|
||||
$this->closed = false;
|
||||
}
|
||||
|
||||
public function append(LoggerLoggingEvent $event) {
|
||||
$smtpHost = $this->smtpHost;
|
||||
$prevSmtpHost = ini_get('SMTP');
|
||||
if(!empty($smtpHost)) {
|
||||
ini_set('SMTP', $smtpHost);
|
||||
}
|
||||
|
||||
$smtpPort = $this->port;
|
||||
$prevSmtpPort= ini_get('smtp_port');
|
||||
if($smtpPort > 0 and $smtpPort < 65535) {
|
||||
ini_set('smtp_port', $smtpPort);
|
||||
}
|
||||
|
||||
// On unix only sendmail_path, which is PHP_INI_SYSTEM i.e. not changeable here, is used.
|
||||
|
||||
$addHeader = empty($this->from) ? '' : "From: {$this->from}\r\n";
|
||||
|
||||
if(!$this->dry) {
|
||||
$result = mail($this->to, $this->subject, $this->layout->getHeader() . $this->layout->format($event) . $this->layout->getFooter($event), $addHeader);
|
||||
} else {
|
||||
echo "DRY MODE OF MAIL APP.: Send mail to: ".$this->to." with additional headers '".trim($addHeader)."' and content: ".$this->layout->format($event);
|
||||
}
|
||||
|
||||
ini_set('SMTP', $prevSmtpHost);
|
||||
ini_set('smtp_port', $prevSmtpPort);
|
||||
}
|
||||
|
||||
/** Sets the 'from' parameter. */
|
||||
public function setFrom($from) {
|
||||
$this->setString('from', $from);
|
||||
}
|
||||
|
||||
/** Returns the 'from' parameter. */
|
||||
public function getFrom() {
|
||||
return $this->from;
|
||||
}
|
||||
|
||||
/** Sets the 'port' parameter. */
|
||||
public function setPort($port) {
|
||||
$this->setPositiveInteger('port', $port);
|
||||
}
|
||||
|
||||
/** Returns the 'port' parameter. */
|
||||
public function getPort() {
|
||||
return $this->port;
|
||||
}
|
||||
|
||||
/** Sets the 'smtpHost' parameter. */
|
||||
public function setSmtpHost($smtpHost) {
|
||||
$this->setString('smtpHost', $smtpHost);
|
||||
}
|
||||
|
||||
/** Returns the 'smtpHost' parameter. */
|
||||
public function getSmtpHost() {
|
||||
return $this->smtpHost;
|
||||
}
|
||||
|
||||
/** Sets the 'subject' parameter. */
|
||||
public function setSubject($subject) {
|
||||
$this->setString('subject', $subject);
|
||||
}
|
||||
|
||||
/** Returns the 'subject' parameter. */
|
||||
public function getSubject() {
|
||||
return $this->subject;
|
||||
}
|
||||
|
||||
/** Sets the 'to' parameter. */
|
||||
public function setTo($to) {
|
||||
$this->setString('to', $to);
|
||||
}
|
||||
|
||||
/** Returns the 'to' parameter. */
|
||||
public function getTo() {
|
||||
return $this->to;
|
||||
}
|
||||
|
||||
/** Enables or disables dry mode. */
|
||||
public function setDry($dry) {
|
||||
$this->setBoolean('dry', $dry);
|
||||
}
|
||||
}
|
360
sources/lib/Log4php/appenders/LoggerAppenderMongoDB.php
Normal file
360
sources/lib/Log4php/appenders/LoggerAppenderMongoDB.php
Normal file
@ -0,0 +1,360 @@
|
||||
<?php
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Appender for writing to MongoDB.
|
||||
*
|
||||
* This class was originally contributed by Vladimir Gorej.
|
||||
*
|
||||
* ## Configurable parameters: ##
|
||||
*
|
||||
* - **host** - Server on which mongodb instance is located.
|
||||
* - **port** - Port on which the instance is bound.
|
||||
* - **databaseName** - Name of the database to which to log.
|
||||
* - **collectionName** - Name of the target collection within the given database.
|
||||
* - **username** - Username used to connect to the database.
|
||||
* - **password** - Password used to connect to the database.
|
||||
* - **timeout** - For how long the driver should try to connect to the database (in milliseconds).
|
||||
*
|
||||
* @version $Revision: 1346363 $
|
||||
* @package log4php
|
||||
* @subpackage appenders
|
||||
* @since 2.1
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
|
||||
* @link http://logging.apache.org/log4php/docs/appenders/mongodb.html Appender documentation
|
||||
* @link http://github.com/log4mongo/log4mongo-php Vladimir Gorej's original submission.
|
||||
* @link http://www.mongodb.org/ MongoDB website.
|
||||
*/
|
||||
class LoggerAppenderMongoDB extends LoggerAppender {
|
||||
|
||||
// ******************************************
|
||||
// ** Constants **
|
||||
// ******************************************
|
||||
|
||||
/** Default prefix for the {@link $host}. */
|
||||
const DEFAULT_MONGO_URL_PREFIX = 'mongodb://';
|
||||
|
||||
/** Default value for {@link $host}, without a prefix. */
|
||||
const DEFAULT_MONGO_HOST = 'localhost';
|
||||
|
||||
/** Default value for {@link $port} */
|
||||
const DEFAULT_MONGO_PORT = 27017;
|
||||
|
||||
/** Default value for {@link $databaseName} */
|
||||
const DEFAULT_DB_NAME = 'log4php_mongodb';
|
||||
|
||||
/** Default value for {@link $collectionName} */
|
||||
const DEFAULT_COLLECTION_NAME = 'logs';
|
||||
|
||||
/** Default value for {@link $timeout} */
|
||||
const DEFAULT_TIMEOUT_VALUE = 3000;
|
||||
|
||||
// ******************************************
|
||||
// ** Configurable parameters **
|
||||
// ******************************************
|
||||
|
||||
/** Server on which mongodb instance is located. */
|
||||
protected $host;
|
||||
|
||||
/** Port on which the instance is bound. */
|
||||
protected $port;
|
||||
|
||||
/** Name of the database to which to log. */
|
||||
protected $databaseName;
|
||||
|
||||
/** Name of the collection within the given database. */
|
||||
protected $collectionName;
|
||||
|
||||
/** Username used to connect to the database. */
|
||||
protected $userName;
|
||||
|
||||
/** Password used to connect to the database. */
|
||||
protected $password;
|
||||
|
||||
/** Timeout value used when connecting to the database (in milliseconds). */
|
||||
protected $timeout;
|
||||
|
||||
// ******************************************
|
||||
// ** Member variables **
|
||||
// ******************************************
|
||||
|
||||
/**
|
||||
* Connection to the MongoDB instance.
|
||||
* @var Mongo
|
||||
*/
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* The collection to which log is written.
|
||||
* @var MongoCollection
|
||||
*/
|
||||
protected $collection;
|
||||
|
||||
public function __construct($name = '') {
|
||||
parent::__construct($name);
|
||||
$this->host = self::DEFAULT_MONGO_URL_PREFIX . self::DEFAULT_MONGO_HOST;
|
||||
$this->port = self::DEFAULT_MONGO_PORT;
|
||||
$this->databaseName = self::DEFAULT_DB_NAME;
|
||||
$this->collectionName = self::DEFAULT_COLLECTION_NAME;
|
||||
$this->timeout = self::DEFAULT_TIMEOUT_VALUE;
|
||||
$this->requiresLayout = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup db connection.
|
||||
* Based on defined options, this method connects to the database and
|
||||
* creates a {@link $collection}.
|
||||
*/
|
||||
public function activateOptions() {
|
||||
try {
|
||||
$this->connection = new Mongo(sprintf('%s:%d', $this->host, $this->port), array('timeout' => $this->timeout));
|
||||
$db = $this->connection->selectDB($this->databaseName);
|
||||
if ($this->userName !== null && $this->password !== null) {
|
||||
$authResult = $db->authenticate($this->userName, $this->password);
|
||||
if ($authResult['ok'] == floatval(0)) {
|
||||
throw new Exception($authResult['errmsg'], $authResult['ok']);
|
||||
}
|
||||
}
|
||||
$this->collection = $db->selectCollection($this->collectionName);
|
||||
} catch (MongoConnectionException $ex) {
|
||||
$this->closed = true;
|
||||
$this->warn(sprintf('Failed to connect to mongo deamon: %s', $ex->getMessage()));
|
||||
} catch (InvalidArgumentException $ex) {
|
||||
$this->closed = true;
|
||||
$this->warn(sprintf('Error while selecting mongo database: %s', $ex->getMessage()));
|
||||
} catch (Exception $ex) {
|
||||
$this->closed = true;
|
||||
$this->warn('Invalid credentials for mongo database authentication');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a new event to the mongo database.
|
||||
*
|
||||
* @param LoggerLoggingEvent $event
|
||||
*/
|
||||
public function append(LoggerLoggingEvent $event) {
|
||||
try {
|
||||
if ($this->collection != null) {
|
||||
$this->collection->insert($this->format($event));
|
||||
}
|
||||
} catch (MongoCursorException $ex) {
|
||||
$this->warn(sprintf('Error while writing to mongo collection: %s', $ex->getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the logging event into an array which can be logged to mongodb.
|
||||
*
|
||||
* @param LoggerLoggingEvent $event
|
||||
* @return array The array representation of the logging event.
|
||||
*/
|
||||
protected function format(LoggerLoggingEvent $event) {
|
||||
$timestampSec = (int) $event->getTimestamp();
|
||||
$timestampUsec = (int) (($event->getTimestamp() - $timestampSec) * 1000000);
|
||||
|
||||
$document = array(
|
||||
'timestamp' => new MongoDate($timestampSec, $timestampUsec),
|
||||
'level' => $event->getLevel()->toString(),
|
||||
'thread' => (int) $event->getThreadName(),
|
||||
'message' => $event->getMessage(),
|
||||
'loggerName' => $event->getLoggerName()
|
||||
);
|
||||
|
||||
$locationInfo = $event->getLocationInformation();
|
||||
if ($locationInfo != null) {
|
||||
$document['fileName'] = $locationInfo->getFileName();
|
||||
$document['method'] = $locationInfo->getMethodName();
|
||||
$document['lineNumber'] = ($locationInfo->getLineNumber() == 'NA') ? 'NA' : (int) $locationInfo->getLineNumber();
|
||||
$document['className'] = $locationInfo->getClassName();
|
||||
}
|
||||
|
||||
$throwableInfo = $event->getThrowableInformation();
|
||||
if ($throwableInfo != null) {
|
||||
$document['exception'] = $this->formatThrowable($throwableInfo->getThrowable());
|
||||
}
|
||||
|
||||
return $document;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an Exception into an array which can be logged to mongodb.
|
||||
*
|
||||
* Supports innner exceptions (PHP >= 5.3)
|
||||
*
|
||||
* @param Exception $ex
|
||||
* @return array
|
||||
*/
|
||||
protected function formatThrowable(Exception $ex) {
|
||||
$array = array(
|
||||
'message' => $ex->getMessage(),
|
||||
'code' => $ex->getCode(),
|
||||
'stackTrace' => $ex->getTraceAsString(),
|
||||
);
|
||||
|
||||
if (method_exists($ex, 'getPrevious') && $ex->getPrevious() !== null) {
|
||||
$array['innerException'] = $this->formatThrowable($ex->getPrevious());
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the connection to the logging database
|
||||
*/
|
||||
public function close() {
|
||||
if($this->closed != true) {
|
||||
$this->collection = null;
|
||||
if ($this->connection !== null) {
|
||||
$this->connection->close();
|
||||
$this->connection = null;
|
||||
}
|
||||
$this->closed = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of {@link $host} parameter.
|
||||
* @param string $host
|
||||
*/
|
||||
public function setHost($host) {
|
||||
if (!preg_match('/^mongodb\:\/\//', $host)) {
|
||||
$host = self::DEFAULT_MONGO_URL_PREFIX . $host;
|
||||
}
|
||||
$this->host = $host;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of {@link $host} parameter.
|
||||
* @return string
|
||||
*/
|
||||
public function getHost() {
|
||||
return $this->host;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of {@link $port} parameter.
|
||||
* @param int $port
|
||||
*/
|
||||
public function setPort($port) {
|
||||
$this->setPositiveInteger('port', $port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of {@link $port} parameter.
|
||||
* @return int
|
||||
*/
|
||||
public function getPort() {
|
||||
return $this->port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of {@link $databaseName} parameter.
|
||||
* @param string $databaseName
|
||||
*/
|
||||
public function setDatabaseName($databaseName) {
|
||||
$this->setString('databaseName', $databaseName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of {@link $databaseName} parameter.
|
||||
* @return string
|
||||
*/
|
||||
public function getDatabaseName() {
|
||||
return $this->databaseName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of {@link $collectionName} parameter.
|
||||
* @param string $collectionName
|
||||
*/
|
||||
public function setCollectionName($collectionName) {
|
||||
$this->setString('collectionName', $collectionName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of {@link $collectionName} parameter.
|
||||
* @return string
|
||||
*/
|
||||
public function getCollectionName() {
|
||||
return $this->collectionName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of {@link $userName} parameter.
|
||||
* @param string $userName
|
||||
*/
|
||||
public function setUserName($userName) {
|
||||
$this->setString('userName', $userName, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of {@link $userName} parameter.
|
||||
* @return string
|
||||
*/
|
||||
public function getUserName() {
|
||||
return $this->userName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of {@link $password} parameter.
|
||||
* @param string $password
|
||||
*/
|
||||
public function setPassword($password) {
|
||||
$this->setString('password', $password, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of {@link $password} parameter.
|
||||
* @return string
|
||||
*/
|
||||
public function getPassword() {
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of {@link $timeout} parameter.
|
||||
* @param int $timeout
|
||||
*/
|
||||
public function setTimeout($timeout) {
|
||||
$this->setPositiveInteger('timeout', $timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of {@link $timeout} parameter.
|
||||
* @return int
|
||||
*/
|
||||
public function getTimeout() {
|
||||
return $this->timeout;
|
||||
}
|
||||
/**
|
||||
* Returns the mongodb connection.
|
||||
* @return Mongo
|
||||
*/
|
||||
public function getConnection() {
|
||||
return $this->connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the active mongodb collection.
|
||||
* @return MongoCollection
|
||||
*/
|
||||
public function getCollection() {
|
||||
return $this->collection;
|
||||
}
|
||||
}
|
44
sources/lib/Log4php/appenders/LoggerAppenderNull.php
Normal file
44
sources/lib/Log4php/appenders/LoggerAppenderNull.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* A NullAppender merely exists, it never outputs a message to any device.
|
||||
*
|
||||
* This appender has no configurable parameters.
|
||||
*
|
||||
* @version $Revision: 1343601 $
|
||||
* @package log4php
|
||||
* @subpackage appenders
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
|
||||
* @link http://logging.apache.org/log4php/docs/appenders/null.html Appender documentation
|
||||
*/
|
||||
class LoggerAppenderNull extends LoggerAppender {
|
||||
|
||||
/**
|
||||
* This appender does not require a layout.
|
||||
*/
|
||||
protected $requiresLayout = false;
|
||||
|
||||
/**
|
||||
* Do nothing.
|
||||
*
|
||||
* @param LoggerLoggingEvent $event
|
||||
*/
|
||||
public function append(LoggerLoggingEvent $event) {
|
||||
}
|
||||
}
|
282
sources/lib/Log4php/appenders/LoggerAppenderPDO.php
Normal file
282
sources/lib/Log4php/appenders/LoggerAppenderPDO.php
Normal file
@ -0,0 +1,282 @@
|
||||
<?php
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* LoggerAppenderPDO appender logs to a database using the PHP's PDO extension.
|
||||
*
|
||||
* ## Configurable parameters: ##
|
||||
*
|
||||
* - dsn - The Data Source Name (DSN) used to connect to the database.
|
||||
* - user - Username used to connect to the database.
|
||||
* - password - Password used to connect to the database.
|
||||
* - table - Name of the table to which log entries are be inserted.
|
||||
* - insertSQL - Sets the insert statement for a logging event. Defaults
|
||||
* to the correct one - change only if you are sure what you are doing.
|
||||
* - insertPattern - The conversion pattern to use in conjuction with insert
|
||||
* SQL. Must contain the same number of comma separated
|
||||
* conversion patterns as there are question marks in the
|
||||
* insertSQL.
|
||||
*
|
||||
* @version $Revision: 1374546 $
|
||||
* @package log4php
|
||||
* @subpackage appenders
|
||||
* @since 2.0
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
|
||||
* @link http://logging.apache.org/log4php/docs/appenders/pdo.html Appender documentation
|
||||
*/
|
||||
class LoggerAppenderPDO extends LoggerAppender {
|
||||
|
||||
// ******************************************
|
||||
// *** Configurable parameters ***
|
||||
// ******************************************
|
||||
|
||||
/**
|
||||
* DSN string used to connect to the database.
|
||||
* @see http://www.php.net/manual/en/pdo.construct.php
|
||||
*/
|
||||
protected $dsn;
|
||||
|
||||
/** Database user name. */
|
||||
protected $user;
|
||||
|
||||
/** Database password. */
|
||||
protected $password;
|
||||
|
||||
/**
|
||||
* The insert query.
|
||||
*
|
||||
* The __TABLE__ placeholder will be replaced by the table name from
|
||||
* {@link $table}.
|
||||
*
|
||||
* The questionmarks are part of the prepared statement, and they must
|
||||
* match the number of conversion specifiers in {@link insertPattern}.
|
||||
*/
|
||||
protected $insertSQL = "INSERT INTO __TABLE__ (timestamp, logger, level, message, thread, file, line) VALUES (?, ?, ?, ?, ?, ?, ?)";
|
||||
|
||||
/**
|
||||
* A comma separated list of {@link LoggerPatternLayout} format strings
|
||||
* which replace the "?" in {@link $insertSQL}.
|
||||
*
|
||||
* Must contain the same number of comma separated conversion patterns as
|
||||
* there are question marks in {@link insertSQL}.
|
||||
*
|
||||
* @see LoggerPatternLayout For conversion patterns.
|
||||
*/
|
||||
protected $insertPattern = "%date{Y-m-d H:i:s},%logger,%level,%message,%pid,%file,%line";
|
||||
|
||||
/** Name of the table to which to append log events. */
|
||||
protected $table = 'log4php_log';
|
||||
|
||||
/** The number of recconect attempts to make on failed append. */
|
||||
protected $reconnectAttempts = 3;
|
||||
|
||||
|
||||
// ******************************************
|
||||
// *** Private memebers ***
|
||||
// ******************************************
|
||||
|
||||
/**
|
||||
* The PDO instance.
|
||||
* @var PDO
|
||||
*/
|
||||
protected $db;
|
||||
|
||||
/**
|
||||
* Prepared statement for the insert query.
|
||||
* @var PDOStatement
|
||||
*/
|
||||
protected $preparedInsert;
|
||||
|
||||
/** This appender does not require a layout. */
|
||||
protected $requiresLayout = false;
|
||||
|
||||
|
||||
// ******************************************
|
||||
// *** Appender methods ***
|
||||
// ******************************************
|
||||
|
||||
/**
|
||||
* Acquires a database connection based on parameters.
|
||||
* Parses the insert pattern to create a chain of converters which will be
|
||||
* used in forming query parameters from logging events.
|
||||
*/
|
||||
public function activateOptions() {
|
||||
try {
|
||||
$this->establishConnection();
|
||||
} catch (PDOException $e) {
|
||||
$this->warn("Failed connecting to database. Closing appender. Error: " . $e->getMessage());
|
||||
$this->close();
|
||||
return;
|
||||
}
|
||||
|
||||
// Parse the insert patterns; pattern parts are comma delimited
|
||||
$pieces = explode(',', $this->insertPattern);
|
||||
$converterMap = LoggerLayoutPattern::getDefaultConverterMap();
|
||||
foreach($pieces as $pattern) {
|
||||
$parser = new LoggerPatternParser($pattern, $converterMap);
|
||||
$this->converters[] = $parser->parse();
|
||||
}
|
||||
|
||||
$this->closed = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects to the database, and prepares the insert query.
|
||||
* @throws PDOException If connect or prepare fails.
|
||||
*/
|
||||
protected function establishConnection() {
|
||||
// Acquire database connection
|
||||
$this->db = new PDO($this->dsn, $this->user, $this->password);
|
||||
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
// Prepare the insert statement
|
||||
$insertSQL = str_replace('__TABLE__', $this->table, $this->insertSQL);
|
||||
$this->preparedInsert = $this->db->prepare($insertSQL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a new event to the database.
|
||||
*
|
||||
* If writing to database fails, it will retry by re-establishing the
|
||||
* connection up to $reconnectAttempts times. If writing still fails,
|
||||
* the appender will close.
|
||||
*/
|
||||
public function append(LoggerLoggingEvent $event) {
|
||||
|
||||
for ($attempt = 1; $attempt <= $this->reconnectAttempts + 1; $attempt++) {
|
||||
try {
|
||||
// Attempt to write to database
|
||||
$this->preparedInsert->execute($this->format($event));
|
||||
$this->preparedInsert->closeCursor();
|
||||
break;
|
||||
} catch (PDOException $e) {
|
||||
$this->warn("Failed writing to database: ". $e->getMessage());
|
||||
|
||||
// Close the appender if it's the last attempt
|
||||
if ($attempt > $this->reconnectAttempts) {
|
||||
$this->warn("Failed writing to database after {$this->reconnectAttempts} reconnect attempts. Closing appender.");
|
||||
$this->close();
|
||||
// Otherwise reconnect and try to write again
|
||||
} else {
|
||||
$this->warn("Attempting a reconnect (attempt $attempt of {$this->reconnectAttempts}).");
|
||||
$this->establishConnection();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the logging event to a series of database parameters by using
|
||||
* the converter chain which was set up on activation.
|
||||
*/
|
||||
protected function format(LoggerLoggingEvent $event) {
|
||||
$params = array();
|
||||
foreach($this->converters as $converter) {
|
||||
$buffer = '';
|
||||
while ($converter !== null) {
|
||||
$converter->format($buffer, $event);
|
||||
$converter = $converter->next;
|
||||
}
|
||||
$params[] = $buffer;
|
||||
}
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the connection to the logging database
|
||||
*/
|
||||
public function close() {
|
||||
// Close the connection (if any)
|
||||
$this->db = null;
|
||||
|
||||
// Close the appender
|
||||
$this->closed = true;
|
||||
}
|
||||
|
||||
// ******************************************
|
||||
// *** Accessor methods ***
|
||||
// ******************************************
|
||||
|
||||
/**
|
||||
* Returns the active database handle or null if not established.
|
||||
* @return PDO
|
||||
*/
|
||||
public function getDatabaseHandle() {
|
||||
return $this->db;
|
||||
}
|
||||
|
||||
/** Sets the username. */
|
||||
public function setUser($user) {
|
||||
$this->setString('user', $user);
|
||||
}
|
||||
|
||||
/** Returns the username. */
|
||||
public function getUser($user) {
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
/** Sets the password. */
|
||||
public function setPassword($password) {
|
||||
$this->setString('password', $password);
|
||||
}
|
||||
|
||||
/** Returns the password. */
|
||||
public function getPassword($password) {
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
/** Sets the insert SQL. */
|
||||
public function setInsertSQL($sql) {
|
||||
$this->setString('insertSQL', $sql);
|
||||
}
|
||||
|
||||
/** Returns the insert SQL. */
|
||||
public function getInsertSQL($sql) {
|
||||
return $this->insertSQL;
|
||||
}
|
||||
|
||||
/** Sets the insert pattern. */
|
||||
public function setInsertPattern($pattern) {
|
||||
$this->setString('insertPattern', $pattern);
|
||||
}
|
||||
|
||||
/** Returns the insert pattern. */
|
||||
public function getInsertPattern($pattern) {
|
||||
return $this->insertPattern;
|
||||
}
|
||||
|
||||
/** Sets the table name. */
|
||||
public function setTable($table) {
|
||||
$this->setString('table', $table);
|
||||
}
|
||||
|
||||
/** Returns the table name. */
|
||||
public function getTable($table) {
|
||||
return $this->table;
|
||||
}
|
||||
|
||||
/** Sets the DSN string. */
|
||||
public function setDSN($dsn) {
|
||||
$this->setString('dsn', $dsn);
|
||||
}
|
||||
|
||||
/** Returns the DSN string. */
|
||||
public function getDSN($dsn) {
|
||||
return $this->setString('dsn', $dsn);
|
||||
}
|
||||
}
|
49
sources/lib/Log4php/appenders/LoggerAppenderPhp.php
Normal file
49
sources/lib/Log4php/appenders/LoggerAppenderPhp.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* LoggerAppenderPhp logs events by creating a PHP user-level message using
|
||||
* the PHP's trigger_error()function.
|
||||
*
|
||||
* This appender has no configurable parameters.
|
||||
*
|
||||
* Levels are mapped as follows:
|
||||
*
|
||||
* - <b>level < WARN</b> mapped to E_USER_NOTICE
|
||||
* - <b>WARN <= level < ERROR</b> mapped to E_USER_WARNING
|
||||
* - <b>level >= ERROR</b> mapped to E_USER_ERROR
|
||||
*
|
||||
* @version $Revision: 1337820 $
|
||||
* @package log4php
|
||||
* @subpackage appenders
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
|
||||
* @link http://logging.apache.org/log4php/docs/appenders/php.html Appender documentation
|
||||
*/
|
||||
class LoggerAppenderPhp extends LoggerAppender {
|
||||
|
||||
public function append(LoggerLoggingEvent $event) {
|
||||
$level = $event->getLevel();
|
||||
if($level->isGreaterOrEqual(LoggerLevel::getLevelError())) {
|
||||
trigger_error($this->layout->format($event), E_USER_ERROR);
|
||||
} else if ($level->isGreaterOrEqual(LoggerLevel::getLevelWarn())) {
|
||||
trigger_error($this->layout->format($event), E_USER_WARNING);
|
||||
} else {
|
||||
trigger_error($this->layout->format($event), E_USER_NOTICE);
|
||||
}
|
||||
}
|
||||
}
|
305
sources/lib/Log4php/appenders/LoggerAppenderRollingFile.php
Normal file
305
sources/lib/Log4php/appenders/LoggerAppenderRollingFile.php
Normal file
@ -0,0 +1,305 @@
|
||||
<?php
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* @package log4php
|
||||
*/
|
||||
|
||||
/**
|
||||
* LoggerAppenderRollingFile writes logging events to a specified file. The
|
||||
* file is rolled over after a specified size has been reached.
|
||||
*
|
||||
* This appender uses a layout.
|
||||
*
|
||||
* ## Configurable parameters: ##
|
||||
*
|
||||
* - **file** - Path to the target file.
|
||||
* - **append** - If set to true, the appender will append to the file,
|
||||
* otherwise the file contents will be overwritten.
|
||||
* - **maxBackupIndex** - Maximum number of backup files to keep. Default is 1.
|
||||
* - **maxFileSize** - Maximum allowed file size (in bytes) before rolling
|
||||
* over. Suffixes "KB", "MB" and "GB" are allowed. 10KB = 10240 bytes, etc.
|
||||
* Default is 10M.
|
||||
* - **compress** - If set to true, rolled-over files will be compressed.
|
||||
* Requires the zlib extension.
|
||||
*
|
||||
* @version $Revision: 1394975 $
|
||||
* @package log4php
|
||||
* @subpackage appenders
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
|
||||
* @link http://logging.apache.org/log4php/docs/appenders/rolling-file.html Appender documentation
|
||||
*/
|
||||
class LoggerAppenderRollingFile extends LoggerAppenderFile {
|
||||
|
||||
/** Compressing backup files is done in chunks, this determines how large. */
|
||||
const COMPRESS_CHUNK_SIZE = 102400; // 100KB
|
||||
|
||||
/**
|
||||
* The maximum size (in bytes) that the output file is allowed to reach
|
||||
* before being rolled over to backup files.
|
||||
*
|
||||
* The default maximum file size is 10MB (10485760 bytes). Maximum value
|
||||
* for this option may depend on the file system.
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
protected $maxFileSize = 10485760;
|
||||
|
||||
/**
|
||||
* Set the maximum number of backup files to keep around.
|
||||
*
|
||||
* Determines how many backup files are kept before the oldest is erased.
|
||||
* This option takes a positive integer value. If set to zero, then there
|
||||
* will be no backup files and the log file will be truncated when it
|
||||
* reaches <var>maxFileSize</var>.
|
||||
*
|
||||
* There is one backup file by default.
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
protected $maxBackupIndex = 1;
|
||||
|
||||
/**
|
||||
* The <var>compress</var> parameter determindes the compression with zlib.
|
||||
* If set to true, the rollover files are compressed and saved with the .gz extension.
|
||||
* @var boolean
|
||||
*/
|
||||
protected $compress = false;
|
||||
|
||||
/**
|
||||
* Set to true in the constructor if PHP >= 5.3.0. In that case clearstatcache
|
||||
* supports conditional clearing of statistics.
|
||||
* @var boolean
|
||||
* @see http://php.net/manual/en/function.clearstatcache.php
|
||||
*/
|
||||
private $clearConditional = false;
|
||||
|
||||
/**
|
||||
* Get the maximum size that the output file is allowed to reach
|
||||
* before being rolled over to backup files.
|
||||
* @return integer
|
||||
*/
|
||||
public function getMaximumFileSize() {
|
||||
return $this->maxFileSize;
|
||||
}
|
||||
|
||||
public function __construct($name = '') {
|
||||
parent::__construct($name);
|
||||
if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
|
||||
$this->clearConditional = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements the usual roll over behaviour.
|
||||
*
|
||||
* If MaxBackupIndex is positive, then files File.1, ..., File.MaxBackupIndex -1 are renamed to File.2, ..., File.MaxBackupIndex.
|
||||
* Moreover, File is renamed File.1 and closed. A new File is created to receive further log output.
|
||||
*
|
||||
* If MaxBackupIndex is equal to zero, then the File is truncated with no backup files created.
|
||||
*
|
||||
* Rollover must be called while the file is locked so that it is safe for concurrent access.
|
||||
*
|
||||
* @throws LoggerException If any part of the rollover procedure fails.
|
||||
*/
|
||||
private function rollOver() {
|
||||
// If maxBackups <= 0, then there is no file renaming to be done.
|
||||
if($this->maxBackupIndex > 0) {
|
||||
// Delete the oldest file, to keep Windows happy.
|
||||
$file = $this->file . '.' . $this->maxBackupIndex;
|
||||
|
||||
if (file_exists($file) && !unlink($file)) {
|
||||
throw new LoggerException("Unable to delete oldest backup file from [$file].");
|
||||
}
|
||||
|
||||
// Map {(maxBackupIndex - 1), ..., 2, 1} to {maxBackupIndex, ..., 3, 2}
|
||||
$this->renameArchievedLogs($this->file);
|
||||
|
||||
// Backup the active file
|
||||
$this->moveToBackup($this->file);
|
||||
}
|
||||
|
||||
// Truncate the active file
|
||||
ftruncate($this->fp, 0);
|
||||
rewind($this->fp);
|
||||
}
|
||||
|
||||
private function moveToBackup($source) {
|
||||
if ($this->compress) {
|
||||
$target = $source . '.1.gz';
|
||||
$this->compressFile($source, $target);
|
||||
} else {
|
||||
$target = $source . '.1';
|
||||
copy($source, $target);
|
||||
}
|
||||
}
|
||||
|
||||
private function compressFile($source, $target) {
|
||||
$target = 'compress.zlib://' . $target;
|
||||
|
||||
$fin = fopen($source, 'rb');
|
||||
if ($fin === false) {
|
||||
throw new LoggerException("Unable to open file for reading: [$source].");
|
||||
}
|
||||
|
||||
$fout = fopen($target, 'wb');
|
||||
if ($fout === false) {
|
||||
throw new LoggerException("Unable to open file for writing: [$target].");
|
||||
}
|
||||
|
||||
while (!feof($fin)) {
|
||||
$chunk = fread($fin, self::COMPRESS_CHUNK_SIZE);
|
||||
if (false === fwrite($fout, $chunk)) {
|
||||
throw new LoggerException("Failed writing to compressed file.");
|
||||
}
|
||||
}
|
||||
|
||||
fclose($fin);
|
||||
fclose($fout);
|
||||
}
|
||||
|
||||
private function renameArchievedLogs($fileName) {
|
||||
for($i = $this->maxBackupIndex - 1; $i >= 1; $i--) {
|
||||
|
||||
$source = $fileName . "." . $i;
|
||||
if ($this->compress) {
|
||||
$source .= '.gz';
|
||||
}
|
||||
|
||||
if(file_exists($source)) {
|
||||
$target = $fileName . '.' . ($i + 1);
|
||||
if ($this->compress) {
|
||||
$target .= '.gz';
|
||||
}
|
||||
|
||||
rename($source, $target);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a string to the target file. Opens file if not already open.
|
||||
* @param string $string Data to write.
|
||||
*/
|
||||
protected function write($string) {
|
||||
// Lazy file open
|
||||
if(!isset($this->fp)) {
|
||||
if ($this->openFile() === false) {
|
||||
return; // Do not write if file open failed.
|
||||
}
|
||||
}
|
||||
|
||||
// Lock the file while writing and possible rolling over
|
||||
if(flock($this->fp, LOCK_EX)) {
|
||||
|
||||
// Write to locked file
|
||||
if(fwrite($this->fp, $string) === false) {
|
||||
$this->warn("Failed writing to file. Closing appender.");
|
||||
$this->closed = true;
|
||||
}
|
||||
|
||||
// Stats cache must be cleared, otherwise filesize() returns cached results
|
||||
// If supported (PHP 5.3+), clear only the state cache for the target file
|
||||
if ($this->clearConditional) {
|
||||
clearstatcache(true, $this->file);
|
||||
} else {
|
||||
clearstatcache();
|
||||
}
|
||||
|
||||
// Rollover if needed
|
||||
if (filesize($this->file) > $this->maxFileSize) {
|
||||
try {
|
||||
$this->rollOver();
|
||||
} catch (LoggerException $ex) {
|
||||
$this->warn("Rollover failed: " . $ex->getMessage() . " Closing appender.");
|
||||
$this->closed = true;
|
||||
}
|
||||
}
|
||||
|
||||
flock($this->fp, LOCK_UN);
|
||||
} else {
|
||||
$this->warn("Failed locking file for writing. Closing appender.");
|
||||
$this->closed = true;
|
||||
}
|
||||
}
|
||||
|
||||
public function activateOptions() {
|
||||
parent::activateOptions();
|
||||
|
||||
if ($this->compress && !extension_loaded('zlib')) {
|
||||
$this->warn("The 'zlib' extension is required for file compression. Disabling compression.");
|
||||
$this->compression = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the 'maxBackupIndex' parameter.
|
||||
* @param integer $maxBackupIndex
|
||||
*/
|
||||
public function setMaxBackupIndex($maxBackupIndex) {
|
||||
$this->setPositiveInteger('maxBackupIndex', $maxBackupIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the 'maxBackupIndex' parameter.
|
||||
* @return integer
|
||||
*/
|
||||
public function getMaxBackupIndex() {
|
||||
return $this->maxBackupIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the 'maxFileSize' parameter.
|
||||
* @param mixed $maxFileSize
|
||||
*/
|
||||
public function setMaxFileSize($maxFileSize) {
|
||||
$this->setFileSize('maxFileSize', $maxFileSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the 'maxFileSize' parameter.
|
||||
* @return integer
|
||||
*/
|
||||
public function getMaxFileSize() {
|
||||
return $this->maxFileSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the 'maxFileSize' parameter (kept for backward compatibility).
|
||||
* @param mixed $maxFileSize
|
||||
* @deprecated Use setMaxFileSize() instead.
|
||||
*/
|
||||
public function setMaximumFileSize($maxFileSize) {
|
||||
$this->warn("The 'maximumFileSize' parameter is deprecated. Use 'maxFileSize' instead.");
|
||||
return $this->setMaxFileSize($maxFileSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the 'compress' parameter.
|
||||
* @param boolean $compress
|
||||
*/
|
||||
public function setCompress($compress) {
|
||||
$this->setBoolean('compress', $compress);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the 'compress' parameter.
|
||||
* @param boolean
|
||||
*/
|
||||
public function getCompress() {
|
||||
return $this->compress;
|
||||
}
|
||||
}
|
122
sources/lib/Log4php/appenders/LoggerAppenderSocket.php
Normal file
122
sources/lib/Log4php/appenders/LoggerAppenderSocket.php
Normal file
@ -0,0 +1,122 @@
|
||||
<?php
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* LoggerAppenderSocket appends to a network socket.
|
||||
*
|
||||
* ## Configurable parameters: ##
|
||||
*
|
||||
* - **remoteHost** - Target remote host.
|
||||
* - **port** - Target port (optional, defaults to 4446).
|
||||
* - **timeout** - Connection timeout in seconds (optional, defaults to
|
||||
* 'default_socket_timeout' from php.ini)
|
||||
*
|
||||
* The socket will by default be opened in blocking mode.
|
||||
*
|
||||
* @version $Revision: 1337820 $
|
||||
* @package log4php
|
||||
* @subpackage appenders
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
|
||||
* @link http://logging.apache.org/log4php/docs/appenders/socket.html Appender documentation
|
||||
*/
|
||||
class LoggerAppenderSocket extends LoggerAppender {
|
||||
|
||||
/**
|
||||
* Target host.
|
||||
* @see http://php.net/manual/en/function.fsockopen.php
|
||||
*/
|
||||
protected $remoteHost;
|
||||
|
||||
/** Target port */
|
||||
protected $port = 4446;
|
||||
|
||||
/** Connection timeout in ms. */
|
||||
protected $timeout;
|
||||
|
||||
// ******************************************
|
||||
// *** Appender methods ***
|
||||
// ******************************************
|
||||
|
||||
/** Override the default layout to use serialized. */
|
||||
public function getDefaultLayout() {
|
||||
return new LoggerLayoutSerialized();
|
||||
}
|
||||
|
||||
public function activateOptions() {
|
||||
if (empty($this->remoteHost)) {
|
||||
$this->warn("Required parameter [remoteHost] not set. Closing appender.");
|
||||
$this->closed = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (empty($this->timeout)) {
|
||||
$this->timeout = ini_get("default_socket_timeout");
|
||||
}
|
||||
|
||||
$this->closed = false;
|
||||
}
|
||||
|
||||
public function append(LoggerLoggingEvent $event) {
|
||||
$socket = fsockopen($this->remoteHost, $this->port, $errno, $errstr, $this->timeout);
|
||||
if ($socket === false) {
|
||||
$this->warn("Could not open socket to {$this->remoteHost}:{$this->port}. Closing appender.");
|
||||
$this->closed = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (false === fwrite($socket, $this->layout->format($event))) {
|
||||
$this->warn("Error writing to socket. Closing appender.");
|
||||
$this->closed = true;
|
||||
}
|
||||
fclose($socket);
|
||||
}
|
||||
|
||||
// ******************************************
|
||||
// *** Accessor methods ***
|
||||
// ******************************************
|
||||
|
||||
/** Sets the target host. */
|
||||
public function setRemoteHost($hostname) {
|
||||
$this->setString('remoteHost', $hostname);
|
||||
}
|
||||
|
||||
/** Sets the target port */
|
||||
public function setPort($port) {
|
||||
$this->setPositiveInteger('port', $port);
|
||||
}
|
||||
|
||||
/** Sets the timeout. */
|
||||
public function setTimeout($timeout) {
|
||||
$this->setPositiveInteger('timeout', $timeout);
|
||||
}
|
||||
|
||||
/** Returns the target host. */
|
||||
public function getRemoteHost() {
|
||||
return $this->getRemoteHost();
|
||||
}
|
||||
|
||||
/** Returns the target port. */
|
||||
public function getPort() {
|
||||
return $this->port;
|
||||
}
|
||||
|
||||
/** Returns the timeout */
|
||||
public function getTimeout() {
|
||||
return $this->timeout;
|
||||
}
|
||||
}
|
303
sources/lib/Log4php/appenders/LoggerAppenderSyslog.php
Normal file
303
sources/lib/Log4php/appenders/LoggerAppenderSyslog.php
Normal file
@ -0,0 +1,303 @@
|
||||
<?php
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Log events to a system log using the PHP syslog() function.
|
||||
*
|
||||
* This appenders requires a layout.
|
||||
*
|
||||
* ## Configurable parameters: ##
|
||||
*
|
||||
* - **ident** - The ident of the syslog message.
|
||||
* - **priority** - The priority for the syslog message (used when overriding
|
||||
* priority).
|
||||
* - **facility** - The facility for the syslog message
|
||||
* - **overridePriority** - If set to true, the message priority will always
|
||||
* use the value defined in {@link $priority}, otherwise the priority will
|
||||
* be determined by the message's log level.
|
||||
* - **option** - The option value for the syslog message.
|
||||
*
|
||||
* Recognised syslog options are:
|
||||
*
|
||||
* - CONS - if there is an error while sending data to the system logger, write directly to the system console
|
||||
* - NDELAY - open the connection to the logger immediately
|
||||
* - ODELAY - delay opening the connection until the first message is logged (default)
|
||||
* - PERROR - print log message also to standard error
|
||||
* - PID - include PID with each message
|
||||
*
|
||||
* Multiple options can be set by delimiting them with a pipe character,
|
||||
* e.g.: "CONS|PID|PERROR".
|
||||
*
|
||||
* Recognised syslog priorities are:
|
||||
*
|
||||
* - EMERG
|
||||
* - ALERT
|
||||
* - CRIT
|
||||
* - ERR
|
||||
* - WARNING
|
||||
* - NOTICE
|
||||
* - INFO
|
||||
* - DEBUG
|
||||
*
|
||||
* Levels are mapped as follows:
|
||||
*
|
||||
* - <b>FATAL</b> to LOG_ALERT
|
||||
* - <b>ERROR</b> to LOG_ERR
|
||||
* - <b>WARN</b> to LOG_WARNING
|
||||
* - <b>INFO</b> to LOG_INFO
|
||||
* - <b>DEBUG</b> to LOG_DEBUG
|
||||
* - <b>TRACE</b> to LOG_DEBUG
|
||||
*
|
||||
* @version $Revision: 1337820 $
|
||||
* @package log4php
|
||||
* @subpackage appenders
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
|
||||
* @link http://logging.apache.org/log4php/docs/appenders/syslog.html Appender documentation
|
||||
*/
|
||||
class LoggerAppenderSyslog extends LoggerAppender {
|
||||
|
||||
/**
|
||||
* The ident string is added to each message. Typically the name of your application.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $ident = "Apache log4php";
|
||||
|
||||
/**
|
||||
* The syslog priority to use when overriding priority. This setting is
|
||||
* required if {@link overridePriority} is set to true.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $priority;
|
||||
|
||||
/**
|
||||
* The option used when opening the syslog connection.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $option = 'PID|CONS';
|
||||
|
||||
/**
|
||||
* The facility value indicates the source of the message.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $facility = 'USER';
|
||||
|
||||
/**
|
||||
* If set to true, the message priority will always use the value defined
|
||||
* in {@link $priority}, otherwise the priority will be determined by the
|
||||
* message's log level.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $overridePriority = false;
|
||||
|
||||
/**
|
||||
* Holds the int value of the {@link $priority}.
|
||||
* @var int
|
||||
*/
|
||||
private $intPriority;
|
||||
|
||||
/**
|
||||
* Holds the int value of the {@link $facility}.
|
||||
* @var int
|
||||
*/
|
||||
private $intFacility;
|
||||
|
||||
/**
|
||||
* Holds the int value of the {@link $option}.
|
||||
* @var int
|
||||
*/
|
||||
private $intOption;
|
||||
|
||||
/**
|
||||
* Sets the {@link $ident}.
|
||||
*
|
||||
* @param string $ident
|
||||
*/
|
||||
public function setIdent($ident) {
|
||||
$this->ident = $ident;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link $priority}.
|
||||
*
|
||||
* @param string $priority
|
||||
*/
|
||||
public function setPriority($priority) {
|
||||
$this->priority = $priority;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link $facility}.
|
||||
*
|
||||
* @param string $facility
|
||||
*/
|
||||
public function setFacility($facility) {
|
||||
$this->facility = $facility;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link $overridePriority}.
|
||||
*
|
||||
* @param string $overridePriority
|
||||
*/
|
||||
public function setOverridePriority($overridePriority) {
|
||||
$this->overridePriority = $overridePriority;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the 'option' parameter.
|
||||
*
|
||||
* @param string $option
|
||||
*/
|
||||
public function setOption($option) {
|
||||
$this->option = $option;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the 'ident' parameter.
|
||||
*
|
||||
* @return string $ident
|
||||
*/
|
||||
public function getIdent() {
|
||||
return $this->ident;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the 'priority' parameter.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPriority() {
|
||||
return $this->priority;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the 'facility' parameter.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFacility() {
|
||||
return $this->facility;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the 'overridePriority' parameter.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getOverridePriority() {
|
||||
return $this->overridePriority;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the 'option' parameter.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getOption() {
|
||||
return $this->option;
|
||||
}
|
||||
|
||||
|
||||
public function activateOptions() {
|
||||
$this->intPriority = $this->parsePriority();
|
||||
$this->intOption = $this->parseOption();
|
||||
$this->intFacility = $this->parseFacility();
|
||||
|
||||
$this->closed = false;
|
||||
}
|
||||
|
||||
public function close() {
|
||||
if($this->closed != true) {
|
||||
closelog();
|
||||
$this->closed = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the event to syslog.
|
||||
*
|
||||
* Log is opened and closed each time because if it is not closed, it
|
||||
* can cause the Apache httpd server to log to whatever ident/facility
|
||||
* was used in openlog().
|
||||
*
|
||||
* @see http://www.php.net/manual/en/function.syslog.php#97843
|
||||
*/
|
||||
public function append(LoggerLoggingEvent $event) {
|
||||
$priority = $this->getSyslogPriority($event->getLevel());
|
||||
$message = $this->layout->format($event);
|
||||
|
||||
openlog($this->ident, $this->intOption, $this->intFacility);
|
||||
syslog($priority, $message);
|
||||
closelog();
|
||||
}
|
||||
|
||||
/** Determines which syslog priority to use based on the given level. */
|
||||
private function getSyslogPriority(LoggerLevel $level) {
|
||||
if($this->overridePriority) {
|
||||
return $this->intPriority;
|
||||
}
|
||||
return $level->getSyslogEquivalent();
|
||||
}
|
||||
|
||||
/** Parses a syslog option string and returns the correspodning int value. */
|
||||
private function parseOption() {
|
||||
$value = 0;
|
||||
$options = explode('|', $this->option);
|
||||
|
||||
foreach($options as $option) {
|
||||
if (!empty($option)) {
|
||||
$constant = "LOG_" . trim($option);
|
||||
if (defined($constant)) {
|
||||
$value |= constant($constant);
|
||||
} else {
|
||||
trigger_error("log4php: Invalid syslog option provided: $option. Whole option string: {$this->option}.", E_USER_WARNING);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/** Parses the facility string and returns the corresponding int value. */
|
||||
private function parseFacility() {
|
||||
if (!empty($this->facility)) {
|
||||
$constant = "LOG_" . trim($this->facility);
|
||||
if (defined($constant)) {
|
||||
return constant($constant);
|
||||
} else {
|
||||
trigger_error("log4php: Invalid syslog facility provided: {$this->facility}.", E_USER_WARNING);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Parses the priority string and returns the corresponding int value. */
|
||||
private function parsePriority() {
|
||||
if (!empty($this->priority)) {
|
||||
$constant = "LOG_" . trim($this->priority);
|
||||
if (defined($constant)) {
|
||||
return constant($constant);
|
||||
} else {
|
||||
trigger_error("log4php: Invalid syslog priority provided: {$this->priority}.", E_USER_WARNING);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user