-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog.php
More file actions
93 lines (81 loc) · 2.16 KB
/
Log.php
File metadata and controls
93 lines (81 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
/**
* @package framework
* @copyright Copyright (c) 2005-2020 The Regents of the University of California.
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Qubeshub\Database;
/**
* Database log class
*/
class Log
{
const HZPATH = '/var/log/hubzero-cms';
const FILENAME = 'sql.log';
/**
* Logs queries to hubzero sql log
*
* @param string $query The query to log
* @param int $time The query time
* @return void
* @since 2.0.0
**/
public static function add($query, $time = 0)
{
list($file, $line) = self::parseBacktrace();
list($type) = explode(' ', $query, 2);
self::write("$file $line " . strtoupper($type) . " {$time} " . str_replace("\n", ' ', $query));
}
/**
* Writes the log statment out
*
* @return void
* @since 2.0.0
**/
public static function write($statement)
{
$logger = new \Hubzero\Log\Writer(
new \Monolog\Logger(\Config::get('application_env')),
\Event::getRoot()
);
$path = (is_dir(self::HZPATH)) ? self::HZPATH : \Config::get('log_path');
$logger->useFiles($path . DS . self::FILENAME, 'info', "%datetime% %message%\n", "Y-m-d\TH:i:s.uP", 0640);
$logger->info($statement);
}
/**
* Parses the debug backtrace for the applicable file and line
*
* @return array
* @since 2.0.0
**/
public static function parseBacktrace()
{
$file = '-';
$line = 0;
// Loop through the backtrace items
foreach (self::getBacktrace() as $item)
{
// Looking for the last instance of one of the following classes...
// this will be our indicator of the command that originated this query
if (isset($item['class'])
&& ($item['class'] == 'Hubzero\Database\Relational'
|| $item['class'] == 'Hubzero\Database\Relationship\Relationship'
|| $item['class'] == 'Hubzero\Database\Rows'))
{
$file = (isset($item['file'])) ? str_replace(PATH_CORE, '', $item['file']) : $file;
$line = (isset($item['line'])) ? $item['line'] : $line;
}
}
return array($file, $line);
}
/**
* Gets the debug backtrace from php
*
* @return array
* @since 2.0.0
**/
public static function getBacktrace()
{
return debug_backtrace();
}
}