1: <?php
2:
3: namespace RedBeanPHP\Logger;
4:
5: use RedBeanPHP\Logger as Logger;
6: use RedBeanPHP\RedException as RedException;
7:
8: /**
9: * Logger. Provides a basic logging function for RedBeanPHP.
10: *
11: * @file RedBeanPHP/Logger.php
12: * @author Gabor de Mooij and the RedBeanPHP Community
13: * @license BSD/GPLv2
14: *
15: * @copyright
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.
19: */
20: class RDefault implements Logger
21: {
22: /**
23: * Logger modes
24: */
25: const C_LOGGER_ECHO = 0;
26: const C_LOGGER_ARRAY = 1;
27:
28: /**
29: * @var integer
30: */
31: protected $mode = 0;
32:
33: /**
34: * @var array
35: */
36: protected $logs = array();
37:
38: /**
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.
43: *
44: * @param $message (optional) message to log (might also be data or output)
45: *
46: * @return void
47: */
48: public function log()
49: {
50: if ( func_num_args() < 1 ) return;
51:
52: foreach ( func_get_args() as $argument ) {
53: if ( is_array( $argument ) ) {
54: $log = print_r( $argument, TRUE );
55: if ( $this->mode === self::C_LOGGER_ECHO ) {
56: echo $log;
57: } else {
58: $this->logs[] = $log;
59: }
60: } else {
61: if ( $this->mode === self::C_LOGGER_ECHO ) {
62: echo $argument;
63: } else {
64: $this->logs[] = $argument;
65: }
66: }
67:
68: if ( $this->mode === self::C_LOGGER_ECHO ) echo "<br>" . PHP_EOL;
69: }
70: }
71:
72: /**
73: * Returns the internal log array.
74: * The internal log array is where all log messages are stored.
75: *
76: * @return array
77: */
78: public function getLogs()
79: {
80: return $this->logs;
81: }
82:
83: /**
84: * Clears the internal log array, removing all
85: * previously stored entries.
86: *
87: * @return self
88: */
89: public function clear()
90: {
91: $this->logs = array();
92: return $this;
93: }
94:
95: /**
96: * Selects a logging mode.
97: * There are several options available.
98: *
99: * * C_LOGGER_ARRAY - log silently, stores entries in internal log array only
100: * * C_LOGGER_ECHO - also forward log messages directly to STDOUT
101: *
102: * @param integer $mode mode of operation for logging object
103: *
104: * @return self
105: */
106: public function setMode( $mode )
107: {
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.' );
110: }
111: $this->mode = $mode;
112: return $this;
113: }
114:
115: /**
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
119: * search query.
120: *
121: * @param string $needle phrase to look for in internal log array
122: *
123: * @return array
124: */
125: public function grep( $needle )
126: {
127: $found = array();
128: foreach( $this->logs as $logEntry ) {
129: if ( strpos( $logEntry, $needle ) !== FALSE ) $found[] = $logEntry;
130: }
131: return $found;
132: }
133: }
134: