1
|
|
|
<?php |
|
|
|
|
2
|
|
|
// This file is part of Moodle - http://moodle.org/ |
3
|
|
|
// |
4
|
|
|
// Moodle is free software: you can redistribute it and/or modify |
5
|
|
|
// it under the terms of the GNU General Public License as published by |
6
|
|
|
// the Free Software Foundation, either version 3 of the License, or |
7
|
|
|
// (at your option) any later version. |
8
|
|
|
// |
9
|
|
|
// Moodle is distributed in the hope that it will be useful, |
10
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12
|
|
|
// GNU General Public License for more details. |
13
|
|
|
// |
14
|
|
|
// You should have received a copy of the GNU General Public License |
15
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Template log writer |
19
|
|
|
* |
20
|
|
|
* @package logstore_fluentd |
21
|
|
|
* @copyright 2015 Daniel Neis (based on standard log store from Petr Skoda) |
22
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
namespace logstore_fluentd\log; |
27
|
|
|
|
28
|
|
|
global $CFG; |
|
|
|
|
29
|
|
|
|
30
|
|
|
require_once($CFG->dirroot.'/admin/tool/log/store/fluentd/vendor/autoload.php'); |
31
|
|
|
require_once($CFG->dirroot.'/lib/filelib.php'); |
32
|
|
|
|
33
|
|
|
use Fluent\Autoloader, |
34
|
|
|
Fluent\Logger\FluentLogger; |
35
|
|
|
|
36
|
|
|
Autoloader::register(); |
37
|
|
|
|
38
|
|
|
defined('MOODLE_INTERNAL') || die(); |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
class store implements \tool_log\log\writer { |
42
|
|
|
use \tool_log\helper\store, |
43
|
|
|
\tool_log\helper\reader; |
44
|
|
|
|
45
|
|
|
/** @var string $logguests true if logging guest access */ |
46
|
|
|
protected $logguests; |
47
|
|
|
|
48
|
|
|
private $logger; |
49
|
|
|
|
50
|
|
|
public function __construct(\tool_log\log\manager $manager) { |
51
|
|
|
$this->helper_setup($manager); |
52
|
|
|
// Log everything before setting is saved for the first time. |
53
|
|
|
$this->logguests = $this->get_config('logguests', 1); |
54
|
|
|
$this->buffersize = 0; |
55
|
|
|
$this->logger = new FluentLogger($this->get_config('fluentd_url', 'localhost'), $this->get_config('fluentd_port', 24224)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Should the event be ignored (== not logged)? |
60
|
|
|
* @param \core\event\base $event |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
protected function is_event_ignored(\core\event\base $event) { |
64
|
|
|
if ((!CLI_SCRIPT or PHPUNIT_TEST) and !$this->logguests) { |
|
|
|
|
65
|
|
|
// Always log inside CLI scripts because we do not login there. |
66
|
|
|
if (!isloggedin() or isguestuser()) { |
|
|
|
|
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
public function write(\core\event\base $event) { |
73
|
|
|
$this->logger->post($this->get_config('fluentd_tag', 'fluentd.moodle'), $event->get_data()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Write one event to the store. |
78
|
|
|
* |
79
|
|
|
* @param \core\event\base $evententries |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
public function insert_event_entries($evententries) { |
83
|
|
|
foreach ($evententries as $e) { |
84
|
|
|
$this->write($e); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Fetch records using given criteria returning a Traversable object. |
90
|
|
|
* |
91
|
|
|
* Note that the traversable object contains a moodle_recordset, so |
92
|
|
|
* remember that is important that you call close() once you finish |
93
|
|
|
* using it. |
94
|
|
|
* |
95
|
|
|
* @param string $selectwhere |
96
|
|
|
* @param array $params |
97
|
|
|
* @param string $sort |
98
|
|
|
* @param int $limitfrom |
99
|
|
|
* @param int $limitnum |
100
|
|
|
* @return \core\dml\recordset_walk|\core\event\base[] |
101
|
|
|
*/ |
102
|
|
|
public function get_events_select_iterator($selectwhere, array $params, $sort, $limitfrom, $limitnum) { |
103
|
|
|
return null; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns an event from the log data. |
108
|
|
|
* |
109
|
|
|
* @param \stdClass $data Log data |
110
|
|
|
* @return \core\event\base |
111
|
|
|
*/ |
112
|
|
|
public function get_log_event($data) { |
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function get_events_select_count($selectwhere, array $params) { |
117
|
|
|
return null; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function get_internal_log_table_name() { |
121
|
|
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Are the new events appearing in the reader? |
126
|
|
|
* |
127
|
|
|
* @return bool true means new log events are being added, false means no new data will be added |
128
|
|
|
*/ |
129
|
|
|
public function is_logging() { |
130
|
|
|
// Only enabled stpres are queried, |
131
|
|
|
// this means we can return true here unless store has some extra switch. |
132
|
|
|
return true; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function dispose() { |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.