1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Log |
4
|
|
|
* |
5
|
|
|
* ログ操作のためのファンクション群 |
6
|
|
|
* |
7
|
|
|
* @package risoluto |
8
|
|
|
* @author Risoluto Developers |
9
|
|
|
* @license http://opensource.org/licenses/bsd-license.php new BSD license |
10
|
|
|
* @copyright (C) 2008-2015 Risoluto Developers / All Rights Reserved. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
//------------------------------------------------------// |
14
|
|
|
// 名前空間の定義 |
15
|
|
|
//------------------------------------------------------// |
16
|
|
|
namespace Risoluto; |
17
|
|
|
|
18
|
|
|
//------------------------------------------------------// |
19
|
|
|
// クラス定義 |
20
|
|
|
//------------------------------------------------------// |
21
|
|
|
class Log |
22
|
|
|
{ |
23
|
|
|
//------------------------------------------------------// |
24
|
|
|
// クラス変数定義 |
25
|
|
|
//------------------------------------------------------// |
26
|
|
|
/** |
27
|
|
|
* $logfile |
28
|
|
|
* @access private |
29
|
|
|
* @var string ログの出力パス |
30
|
|
|
*/ |
31
|
|
|
private $logfile = ''; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* $currentloglevel |
35
|
|
|
* @access private |
36
|
|
|
* @var string 現在のログレベル |
37
|
|
|
*/ |
38
|
|
|
private $currentloglevel = 'warn'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* $loglevel |
42
|
|
|
* @access private |
43
|
|
|
* @var array ログレベル閾値情報を保持 |
44
|
|
|
*/ |
45
|
|
|
private $loglevel = [ |
46
|
|
|
"stop" => 0, |
47
|
|
|
"emerg" => 1, |
48
|
|
|
"alert" => 2, |
49
|
|
|
"crit" => 3, |
50
|
|
|
"error" => 4, |
51
|
|
|
"warn" => 5, |
52
|
|
|
"notice" => 6, |
53
|
|
|
"info" => 7, |
54
|
|
|
"debug" => 8, |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
//------------------------------------------------------// |
58
|
|
|
// クラスメソッド定義 |
59
|
|
|
//------------------------------------------------------// |
60
|
|
|
/** |
61
|
|
|
* __construct() |
62
|
|
|
* |
63
|
|
|
* コンストラクタ |
64
|
|
|
* |
65
|
|
|
* @access public |
66
|
|
|
* @return \Risoluto\Log |
|
|
|
|
67
|
|
|
*/ |
68
|
|
|
public function __construct() |
69
|
|
|
{ |
70
|
|
|
$this->logfile = RISOLUTO_LOGS . "risoluto-[[[REPLACED]]].log"; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* setLogFile($path) |
75
|
|
|
* |
76
|
|
|
* ログ出力パスをセットする |
77
|
|
|
* |
78
|
|
|
* @access public |
79
|
|
|
* |
80
|
|
|
* @param string $path ログ出力パス |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
public function setLogFile( $path ) |
85
|
|
|
{ |
86
|
|
|
$this->logfile = $path; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* setCurrentLogLevel($loglevel) |
91
|
|
|
* |
92
|
|
|
* ログレベルをセットする |
93
|
|
|
* |
94
|
|
|
* @access public |
95
|
|
|
* |
96
|
|
|
* @param string $loglevel ログレベル |
97
|
|
|
* |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
public function setCurrentLogLevel( $loglevel ) |
101
|
|
|
{ |
102
|
|
|
$this->currentloglevel = $loglevel; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* log($loglvl, $logmes) |
107
|
|
|
* |
108
|
|
|
* 指定されたログレベルで指定された文字列を出力する |
109
|
|
|
* |
110
|
|
|
* @param string $loglvl ログレベル(stop|emerg|alert|crit|error|warn|notice|info|debug) |
111
|
|
|
* @param string $logmes 出力するメッセージ |
112
|
|
|
* |
113
|
|
|
* @return boolean 出力結果({書き込んだバイト数}:正常終了/false:異常終了) |
114
|
|
|
*/ |
115
|
|
|
public function log( $loglvl, $logmes ) |
116
|
|
|
{ |
117
|
|
|
//-- ローカル変数 --// |
118
|
|
|
$currentdate = date( \DateTime::W3C ); |
119
|
|
|
$outfile = str_replace( '[[[REPLACED]]]', date( 'Ymd' ), $this->logfile ); |
120
|
|
|
|
121
|
|
|
// 現在のログレベル以下の場合は出力しない |
122
|
|
|
if ($this->loglevel[ $this->currentloglevel ] < $this->loglevel[ $loglvl ]) { |
123
|
|
|
return true; |
124
|
|
|
} else { |
125
|
|
|
return file_put_contents( $outfile, "[$loglvl at $currentdate] $logmes\n", FILE_APPEND | LOCK_EX ); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.