3 namespace RedBeanPHP\Logger;
5 use RedBeanPHP\Logger as Logger;
6 use RedBeanPHP\RedException as RedException;
9 * Logger. Provides a basic logging function for RedBeanPHP.
11 * @file RedBeanPHP/Logger.php
12 * @author Gabor de Mooij and the RedBeanPHP Community
16 * copyright (c) G.J.G.T. (Gabor) de Mooij
17 * This source file is subject to the BSD/GPLv2 License that is bundled
18 * with this source code in the file license.txt.
20 class RDefault implements Logger
25 const C_LOGGER_ECHO = 0;
26 const C_LOGGER_ARRAY = 1;
36 protected $logs = array();
39 * Default logger method logging to STDOUT.
40 * This is the default/reference implementation of a logger.
41 * This method will write the message value to STDOUT (screen) unless
42 * you have changed the mode of operation to C_LOGGER_ARRAY.
44 * @param $message (optional) message to log (might also be data or output)
50 if ( func_num_args() < 1 ) return;
52 foreach ( func_get_args() as $argument ) {
53 if ( is_array( $argument ) ) {
54 $log = var_export( $argument, TRUE );
55 if ( $this->mode === self::C_LOGGER_ECHO ) {
61 if ( $this->mode === self::C_LOGGER_ECHO ) {
64 $this->logs[] = $argument;
68 if ( $this->mode === self::C_LOGGER_ECHO ) echo "<br>" . PHP_EOL;
73 * Returns the internal log array.
74 * The internal log array is where all log messages are stored.
78 public function getLogs()
84 * Clears the internal log array, removing all
85 * previously stored entries.
89 public function clear()
91 $this->logs = array();
96 * Selects a logging mode.
97 * There are several options available.
99 * * C_LOGGER_ARRAY - log silently, stores entries in internal log array only
100 * * C_LOGGER_ECHO - also forward log messages directly to STDOUT
102 * @param integer $mode mode of operation for logging object
106 public function setMode( $mode )
108 if ($mode !== self::C_LOGGER_ARRAY && $mode !== self::C_LOGGER_ECHO ) {
109 throw new RedException( 'Invalid mode selected for logger, use C_LOGGER_ARRAY or C_LOGGER_ECHO.' );
116 * Searches for all log entries in internal log array
117 * for $needle and returns those entries.
118 * This method will return an array containing all matches for your
121 * @param string $needle phrase to look for in internal log array
125 public function grep( $needle )
128 foreach( $this->logs as $logEntry ) {
129 if ( strpos( $logEntry, $needle ) !== FALSE ) $found[] = $logEntry;